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

边栏推荐
- Part 29 supplement (XXIX) basis of ECMAScript
- 【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
- 学习笔记2--高精度地图定义及价值
- Global and Chinese market of wireless chipsets 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of picture archiving and communication system (PACS) 2022-2028: Research Report on technology, participants, trends, market size and share
- 游戏思考15:全区全服和分区分服的思考
- 8.8.4-PointersOnC-20220215
- UDS bootloader of s32kxxx bootloader
- Global and Chinese markets for power over Ethernet (POE) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
- 微信小程序中使用tabBar
猜你喜欢

"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
![[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production](/img/dc/e9adb1b41c2175c6f18d8027e0530a.jpg)
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production

Learning notes 25 - multi sensor front fusion technology

gradle

MySQL application day02

Learn C language from scratch day 025 (maze)

Datawhale 社区黑板报(第1期)

Review notes of compilation principles

969 interlaced string

关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
随机推荐
I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
Datawhale 社区黑板报(第1期)
Luogu p1775 stone merger (weakened version)
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
Global and Chinese markets for freight and logistics 2022-2028: Research Report on technology, participants, trends, market size and share
Mathematics - feelings -20220215
Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production
Part 29 supplement (XXIX) basis of ECMAScript
What is commercial endowment insurance? Is commercial endowment insurance safe?
Principle of finding combinatorial number and template code
Sql--- related transactions
Zak's latest "neural information transmission", with slides and videos
【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
[JS download files through url]
Global and Chinese market of wireless chipsets 2022-2028: Research Report on technology, participants, trends, market size and share
How to reflect and solve the problem of bird flight? Why are planes afraid of birds?
8.8.4-PointersOnC-20220215
Exclusive delivery of secret script move disassembly (the first time)
969 interlaced string