写个菜单转换+图标转换练习

menu.asm:

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include        windows.inc
include        gdi32.inc
includelib    gdi32.lib
include        user32.inc
includelib    user32.lib
include        kernel32.inc
includelib    kernel32.lib
include        shell32.inc
includelib    shell32.lib
IDM_MAIN    equ    2000h
IDM_OPEN    equ    2101h
IDM_CLOSE    equ    2102h
IDM_EXIT    equ    2103h
IDM_UNDO    equ    2201h
IDM_COPY    equ    2202h
IDM_CUT        equ    2203h
IDM_BIG        equ    2204h
IDM_MID        equ    2205h
IDM_SMALL    equ    2206h
IDM_CHECK1    equ    2207h
IDM_CHECK2    equ    2208h
IDM_CHECK3    equ    2209h
IDM_CHECK4    equ    2210h
IDM_HELP    equ    2301h
IDM_ABOUT    equ    2302h

IDM_MENU        equ    4000h
IDM_BIGVIEW        equ    4101h
IDM_SMALLVIEW    equ    4102h
IDM_ML            equ    4201h
IDM_BUILD        equ    4202h
IDM_SELECT    equ 5000h

IDI_ICO        equ    6000h
IDI_ICO1    equ    6001h
IDI_ICO2    equ 6002h
IDI_ICO3    equ 6003h
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data
szBuffer    db        254 dup (?)

.data?
hInstance    dd        ?
hWinMain    dd        ?
hMenu        dd        ?
hRightMenu    dd        ?
hMenu2        dd        ?
hIcon        dd        ?
hIcon1        dd        ?
hIcon2        dd        ?
hIcon3        dd        ?

.const
szClassName    db    ’MyClass’,0
szCaption    db    ’My first Window !’,0
szCopy        db    ’复制’,0
szCut        db    ’剪切’,0
szExit        db    ’退出’,0
szCaptionName    db    ’提示’,0
szFormat    db    ’你选中的菜单项的ID是:%08x’,0
szSelect    db    ’选择副菜单’,0
szBuild        db    ’组建’,0

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 窗口过程
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcWinMain    proc    uses ebx edi esi hWnd,uMsg,wParam,lParam
local @stPos:POINT
mov    eax,uMsg
;********************************************************************
.if    eax==WM_CREATE
invoke CreatePopupMenu
mov hRightMenu,eax
invoke AppendMenu,hRightMenu,0,IDM_COPY,addr szCopy
invoke AppendMenu,hRightMenu,0,IDM_CUT,addr szCut
invoke AppendMenu,hRightMenu,MF_SEPARATOR,0,0
invoke AppendMenu,hRightMenu,0,IDM_EXIT,addr szExit
invoke AppendMenu,hRightMenu,MF_SEPARATOR,0,0
invoke AppendMenu,hRightMenu,0,IDM_SELECT,addr szSelect
invoke AppendMenu,hRightMenu,0,IDM_BUILD,addr szBuild
.elseif eax==WM_RBUTTONDOWN
invoke GetCursorPos,addr @stPos
invoke TrackPopupMenu,hRightMenu,TPM_LEFTALIGN,@stPos.x,@stPos.y,0,hWnd,0
;********************************************************************
.elseif    eax ==    WM_CLOSE
invoke    DestroyMenu,hRightMenu
invoke  DestroyMenu,hMenu2
invoke    DestroyWindow,hWinMain
invoke    PostQuitMessage,NULL
;********************************************************************
.elseif eax==WM_COMMAND

mov eax,wParam
.if ax==IDM_EXIT
invoke    DestroyMenu,hRightMenu
invoke  DestroyMenu,hMenu2
invoke DestroyWindow,hWinMain
invoke PostQuitMessage,NULL
ret
.elseif ax>=IDM_BIG && ax<=IDM_SMALL
mov ebx,eax
invoke CheckMenuRadioItem,hMenu,IDM_BIG,IDM_SMALL,eax,MF_BYCOMMAND
.if bx == IDM_BIG
invoke SendMessage,hWnd,WM_SETICON,ICON_SMALL,hIcon1
.elseif bx == IDM_MID
invoke SendMessage,hWnd,WM_SETICON,ICON_SMALL,hIcon2
.else
invoke SendMessage,hWnd,WM_SETICON,ICON_SMALL,hIcon3
.endif
.elseif ax>=IDM_CHECK1 && ax<=IDM_CHECK4
xor ebx,ebx
mov bx,ax
invoke GetMenuState,hMenu,ebx,MF_BYCOMMAND
.if eax & MF_CHECKED
mov eax,MF_UNCHECKED
.else
mov eax,MF_CHECKED
.endif
invoke CheckMenuItem,hMenu,ebx,eax
.elseif ax==IDM_SELECT
xor ebx,ebx
mov bx,ax
invoke GetMenuState,hRightMenu,ebx,MF_BYCOMMAND
.if eax & MF_CHECKED
invoke SetMenu,hWinMain,hMenu
mov eax,MF_UNCHECKED
.else
invoke SetMenu,hWinMain,hMenu2
mov eax,MF_CHECKED
.endif
invoke CheckMenuItem,hRightMenu,ebx,eax
.endif
invoke wsprintf,addr szBuffer,addr szFormat,wParam
invoke MessageBox,hWinMain,addr szBuffer,addr szCaptionName,MB_OK
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
;********************************************************************
xor    eax,eax
ret

