论坛首页 综合技术论坛

WIN32汇编之菜单、加速键、快捷键

浏览 2601 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-02-20   最后修改:2010-02-20
(一)
Invoke checkmenuitem,hmenu,ebx,eax
.elseif eax>=IDM_BIG&&eax<=DM_DETAIL
invokecheckmenuradioitem,hmenu,IDM_BIG,IDM_DETAIL,eax,MF_BYCOMMAND
.endif
重点与难点:
1、关于checkmenuitem
The CheckMenuItem function sets the state of the specified menuitem's check-mark attribute to either selected or clear.
设置菜单项的check状态的选择或清除
Syntax
DWORD CheckMenuItem(
HMENU hmenu,
UINT uIDCheckItem,
UINT uCheck
);
参数说明
UCHECK如下:
MF_BYCOMMAND uIDCheckItem参数为为命令ID
Indicates that the uIDCheckItem parameter gives the identifier ofthe menu item. The MF_BYCOMMAND flag is the default, if neither theMF_BYCOMMAND nor MF_BYPOSITION flag is specified.
MF_BYPOSITION uIDCheckItem参数为从0起始的菜单序号
Indicates that the uIDCheckItem parameter gives the zero-basedrelative position of the menu item.
MF_CHECKED 设置菜单项为选择
Sets the check-mark attribute to the selected state.
MF_UNCHECKED 设置菜单项为没有选择
Sets the check-mark attribute to the clear state
2、关于checkmenuradioitem
The CheckMenuRadioItem function checks a specified menu item andmakes it a radio item. At the same time, the function clears allother menu items in the associated group and clears the radio-itemtype flag for those items
用于radio类型的菜单项选择,选中某一项并清除其它项的选择标志,其它项由 UINT idFirst,
UINT idLast两个参数指定。
Syntax
BOOL CheckMenuRadioItem(

HMENU hmenu,
UINT idFirst,
UINT idLast,
UINT idCheck,
UINT uFlags
);
uFlags 参数:If this parameter is MF_BYCOMMAND, the other parametersspecify menu item identifiers(命令ID). If it is MF_BYPOSITION, theother parameters specify the menu item positions(菜单序号)
(二)
.elseif eax==wm_bycommand
mov eax,wparam
movzx eax,ax
..if eax==IDM_HELP||IDM_ABOUT
invoke _displaymenuitem,wparam
.else
invoke defwindowproc,hwnd,umsg,wparam,lparam;交给默认窗口处理程序处理
ret
.endif
.elseif

(三)右键弹出菜单
.elseif eax==WM_RBUTTONDOWN
invoke getcursorpos,addr @stpos;
invoketrackpopupmenu,hsubmenu,TPM_LEFT,@stops.x,@stops.y,null,hwnd,null;
.elseif eax==wm_close
call _quit
.else
invoke defwindowproc,hwnd,umsg,wparam,lparam;交给默认窗口处理程序处理
ret
.endif

