Click here to Skip to main content
12,066,952 members (59,354 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Win32 menu Message
Hello Everyone,

I'm trying to redraw the menu (a menu with POPUP style ,right keydown menu), I already recoded three messages: WM_MESUREITEM, WM_INITPOPUP and WM_DRAWITEM, but the finally result doesn't make me satisfied.
The three messages above only deal with menu items, the whole menu frame looks so ugly, the frame of the menu has not been redrawn and I still do not know how.

Here I list the code of the three messages:
case WM_DRAWITEM:
{
	UINT ctlID = (UINT) wParam;  
	LPDRAWITEMSTRUCT lpDraw = (LPDRAWITEMSTRUCT)lParam; 
	//if (ctlID == 0)	{
		HDC hDC = lpDraw->hDC;
 
		SetBkMode(hDC , TRANSPARENT);
		char mStr[255] = {0};
		HMENU hMenu = (HMENU)lpDraw->hwndItem;
		int len = GetMenuString(hMenu ,lpDraw->itemID , mStr , 255 , MF_BYCOMMAND);
		//HRGN menuRgn = lpDraw->rcItem;
		HBRUSH hBrush = CreateSolidBrush(RGB(33, 82 ,153));
		HBRUSH hBrushSel = CreateSolidBrush(RGB(37,125,194));
		HBRUSH hBrushFont = CreateSolidBrush(RGB(206,206,206));
		HBRUSH hBrushUnSel = CreateSolidBrush(RGB(27,30,32));
		HBRUSH hBrushSep   = CreateSolidBrush(RGB(0,0,0));
		
		if (lpDraw->itemID != 0) {
			if (lpDraw->itemAction & ODA_SELECT)  
			{
				FillRect(hDC ,&(lpDraw->rcItem) , hBrushSel);
				if (lpDraw->itemState & ODS_GRAYED)
					SetTextColor(hDC , RGB(70,70,70));
				else SetTextColor(hDC , RGB(206,206,206));
			}
			if (!((lpDraw->itemAction & ODA_SELECT) &&(lpDraw->itemState & ODS_SELECTED))) 
			{
				FillRect(hDC ,&(lpDraw->rcItem) , hBrushUnSel);
				if (lpDraw->itemState & ODS_GRAYED)
					SetTextColor(hDC , RGB(70,70,70));
				else SetTextColor(hDC ,RGB(206,206,206));
			}		
		}
		else {
			HDC hDCMEM = CreateCompatibleDC(hDC);
			HBITMAP hBMP = LoadBitmap(G_hInstance , "SEPARATOR");
			SelectObject(hDCMEM , hBMP );
			StretchBlt(hDC , (lpDraw->rcItem).left ,(lpDraw->rcItem).top, RWIDTH(lpDraw->rcItem) ,RHEIGHT(lpDraw->rcItem),
				 hDCMem ,		1 , 0 , 1, 4 ,SRCCOPY);
			//FillRect(hDC ,&(lpDraw->rcItem) , hBrushSep);
			DeleteObject(hBMP);
			DeleteDC(hDCMEM);
			
		}
		TextOut(hDC ,(lpDraw->rcItem).left + 20 ,(lpDraw->rcItem).top + 4 , mStr , len  );
		DeleteObject(hBrush);
		DeleteObject(hBrushSel);
		DeleteObject(hBrushFont);
		DeleteObject(hBrushUnSel);
		DeleteObject(hBrushSep);
		return 0;
		//FrameRect(hDC , &(lpDraw->rcItem) , hBrush);
	//}
}
break;
case WM_MEASUREITEM: 
{
	UINT ctlID = (UINT) wParam; //控件标识符
	LPMEASUREITEMSTRUCT lpItemStruct = (LPMEASUREITEMSTRUCT)lParam; 
	if (lpItemStruct->CtlType == ODT_MENU ) {	 
		if (0 == lpItemStruct->itemID)	{
			lpItemStruct->itemWidth = 150;
			lpItemStruct->itemHeight = 2;
		}
		else {
			lpItemStruct->itemWidth = 150;
			lpItemStruct->itemHeight = 20;	
		}
		return 0;
	}			
	
}
break;
case WM_INITMENUPOPUP:
{
	HMENU hMenu = (HMENU)wParam;
	int		uPos = (UINT) LOWORD(lParam); 
	BOOL	fSystemMenu = (BOOL) HIWORD(lParam); 
	
	int itemCount = GetMenuItemCount(hMenu);
	for (int i = 0 ; i < itemCount ; i++)	{
		HMENU mItem = GetSubMenu(hMenu , i);
		UINT mItemID = GetMenuItemID(hMenu , i);
		char mStr[255] = {0};
		GetMenuString(hMenu , i , mStr , 255 , MF_BYPOSITION);
		ModifyMenu(hMenu,i,MF_BYPOSITION|MF_OWNERDRAW,mItemID,(LPCTSTR)mStr);
		//MessageBox(hWnd , mStr , "MENU" , MB_OK);
	}
	return 0;
}
break;

Hope anyone give me a suggestion , thanks !
Posted 24-Nov-12 8:29am
Younth490
Edited 24-Nov-12 8:53am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You have not really explained what your problem is, but some of these links[^] may help you.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 25 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100