_ProcWinMain    endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_WinMain    proc
local    @stWndClass:WNDCLASSEX
local    @stMsg:MSG

invoke    GetModuleHandle,NULL
mov    hInstance,eax
invoke LoadMenu,hInstance,IDM_MAIN
mov hMenu,eax
invoke LoadMenu,hInstance,IDM_MENU
mov hMenu2,eax
invoke LoadIcon,hInstance,IDI_ICO
mov hIcon,eax
invoke LoadIcon,hInstance,IDI_ICO1
mov hIcon1,eax
invoke LoadIcon,hInstance,IDI_ICO2
mov hIcon2,eax
invoke LoadIcon,hInstance,IDI_ICO3
mov hIcon3,eax
invoke    RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
;********************************************************************
; 注册窗口类
;********************************************************************
invoke    LoadCursor,0,IDC_ARROW
mov    @stWndClass.hCursor,eax
push    hInstance
pop    @stWndClass.hInstance
push hIcon
pop @stWndClass.hIcon
mov    @stWndClass.cbSize,sizeof WNDCLASSEX
mov    @stWndClass.style,CS_HREDRAW  or CS_VREDRAW
mov    @stWndClass.lpfnWndProc,offset _ProcWinMain
mov    @stWndClass.hbrBackground,COLOR_WINDOW + 1
mov    @stWndClass.lpszClassName,offset szClassName
invoke    RegisterClassEx,addr @stWndClass
;********************************************************************
; 建立并显示窗口
;********************************************************************
invoke CreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,offset szCaption,
WS_OVERLAPPEDWINDOW,
100,100,600,400,
NULL,hMenu,hInstance,NULL
mov    hWinMain,eax
invoke    ShowWindow,hWinMain,SW_SHOWNORMAL
invoke    UpdateWindow,hWinMain
;********************************************************************
; 消息循环
;********************************************************************
.while    TRUE
invoke    GetMessage,addr @stMsg,NULL,0,0
.break    .if eax    == 0
invoke    TranslateMessage,addr @stMsg
invoke    DispatchMessage,addr @stMsg
.endw
ret

_WinMain    endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
call    _WinMain
invoke    ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end    start

 

 

menu.rc:

#include<resource.h>
#define IDM_MAIN    0×2000
#define IDM_OPEN    0×2101
#define IDM_CLOSE    0×2102
#define IDM_EXIT    0×2103
#define IDM_UNDO    0×2201
#define IDM_COPY    0×2202
#define IDM_CUT        0×2203
#define IDM_BIG        0×2204
#define IDM_MID        0×2205
#define IDM_SMALL    0×2206
#define IDM_CHECK1    0×2207
#define IDM_CHECK2    0×2208
#define IDM_CHECK3    0×2209
#define IDM_CHECK4    0×2210
#define IDM_HELP    0×2301
#define IDM_ABOUT    0×2302

#define IDM_MENU            0×4000
#define IDM_BIGVIEW            0×4101
#define IDM_SMALLVIEW        0×4102
#define IDM_ML                0×4201
#define IDM_BUILD            0×4202
#define IDI_ICO        0×6000
#define IDI_ICO1    0×6001
#define IDI_ICO2    0×6002
#define IDI_ICO3    0×6003
IDI_ICO        ICON DISCARDABLE “main.ico”
IDI_ICO1    ICON DISCARDABLE “1.ico”
IDI_ICO2    ICON DISCARDABLE “2.ico”
IDI_ICO3    ICON DISCARDABLE “3.ico”
IDM_MAIN MENU discardable
{
POPUP “新建”
{
MENUITEM “打开(N)”,IDM_OPEN
MENUITEM “关闭(C)”,IDM_CLOSE
MENUITEM SEPARATOR
MENUITEM “退出(Q)”,IDM_EXIT
}
POPUP “编辑”
{
MENUITEM “撤销”,IDM_UNDO,INACTIVE
MENUITEM “复制”,IDM_COPY,GRAYED
MENUITEM “剪切”,IDM_CUT
MENUITEM “大图标”,IDM_BIG
MENUITEM “中图标”,IDM_MID
MENUITEM “小图标”,IDM_SMALL
MENUITEM “选中1″,IDM_CHECK1
MENUITEM “选中2″,IDM_CHECK2
MENUITEM “选中3″,IDM_CHECK3
MENUITEM “选中4″,IDM_CHECK4
}
POPUP “帮助”,HELP
{
MENUITEM “帮助选项”,IDM_HELP
MENUITEM “关于”,IDM_ABOUT
}
}
IDM_MENU    MENU    DISCARDABLE
{
POPUP    ”视图”
{
MENUITEM “大图”,IDM_BIGVIEW
MENUITEM “小图”,IDM_SMALLVIEW

}
POPUP    ”工程”
{
MENUITEM “编译”,IDM_ML
MENUITEM SEPARATOR
MENUITEM “组建”,IDM_BUILD
}
}