重点与难点:
1、 the GetCursorPos function retrieves the cursor's position, inscreen coordinates.
返回当前光标位置,即鼠标位置是是相对于屏幕的座标。
Syntax
BOOL GetCursorPos(

LPPOINT lpPoint
);
2、 trackpopupmenu
The TrackPopupMenu function displays a shortcut menu at thespecified location and tracks the selection of items on the menu.The shortcut menu can appear anywhere on the screen.
在屏幕的任何指定位置显示一个弹出菜单。
Syntax
BOOL TrackPopupMenu(

HMENU hMenu,
UINT uFlags,
int x,
int y,
int nReserved,
HWND hWnd,
HWND prcRect
);
参数说明
(1) uFlags
TPM_CENTERALIGN
If this flag is set, the function centers the shortcut menuhorizontally relative to the coordinate specified by the xparameter.
TPM_LEFTALIGN
If this flag is set, the function positions the shortcut menu sothat its left side is aligned with the coordinate specified by thex parameter.
TPM_RIGHTALIGN
Positions the shortcut menu so that its right side is aligned withthe coordinate specified by the x parameter.
Use one of the following flags to specify how the functionpositions the shortcut menu vertically.
TPM_BOTTOMALIGN
If this flag is set, the function positions the shortcut menu sothat its bottom side is aligned with the coordinate specified bythe y parameter.
TPM_TOPALIGN
If this flag is set, the function positions the shortcut menu sothat its top side is aligned with the coordinate specified by the yparameter.
TPM_VCENTERALIGN
If this flag is set, the function centers the shortcut menuvertically relative to the coordinate specified by the yparameter.
Use the following flags to determine the user selection withouthaving to set up a parent window for the menu.
以上为菜单显示时的对齐方式相对于x、y参数
TPM_NONOTIFY用户选择菜单项时不发送通知消息
If this flag is set, the function does not send notificationmessages when the user clicks on a menu item.
TPM_RETURNCMD 返回命令ID
If this flag is set, the function returns the menu item identifierof the user's selection in the return value.
Use one of the following flags to specify which mouse button theshortcut menu tracks.
TPM_LEFTBUTTON 左键键选择菜单
If this flag is set, the user can select menu items with only theleft mouse button.
TPM_RIGHTBUTTON 右键选择菜单
If this flag is set, the user can select menu items with both theleft and right mouse buttons.
Windows 98/Me, Windows 2000/XP:Use any reasonable combination ofthe following flags to modify the animation of a menu. For example,by selecting a horizontal and a vertical flag you can achievediagonal animation. 可以用下列参数来展示菜单显示的动态效果
TPM_HORNEGANIMATION
Animates the menu from right to left.
TPM_HORPOSANIMATION
Animates the menu from left to right.
TPM_NOANIMATION
Displays menu without animation.
TPM_VERNEGANIMATION
Animates the menu from bottom to top.
TPM_VERPOSANIMATION
Animates the menu from top to bottom
(2) nReserved
Reserved; must be zero. 必须为null
(3) hWnd 要弹出菜单的句柄
[in] Handle to the window that owns the shortcut menu. This windowreceives all messages from the menu. The window does not receive aWM_COMMAND message from the menu until the function returns. If youspecify TPM_NONOTIFY in the uFlags parameter, the function does notsend messages to the window identified by hWnd. However, you muststill pass a window handle in hWnd. It can be any window handlefrom your application
仅指窗口的弹出菜单类型,在本函数返回后,窗口才会接收到wm_command消息,但POPUP类型的菜单只能在第二层中定义,如文件属于第一层,打开、新建等才属于第二层,所以hdunmrnu参数取得了子菜单句柄,invokegetsubmenu.hnemu,npos
,mov hsubmenu,eax。npos指定要取得的第一层菜单的序号,如第一层有2个菜单项,文件和查看,则文件为0,查看为1。
  _procwinmain endp

_winmaing proc
local @stwndclass:wndclassex
local @stmsg:MSG
local @haccelerator
invoke getmodulehandle,null
mov hinstance,eax
invoke loadmenu,hinstance,IDM_MIAN;加载菜单
mov hmenu,eax
invoke loadaccelerators,hinstance,IDA_MAIN;加载加速键
mov @haccelerator,eax
;注册窗口类和建立显示窗口省略
;...............
;...............
;重点:消息循环
.while true
invoke getmessage,addr @stmsg,null,0,0
.break .if eax==0
invoke translateaccelerator,hwinmain,@haccelerator,addr@stmsg
;关于translateaccelerator
;-----------
The TranslateAccelerator function processes accelerator keys formenu commands. The function translates a WM_KEYDOWN orWM_SYSKEYDOWN message to a WM_COMMAND or WM_SYSCOMMAND message (ifthere is an entry for the key in the specified accelerator table)and then sends the WM_COMMAND or WM_SYSCOMMAND message directly tothe appropriate window procedure. TranslateAccelerator does notreturn until the window procedure has processed the message.
对菜单的快键进行预处理,将WM_KEYDOWN或WM_SYSKEYDOWN消息处理成为WM_COMMAND或WM_SYSCOMMAND,并直接将消息送到窗口过程处理,直到窗口过程处理该消息后才返回.
Syntax

int TranslateAccelerator( HWND hWnd,
HACCEL hAccTable,加速键表的资源句柄
LPMSG lpMsg
);
Parameters

hWnd
[in] Handle to the window whose messages are to betranslated.
hAccTable
[in] Handle to the accelerator table. The accelerator table musthave been loaded by a call to the LoadAccelerators function orcreated by a call to the CreateAcceleratorTable function.
lpMsg
[in] Pointer to an MSG structure that contains message informationretrieved from the calling thread's message queue using theGetMessage or PeekMessage function.


[Return Value]

If the function succeeds, the return value isnonzero.执行成功返回非0

If the function fails, the return value is zero.不成功返回0

