windows编程之WM_MENUSELECT消息

WM_MENUSELECT消息:

(1) 程序窗口如下所示:

windows编程之WM_MENUSELECT消息

(2)程序中的消息处理如下所示:

LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)

{
	static int idColor [5] = {WHITE_BRUSH,LTGRAY_BRUSH, GRAY_BRUSH,DKGRAY_BRUSH, BLACK_BRUSH } ;
	static int iSelection = IDM_BKGND_WHITE ;
	HMENU      hMenu ;
	switch (message)
	{
	case WM_MENUSELECT:
		hMenu = GetMenu (hwnd) ;
		//
		//if( lParam != 0 )
		if(true)
		{
			switch (LOWORD (wParam))
			{
			case 0:
				MessageBox(NULL,TEXT("文件选中"),NULL,MB_OK);
				return 0;
			case 1:
				MessageBox(NULL,TEXT("编辑颜色选中"),NULL,MB_OK);
				return 0;

			case   IDM_FILE_NEW:
			case   IDM_FILE_OPEN:
			case   IDM_FILE_SAVE:
				MessageBeep (0) ;
				return 0 ;

			case   IDM_FILE_EXIT:
				SendMessage (hwnd, WM_CLOSE, 0, 0) ;
				return 0 ;

			case   IDM_BKGND_WHITE:              // Note: Logic below
			case   IDM_BKGND_LTGRAY:             // assumes that IDM_WHITE
			case   IDM_BKGND_GRAY:               // through IDM_BLACK are
			case   IDM_BKGND_DKGRAY:             // consecutive numbers in
			case   IDM_BKGND_BLACK:              // the order shown here.

				CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
				iSelection = LOWORD (wParam) ;
				CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
				//CheckMenuRadioItem(hMenu,IDM_BKGND_WHITE,IDM_BKGND_BLACK,iSelection,MF_BYCOMMAND);
				//重新设置窗口,这里是背景颜色
				SetClassLong(hwnd, GCL_HBRBACKGROUND, (LONG)GetStockObject(idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;
				InvalidateRect (hwnd, NULL, TRUE) ;
			break;
			case   IDM_ABOUT:
				MessageBox(hwnd, TEXT("Help not yet implemented!"),szAppName, MB_ICONEXCLAMATION | MB_OK) ;
				return 0 ;

			case   IDM_VERSION:
				MessageBox (hwnd,TEXT ("版本1"),szAppName, MB_ICONINFORMATION | MB_OK) ;
				break;
		}
	}
	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
(3)wParam ,lParam指代

wParam和lParam指代要看选择项是命令项还是弹出菜单,

<1>命令项的时候,LOWORD(wParam) = 菜单项ID

<2> 弹出菜单,LOWORD(wParam)= 弹出菜单相对于目菜单的索引。

<3>lParam代表的所选项所归属菜单的句柄。

(4)测试过程

<1>单击菜单中“帮助”
windows编程之WM_MENUSELECT消息

消息属性分别为(按顺序):

windows编程之WM_MENUSELECT消息

windows编程之WM_MENUSELECT消息

<2>单击菜单中“视图”按钮
windows编程之WM_MENUSELECT消息

消息属性(按顺序)

windows编程之WM_MENUSELECT消息

windows编程之WM_MENUSELECT消息

从上面可以看出,不管点击那个菜单项,最终都有个”菜单已关闭”的消息,这个时候的WM_MENUSELECT中的wParam= FFFF0000,lParam= 00000000.

即便是在菜单栏的空白处单击也会出现这样的WM_MENUSELECT的消息。







windows编程之WM_MENUSELECT消息,布布扣,bubuko.com

windows编程之WM_MENUSELECT消息

上一篇:解决.Net 3.1 alpine镜像无法连接Sql server的问题


下一篇:添加shell命令支持