《Windows程序设计》学习笔记(chap10菜单及其它资源)(二)

2019-04-13 15:16发布

题外话:吃完晚饭再次看《Windows程序设计》,效率果然不行。呃……貌似今天看的有点多。   晚上弄了半天,俄罗斯方块的工程里面总算是挂出菜单了(麻烦的资源加载诶),而且是创建时加载的菜单,不能修改,以后的代码有的改了…… GetLastError()这个函数还是要常用,调试就靠它了,不然断点设了死也看不出句柄对不对。 题外话2:模电课实在听不下去了(确切的来说是看不下去了,只自学,从不听课),打开上网本,继续coding。真对不起老师啊~(貌似从没对得起过) EnableMenuItem函数,MSDN定义如下: The EnableMenuItem function enables, disables, or grays the specified menu item. Syntax
Parameters
hMenu
[in] Handle to the menu.
uIDEnableItem
[in] Specifies the menu item to be enabled, disabled, or grayed, as determined by the uEnable parameter. This parameter specifies an item in a menu bar, menu, or submenu.
uEnable
[in] Controls the interpretation of the uIDEnableItem parameter and indicate whether the menu item is enabled, disabled, or grayed. This parameter must be a combination of either MF_BYCOMMAND or MF_BYPOSITION and MF_ENABLED, MF_DISABLED, or MF_GRAYED.
MF_BYCOMMAND
Indicates that uIDEnableItem gives the identifier of the menu item. If neither the MF_BYCOMMAND nor MF_BYPOSITION flag is specified, the MF_BYCOMMAND flag is the default flag.
MF_BYPOSITION
Indicates that uIDEnableItem gives the zero-based relative position of the menu item.
MF_DISABLED
Indicates that the menu item is disabled, but not grayed, so it cannot be selected.
MF_ENABLED
Indicates that the menu item is enabled and restored from a grayed state so that it can be selected.
MF_GRAYED
Indicates that the menu item is disabled and grayed so that it cannot be selected.
Return Value
The return value specifies the previous state of the menu item (it is either MF_DISABLED, MF_ENABLED, or MF_GRAYED). If the menu item does not exist, the return value is -1.
Remarks
An application must use the MF_BYPOSITION flag to specify the correct menu handle. If the menu handle to the menu bar is specified, the top-level menu item (an item in the menu bar) is affected. To set the state of an item in a drop-down menu or submenu by position, an application must specify a handle to the drop-down menu or submenu. When an application specifies the MF_BYCOMMAND flag, the system checks all items that open submenus in the menu identified by the specified menu handle. Therefore, unless duplicate menu items are present, specifying the menu handle to the menu bar is sufficient. The InsertMenu, InsertMenuItem, LoadMenuIndirect, ModifyMenu, and SetMenuItemInfo functions can also set the state (enabled, disabled, or grayed) of a menu item. When you change a window menu, the menu bar is not immediately updated. To force the update, call DrawMenuBar.
Function Information
Minimum DLL Version
user32.dll
Header
Declared in Winuser.h, include Windows.h
Import library
User32.lib
Minimum operating systems
Windows 95, Windows NT 3.1
Unicode
Implemented as Unicode version.
总而言之,就是简单的设置菜单是否选择和变灰,MF_BYCOMMAND和MF_BYPOSITION这两个标示符么,据我翻译就是是否和有关命令相连的标示,后者直接使其菜单选项失效。 SendMessage函数就举个使用案例就很好理解,直接发送message命令SendMessage (hwnd, WM_CLOSE, 0, 0) Syntax
LRESULT SendMessage(     

    HWND hWnd,     UINT Msg,     WPARAM wParam,     LPARAM lParam );
使用纯代码创建菜单???相当非主流的方法,基本不用,还是用资源好 但还是摘抄一下: 不知道还有人用这种方法么……难道是为了程序可以自定义菜单?不知不知……   右键弹出菜单 貌似也不难,就是在WM_CREATE消息中加入这两个函数 hMenu = LoadMenu (hInst, szAppName) ; hMenu = GetSubMenu (hMenu, 0) ; 然后在需要右击的地方(就是WM_RBUTTONUP这个消息里面)再调用该菜单 在WM_RBUTTONUP消息处理期间,POPMENU提供了鼠标指针的位置,将此位置转换为屏幕坐标,再将坐标值传递给TrackPopupMenu: PS: 题外话3:一节模电课又快结束了,有心情继续看书了,不写日志了,关机继续看模电书(依旧从不听课,还是对不住老师……) 题外话4:上课回来,继续菜单项目的学习…… hMenu = GetSystemMenu (hwnd, FALSE) ; 获取系统菜单的句柄,第二个参数为FALSE就是返回的是副本,为TRUE就返回最原始的菜单,返回值自然就是一个句柄啦。 修改菜单的函数: 改变菜单 我们已经看到了如何使用AppendMenu函数为程序定义菜单以及将菜单项加入到系统菜单中。在Windows 3.0之前,您不得不被迫使用ChangeMenu函数来完成这种工作。ChangeMenu函数有很多功能,至少在当时,整个Windows中它是最复杂的函数之一。现在,许多函数都比ChangeMenu函数还要复杂,并且ChangeMenu的功能被分解为五个新的函数:
  • AppendMenu在菜单尾部添加一个新的菜单项目
  • DeleteMenu删除菜单中一个现有的菜单项并清除该项目
  • InsertMenu在菜单中插入一个新项目
  • ModifyMenu修改一个现有的菜单项目
  • RemoveMenu从菜单中移走某一项目
如果菜单项是一个弹出式菜单,那么DeleteMenu和RemoveMenu之间的区别就很重要。DeleteMenu清除弹出式菜单,但RemoveMenu不清除它。
  1. 一些菜单用的其它函数,简略列一下:

1.更新菜单用函数:DrawMenuBar (hwnd) 2.获得弹出式菜单的句柄:hMenuPopup = GetSubMenu (hMenu, iPosition) ; 3.获得顶层菜单或者弹出式菜单中目前的项数:iCount = GetMenuItemCount (hMenu) ; 4.取得弹出式菜单项的菜单ID:id = GetMenuItemID (hMenuPopup, iPosition) ; 5.选中、或者取消选中弹出式菜单中的某一项:CheckMenuItem (hMenu, id, iCheck) ; 6.获取菜单字符串:iCharCount = GetMenuString (hMenu, id, pString, iMaxCount, iFlag) ; 7.菜单项目前的属性:iFlags = GetMenuState (hMenu, id, iFlag) ; 8.清除菜单:DestroyMenu (hMenu) ;
==================================================================   呼啦啦,菜单的学习到此告一段落。。。赶紧改我的代码去了 系统快捷键,有空再看