当前位置:网站首页>Self drawing of menu items and CListBox items
Self drawing of menu items and CListBox items
2022-07-02 01:19:00 【Muxi ₀₃₁】
Self drawing of menu items
Self drawing of menu items
step :
1. Add the menu item that needs self drawing MF_OWNERDRAW, Use ModeifyMenu Function is suitable for
2. Use WM_MEASUREITEM Set the width and height of menu items
3. Use WM_DRAWITEM Draw menu items
- Get menu , Menu item plus MF_OWNERDRAW
CMenu*pMenu=pFrame->GetMenu();
ASSERT(pFrame!=NULL);
//ASSERT () Is a macro that is often used when debugging programs , When the program is running, it evaluates the expression in brackets , If the expression is FALSE (0), The program will report an error , And terminate execution . If the expression is not 0, Then continue to execute the following statements . ... ASSERT Only in Debug Only valid in version , If compiled as Release Version is ignored
for(int i=0;i<5;i++)
pMenu->ModifyMenu(IDM_COLOR_RED+i,MF_OWNERDRAW,IDM_COLOR_RED+i);
return TRUE;
- Set the width and height of menu items
void CMainFrame::OnMeasureItem(int nIDCtl,LPMEASUREITEMSTRUCT lpmis)
{
lpmis->itemWidth=::GetSystemMetrics(SM_CYMENU)*4;
lpmis->itemHeight=::GetSystemMetrics(SM_CYMENU);
}
- Draw menu items
Draw menu item steps
1. Highlight the selected item pBrush->CreateSolidBrush(::GetSysColor((lpdis->itemState & ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_WINDOW));
2. Draw the selected item border dc.FrameRect(&(lpdis->rcItem), pBrush);
3.BitBlt Draw the left check mark (√), Right rectangle fill color
void CMainFrame::OnDrawItem(int nIDCtl,LPDRAWITEMSTRUCT lpdis)
{
BITMAP bm;
CBitmap bitmap;
bitmap.LoadOEMBitmap(OBM_CHECK);
bitmap.GetObject(sizeof(bm),&bm);//int GetObject(int nCount,LPVOID lpObject) const;
//CGdiObject::GetObject Fill the buffer with data that defines the specified object &bm
//nCount :Specifies the number of bytes to copy into the lpObject buffer.
//lpObject:Points to a user-supplied buffer that is to receive the information.
//https://docs.microsoft.com/en-us/cpp/mfc/reference/cgdiobject-class?view=msvc-170
CDC dc;
dc.Attach(lpdis->hDC);
CBrush* pBrush = new CBrush;
// Highlight the selected menu item
pBrush->CreateSolidBrush(::GetSysColor((lpdis->itemState & ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_WINDOW));
// Picture frame
dc.FrameRect(&(lpdis->rcItem), pBrush);
if (lpdis->itemState & ODS_CHECKED)
{
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap* pOldBitMap = dcMem.SelectObject(&bitmap);
dc.BitBlt(lpdis->rcItem.left + 4, lpdis->rcItem.top + (((lpdis->rcItem.bottom - lpdis->rcItem.top) - bm.bmHeight) / 2),
bm.bmWidth, bm.bmHeight, &dcMem, 0, 0, SRCCOPY);
/* BOOL BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop); */
dcMem.SelectObject(pOldBitMap);
}
pBrush=new CBrush(col[lpdis->itemID-ID_COLOR_RED]);
CRect rect = lpdis->rcItem;
rect.DeflateRect(6,4);// Reduce the width and height , Don't move top And left
rect.left += bm.bmWidth;
dc.FillRect(rect, pBrush);
delete pBrush;
dc.Detach();
CFrameWnd::OnDrawItem(nIDCtl, lpdis);
}
- design sketch

CListBox Self drawing of items
step :
1. Cover ** CListBox::PreCreateWindow** function , Make sure cs style contain ** LBS_OWNERDRAWFIXED** or ** LBS_OWNERDRAWVARIABLE**
2. Cover ** CListBox::MeasureItem** Set the width and height of menu items
3. Cover ** CListBox::DrawItem** Draw menu items
class CMyListBox :public CListBox
{
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
int AddIcon(HICON hIcon);
void ProjectImage(CDC* pDC, LPRECT pRect, COLORREF clrBackColor);
DECLARE_MESSAGE_MAP()
afx_msg void OnLbnSelchange();
};
- Cover ** CListBox::PreCreateWindow** function , Make sure cs style contain ** LBS_OWNERDRAWFIXED** or ** LBS_OWNERDRAWVARIABLE**
CIconListBox::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |=LBS_OWNERDRAWFIXED;
// Indicates that it does not contain LBS_OWNERDRAWFIXED|LBS_SORT
cs.style &=~(LBS_OWNERDRAWFIXED|LBS_SORT);
}
- Cover ** CListBox::MeasureItem** function , Set up listbox item Height and width
void CIconListBox::MeasureItem(LPMEASUREITEMSTRUCT lpmis)
{
lpmis->itemHeight = 32;
}
- Cover ** CListBox::DrawItem** function , Began to draw ListBox term
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpdis)
{
CDC dc;
// load DrawItem dc
dc.Attach(lpdis->hDC);
CRect rect = lpdis->rcItem;
UINT nIndex = lpdis->itemID;
// Draw the selected item to highlight
CBrush* pBrush = new CBrush(::GetSysColor(lpdis->itemState & ODS_SELECTED ? COLOR_HIGHLIGHT : COLOR_WINDOW));
dc.FillRect(rect, pBrush);
delete pBrush;
// Draw the focus rectangle
if (lpdis->itemState&ODS_FOCUS)
{
dc.DrawFocusRect(rect);
}
if (nIndex!=(UINT)-1)
{
dc.DrawIcon(rect.left + 4, rect.top + 2, (HICON)GetItemData(nIndex));
}
/* BOOL DrawIcon( int x, int y, HICON hIcon ); //Retrieves the application-supplied doubleword value associated with the specified list-box item. DWORD_PTR GetItemData( int nIndex ) const; */
dc.Detach();
}
- Set up ListBox term Icon
int CMyListBox::AddIcon(HICON hIcon)
{
int nIndex=AddString(_T(""));
if ((nIndex!=LB_ERR)&&(nIndex!=LB_ERRSPACE))
{
SetItemData(nIndex, (DWORD)hIcon);
}
return nIndex;
}
- Displayed in the rcImage Area
void CMyListBox::ProjectImage(CDC* pDC, LPRECT pRect, COLORREF clrBackColor)
{
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDC, 32, 32);
CBitmap* pOldBitmap = dcMem.SelectObject(&bitmap);
CBrush* pBrush = new CBrush(clrBackColor);
dcMem.FillRect(CRect(0, 0, 32, 32), pBrush);
delete pBrush;
int nIndex = GetCurSel();
if (nIndex != LB_ERR)
dcMem.DrawIcon(0, 0, (HICON)GetItemData(nIndex));
pDC->StretchBlt(pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top, &dcMem, 0, 0, 32, 32, SRCCOPY);
dcMem.SelectObject(pOldBitmap);
}
- design sketch

