当前位置:网站首页>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

边栏推荐
- 6-3漏洞利用-SSH环境搭建
- Keepalived introduction and installation
- Sql--- related transactions
- Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
- SSO single sign on implementation.
- Edge extraction edges based on Halcon learning_ image. Hdev routine
- cookie、session、tooken
- [IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
- Global and Chinese markets of beverage seasoning systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
猜你喜欢

学习笔记24--多传感器后融合技术

Datawhale 社区黑板报(第1期)

Study note 2 -- definition and value of high-precision map

Edge computing accelerates live video scenes: clearer, smoother, and more real-time

969 interlaced string

【八大排序①】插入排序(直接插入排序、希尔排序)

The concept and application of Cartland number
![[eight sorting ③] quick sorting (dynamic graph deduction Hoare method, digging method, front and back pointer method)](/img/c2/7ebc67e9b886e3baf3c98489bf9bce.png)
[eight sorting ③] quick sorting (dynamic graph deduction Hoare method, digging method, front and back pointer method)

ACM tutorial - quick sort (regular + tail recursion + random benchmark)

Entrepreneurship is a little risky. Read the data and do a business analysis
随机推荐
Zak's latest "neural information transmission", with slides and videos
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
[IVX junior engineer training course 10 papers] 04 canvas and a group photo of IVX and me
Global and Chinese market of avionics systems 2022-2028: Research Report on technology, participants, trends, market size and share
Daily work and study notes
浅浅了解Servlet
教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
Circular statements in shell programming
MySQL application day02
Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
Recently, three articles in the nature sub Journal of protein and its omics knowledge map have solved the core problems of biology
We should make clear the branch prediction
How to reflect and solve the problem of bird flight? Why are planes afraid of birds?
Principle of finding combinatorial number and template code
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
How can I batch produce the same title for the video?
cookie、session、tooken
Bubble Sort Graph