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

边栏推荐
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- 【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
- Daily work and study notes
- DTL dephossite | prediction method of dephosphorylation sites based on Transfer Learning
- Sql--- related transactions
- [IVX junior engineer training course 10 papers to get certificates] 03 events and guessing numbers games
- [WesternCTF2018]shrine writeup
- [Chongqing Guangdong education] Tianshui Normal University universe exploration reference
- 游戏思考15:全区全服和分区分服的思考
- Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
猜你喜欢
![[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

6-3漏洞利用-SSH环境搭建

Edge extraction edges based on Halcon learning_ image. Hdev routine
![[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card](/img/99/53b0ae47bada8b0d4db30d0517fe3d.jpg)
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card

浅浅了解Servlet

How does schedulerx help users solve the problem of distributed task scheduling?

About asp Net core uses a small detail of datetime date type parameter

gradle

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

Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
随机推荐
[IVX junior engineer training course 10 papers] 02 numerical binding and adaptive website production
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
[eight sorts ②] select sort (select sort, heap sort)
Exclusive delivery of secret script move disassembly (the first time)
Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL application day02
Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated
Global and Chinese market of wireless chipsets 2022-2028: Research Report on technology, participants, trends, market size and share
XMIND mind map
Bilstm CRF code implementation
Minimize the error
6-3 vulnerability exploitation SSH environment construction
Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
CTF daily question day45 sensor
Brief introduction to the development of mobile network
We should make clear the branch prediction
Global and Chinese markets for the application of artificial intelligence in security, public security and national security 2022-2028: Research Report on technology, participants, trends, market size
How to extract login cookies when JMeter performs interface testing
6-2漏洞利用-ftp不可避免的问题
How does schedulerx help users solve the problem of distributed task scheduling?