边栏推荐
- GL Studio 5 安装与体验
- Minimize the error
- "C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
- 6-3 vulnerability exploitation SSH environment construction
- Shell Function
- Datawhale community blackboard newspaper (issue 1)
- SSO single sign on implementation.
- Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
- [IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
- The concept and application of Cartland number
猜你喜欢

How can I batch produce the same title for the video?

Excel PivotTable

gradle
![[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)](/img/0d/22f3f65ab9422383df9a55d0724d59.jpg)
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)

Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated

Since I understand the idea of dynamic planning, I have opened the door to a new world

SSO single sign on implementation.

The concept and application of Cartland number

学习笔记25--多传感器前融合技术
![[image enhancement] vascular image enhancement based on frangi filter with matlab code](/img/b3/b4164fb7db8645f470180e352b5717.png)
[image enhancement] vascular image enhancement based on frangi filter with matlab code
随机推荐
技术大佬准备就绪,话题C位由你决定
Review notes of compilation principles
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
How does schedulerx help users solve the problem of distributed task scheduling?
Advanced skills of testers: a guide to the application of unit test reports
【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码
学习笔记2--高精度地图定义及价值
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
GL Studio 5 installation and experience
Evolution of Himalayan self-developed gateway architecture
Global and Chinese markets for distributed generation and energy storage in telecommunications networks 2022-2028: Research Report on technology, participants, trends, market size and share
Develop a simple login logic based on SSM
Fastadmin controls the length of fields in the table
How does schedulerx help users solve the problem of distributed task scheduling?
[IVX junior engineer training course 10 papers to get certificates] 03 events and guessing numbers games
Han Zhichao: real time risk control practice of eBay based on graph neural network
The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
Cat Party (Easy Edition)
970 golang realizes the communication between multithreaded server and client
Day 13 of hcip (relevant contents of BGP agreement)