;-----------

;如果不属于加速键消息或处理失败,则进行标准的处理方案,否则以下不执行,因为该函数直接把
;消息发送到窗口过程,再进行发送
.if eax==0
invoke translatemessage,addr @stmsg
invoke dispatchmessage.addr @stmsg
.endif
.endw
ret
_winmain endp
start:
call _winmain
invoke exitprocess,null
end start

    菜单IDMENU [DISCARDABLE]
BEGIN
菜单项定义
....
END
菜单ID可以是字符型也可以是数值型,数值型ID>10000h,DISCARDABLE表示菜单不再使用的时候暂从内存释放。
MENUITEM 菜单文字,命令ID[,选荐列表]

MENUITEM SEPARATOR分隔条

POPUP 菜单文字[,选项列表]
BEGIN
item-definitions
...
END
顶层菜单
菜单文字:显示在菜单中的字符串。
命令ID:用来分辨不同的菜单项。
选项:用来定义菜单项的各种属性。
CHECKED
GRAYED:灰化
INACTIVE:禁用
MENUBREAK,MENUBARBREAK:此菜单项和以后的菜单项列到新的列中。
.386
.model flat,stdcall
option casemap:none
;---------------
include windows.inc
include user32.inc
include user32.lib
include kernel32.inc
include kernel32.lib
;-----------
ICO_MAIN EQU 1000H;图标
IDM_MAIN EQU 2000H;菜单
IDA_MAIN EQU 2000H;加速键
;-------------下面定义命令ID
IDM_OPEN EQU 4101H
IDM_OPTION EQU 4102H
IDM_EXIT EQU 4103H
......................
IDM_ABOUT EQU 4302H
;--------------------命令ID定义完毕
;------变量
.DATA?
hinstance dd ?
hwinmain dd ?
hmenu dd?
hsubmenu dd ?
;--------常量
.const
szclassname db 'menu example',0
szcaptionmain db 'menu',0
szmenuhellp db '帮助主题(&H)',0
szmenuabout db '关于本程序(&A)....',0
szcaption db '菜单选择',0
szformat db '您选择了菜单命令%08x',0
;---------代码段
.code
_displaymenuitem proc _dwcommandid;显示当前选择的菜单命令
local @szbuffer[256]:byte
pushad

invoke wsprintf,addr @szbuffer,addr szformat,_dwcommandid

;关于wsprintf函数

The wsprintf function formats and stores a series of characters andvalues in a buffer. Any arguments are converted and copied to theoutput buffer according to the corresponding format specificationin the format string. The function appends a terminating nullcharacter to the characters it writes, but the return value doesnot include the terminating null character in its charactercount.
格式化一系列的字符和值在缓冲区内,函数将一个终止的NULL字符插入字符的后面,返回 字符个数,不包括终止符。
语法:
int wsprintf(
LPTSTR lpOut,
LPCTSTR lpFmt,
...
);


lpOut缓冲区
[out] Pointer to a buffer to receive the formatted output. Themaximum size of the buffer is 1024 bytes.
lpFmt
含格式化控制符的字符串
[in] Pointer to a null-terminated string that contains theformat-control specifications. In addition to ordinary ASCIIcharacters, a format specification for each argument appears inthis string. For more information about the format specification,see the Remarks section.

被格化后插入上述字符串中的参数(可以有多个参数,随着上述字符串所含格式化控制符数量而定)
[in] Specifies one or more optional arguments. The number and typeof argument parameters depend on the corresponding format-controlspecifications in the lpFmt parameter.
 格式化规范如下
A format specification has the following form:

