Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
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
Updated 24-Nov-12 7:53am
v2

1 solution

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900