%[-][
[img][width][.precision]type
Field Meaning 以上规范中字段的意义
– Pad the output with blanks or zeros to the right to fill thefield width, justifying output to the left. If this field isomitted, the output is padded to the left, justify填充ing it to theright.
-:输出有左边开始的,用空格或0右填充宽度,如果没有这个则左填充。
[/img]
Prefix hexadecimal values with 0x (lowercase) or 0X(uppercase).
将数字用16进制格式0x或oX前缀
0 Pad the output value with zeros to fill the field width. If thisfield is omitted, the output value is padded with blankspaces.
用0填充宽度,没有这个参数用空格填充
width: Copy the specified minimum number of characters to theoutput buffer. The width field is a nonnegative integer. The widthspecification never causes a value to be truncated; if the numberof characters in the output value is greater than the specifiedwidth, or if the width field is not present, all characters of thevalue are printed, subject to the precision specification.
复制指定最小数量的字符到缓冲区中,但是如字符的实际宽度比指定最小数量还大,或这个参数缺省,所有字符被输出。

.precision :For numbers, copy the specified minimum number ofdigits to the output buffer. If the number of digits in theargument is less than the specified precision, the output value ispadded on the left with zeros. The value is not truncated when thenumber of digits exceeds the specified precision. If the specifiedprecision is 0 or omitted entirely, or if the period (.) appearswithout a number following it, the precision is set to 1.

数字的精确度
复制指定最小位数的数字到缓冲区中,但是如数字的实际位数比指定最小数量还小,用0从左边填充,实际位数超过指定,则值不会被删短,如果说指定为0或这个参数缺省,或这个参数指定时,该参数其小数点没有数字,则精确度为1,对于字符来说是复制最大数量的字符。

type Output the corresponding argument as acharacter, a string, or a number. This field can be any of thefollowing values.
type就是指输出到缓冲区的类型。
Value Meaning
c Single character. This value is interpreted as type WCHAR if thecalling application defines Unicode and as type __wchar_totherwise.
C Single character. This value is interpreted as type __wchar_t ifthe calling application defines Unicode and as type WCHARotherwise.
d Signed decimal integer. This value is equivalent to i.带符号的整数
hc, hC Single character. The wsprintf function ignores characterarguments with a numeric value of zero. This value is alwaysinterpreted as type __wchar_t, even when the calling applicationdefines Unicode.
hd Signed short integer argument.
hs, hS String. This value is always interpreted as typeLPSTR(被指定为lpstr类型), even when the calling application definesUnicode.
hu Unsigned short integer.
i Signed decimal integer. This value is equivalent to d.十进制整数
lc, lC Single character. The wsprintf function ignores characterarguments with a numeric value of zero. This value is alwaysinterpreted as type WCHAR, even when the calling application doesnot define Unicode.
ld Long signed integer. This value is equivalent to li.
li Long signed integer. This value is equivalent to ld.
ls, lS String. This value is always interpreted as typeLPWSTR(被指定为LPWSTR类型), even when the calling application does notdefine Unicode. This value is equivalent to ws.
lu Long unsigned integer.长整型无符号整数
lx, lX Long unsigned hexadecimal integer in lowercase oruppercase.
p Windows 2000/XP: Pointer. The address is printed usinghexadecimal. (16进制的指针类型)
s String. This value is interpreted as type LPWSTR when the callingapplication defines Unicode and as type LPSTR otherwise.
S String. This value is interpreted as type LPSTR when the callingapplication defines Unicode and as type LPWSTR otherwise.
u Unsigned integer argument. 无符号整数
x, X Unsigned hexadecimal integer in lowercase or uppercase.无符号16进制


To use buffers larger than 1024 bytes, use_snwprintf当缓冲区超过1024bytes时用_snwprintf
 接着1的程序

invoke messagebox,hwinmain,addr @szbuffer,offsetszcaption,MB_OK
popad;将前面保存的寄存器值复原,PUSHAD指令是把32位的通用寄存器全部入栈,入栈顺序为EAX,ECX,EDX,EX,质量保证执行前的ESP、
EBP、ESI、EDI。出栈顺序相反。
ret
_displaymenuitem endp

;退出
_quit proc
invoke destroywindow,hwinmain
invoke postquitmessagfe,null
ret
_quit endp
;窗口过程
_procwinmain proc uses ebx edi esi hwnd,umsg,wparam,lparam
local @stpos:point
local @hsysmenu

mov eax,umsg

.if eax==WM_CREATE

invokegetsubmenu,hmenu,1;得到第2个子菜单的句柄也就是查看,hmenu为总菜单的句柄。总菜单句柄用getmenu得到
mov hsubmenu,eax

;得到系统菜单并在系统菜单上增加自己的菜单即在系统菜单中的还原,移动等下面增加

invoke getsystemmenu,hwnd,false;

;-------------------------
;关于getsystemmenu访问系统菜单或控制菜单
Syntax

HMENU GetSystemMenu(
HWND hWnd,
BOOL bRevert
);
如果bRevert为true则将窗口菜单重置到缺省标准状态,以前的菜单销毁,返回NULL;为FALSE,则返回系统菜单拷贝的句柄。
这里的菜单项发送wm_syscommand消息。

;----------
mov @hsysmenu,eax
invoke appendmenu,@hsysmenu,MF_SPARATOR,0,NULL;在系统菜单里增加分隔符
invoke appendmenu,@hsysmenu,0,IDM_HELP,offsetszmenuhelp;增加‘帮助主题’菜单项
invoke appendmenu,@hsysmenu,0,IDM_ABOUT,offsetszmenuabout;增加‘关于本程序’菜单项
;----------
;关于appendmenu
;----------msdn
The AppendMenu function appends a new item to the end of thespecified menu bar, drop-down menu, submenu, or shortcut menu. Youcan use this function to specify the content, appearance, andbehavior of the menu item.

Syntax

BOOL AppendMenu(
HMENU hMenu,
UINT uFlags,
UINT_PTR uIDNewItem,
LPCTSTR lpNewItem
);
Parameters

hMenu
[in] Handle to the menu bar, drop-down menu, submenu, or shortcutmenu to be changed.
=================================================
uFlags
[in] Specifies flags to control the appearance and behavior of thenew menu item. This parameter can be a combination of the valueslisted in the following
Value Description
MF_BITMAP Uses a bitmap as the menu item. The lpNewItem parametercontains a handle to the bitmap.
MF_CHECKED Places a check mark next to the menu item. If theapplication provides check-mark bitmaps (see SetMenuItemBitmaps,this flag displays the check-mark bitmap next to the menuitem.
MF_DISABLED Disables the menu item so that it cannot be selected,but the flag does not gray it.
MF_ENABLED Enables the menu item so that it can be selected, andrestores it from its grayed state.
MF_GRAYED Disables the menu item and grays it so that it cannot beselected.
MF_MENUBARBREAK Functions the same as the MF_MENUBREAK flag for amenu bar. For a drop-down menu, submenu, or shortcut menu, the newcolumn is separated from the old column by a vertical line.
MF_MENUBREAK Places the item on a new line (for a menu bar) or in anew column (for a drop-down menu, submenu, or shortcut menu)without separating columns.
MF_OWNERDRAW Specifies that the item is an owner-drawn item. Beforethe menu is displayed for the first time, the window that owns themenu receives a WM_MEASUREITEM message to retrieve the width andheight of the menu item. The WM_DRAWITEM message is then sent tothe window procedure of the owner window whenever the appearance ofthe menu item must be updated.
MF_POPUP Specifies that the menu item opens a drop-down menu orsubmenu. The uIDNewItem parameter specifies a handle to thedrop-down menu or submenu. This flag is used to add a menu name toa menu bar, or a menu item that opens a submenu to a drop-downmenu, submenu, or shortcut menu.
MF_SEPARATOR Draws a horizontal dividing line. This flag is usedonly in a drop-down menu, submenu, or shortcut menu. The linecannot be grayed, disabled, or highlighted. The lpNewItem anduIDNewItem parameters are ignored.
MF_STRING Specifies that the menu item is a text string; thelpNewItem parameter is a pointer to the string.
MF_UNCHECKED Does not place a check mark next to the item(default). If the application supplies check-mark bitmaps (seeSetMenuItemBitmaps), this flag displays the clear bitmap next tothe menu item.


The following groups of flags cannot be used together:


MF_BITMAP, MF_STRING, and MF_OWNERDRAW
MF_CHECKED and MF_UNCHECKED
MF_DISABLED, MF_ENABLED, and MF_GRAYED
MF_MENUBARBREAK and MF_MENUBREAK

===========================
uIDNewItem
[in] Specifies either the identifier of the new menu item or, ifthe uFlags parameter is set to MF_POPUP, a handle to the drop-downmenu or submenu.


lpNewItem
[in] Specifies the content of the new menu item. The interpretationof lpNewItem depends on whether the uFlags parameter includes theMF_BITMAP, MF_OWNERDRAW, or MF_STRING flag, as shown in thefollowing table.


MF_BITMAP
Contains a bitmap handle.
MF_OWNERDRAW
Contains an application-supplied value that can be used to maintainadditional data related to the menu item. The value is in theitemData member of the structure pointed to by the lParam parameterof the WM_MEASUREITEM or WM_DRAWITEM message sent when the menu iscreated or its appearance is updated.
MF_STRING
Contains a pointer to a null-terminated string.
Return Value

If the function succeeds, the return value is nonzero. If thefunction fails, the return value is zero. To get extended errorinformation, call GetLastError.

;------------
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics