IT保安 保安  2014年01月02日
WIN32汇编,关于图标资源那一章的问题


#include <resource.h>

#define IDM_MAIN 0x0001
#define ICO_MAIN 0x0002
#define IDA_MAIN 0x0001
#define IDM_OPTION 0x0003
#define IDM_EXIT 0x0004
#define IDM_SETFONT 0x0005
#define IDM_SETCOLOR 0x0006
#define IDM_BIG 0x0007
#define IDM_SMALL 0x0008
#define ICO_BIG 0x1000
#define ICO_SMALL 0x1001
#define CUR_2 0x1000
#define IDM_CUR1 0x2203
#define IDM_CUR2 0x2204
#define IDM_HELP 0x2205
#define IDM_ABOUT 0x2206


ICO_BIG ICON "Big.ico"
ICO_SMALL ICON "Small.ico"
CUR_2 CURSOR "2.Cur"

IDM_MAIN menu discardable
BEGIN
popup "文件(&F)..."
BEGIN
menuitem "打开文件(&O)..." IDM_OPTION
menuitem separator
menuitem "退出(&P)..." IDM_EXIT
END
popup "设置(&P)..."
BEGIN
menuitem "设置字体" IDM_SETFONT
menuitem "固定光标" IDM_CUR2
menuitem "动态光标" IDM_CUR1
menuitem separator
menuitem "大图标" IDM_BIG
menuitem "小图标" IDM_SMALL
END
END


;编写第一个自主完成的窗口程序

.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
include gdi32.inc
includelib gdi32.lib

;----------------------------
;开始等值定义,不同于C的#define,用EQU
;-----------------------------
IDM_MAIN equ 0001h
ICO_MAIN equ 0002h
IDA_MAIN equ 0001h
IDM_OPTION equ 0003h
IDM_EXIT equ 0004h
IDM_SETFONT equ 0005h
IDM_SETCOLOR equ 0006h
IDM_BIG equ 0007h
IDM_SMALL equ 0008h
ICO_SMALL equ 1001h
CUR_2 equ 1000h
IDM_CUR1 equ 2203h
IDM_CUR2 equ 2204h
ICO_BIG equ 1000h
IDM_HELP equ 2205h
IDM_ABOUT equ 2206h


.data
szButton db 'Button',0
szButtonText db 'Button',0

.data?
hInstance dd ?
hWinMain dd ?
hMenu dd ?
hSubMenu dd ?
hIcoBig dd ?
hIcoSmall dd ?
hCur1 dd ?
hCur2 dd ?

.const
szClassName db 'Myclass',0
szCaption db '我的窗口',0
szText db '窗口内容',0
szMessageBoxText db '你按下了这个键,此ID命令是:%d',0
szMessageBoxTitle1 db '按键检测',0
szSysmenuAppen db '增加主题...',0
szCursorFile db '1.ani',0
.code
_DisplayMenuItem Proc _dwCommandId
local @szBuffer[256]:byte
pushad
invoke wsprintf,addr @szBuffer,addr szMessageBoxText,_dwCommandId
invoke MessageBox,hWinMain,addr @szBuffer,addr szMessageBoxTitle1,MB_OK
popad
ret
_DisplayMenuItem endp
_Quit Proc
invoke DestroyWindow,hWinMain
invoke PostQuitMessage,NULL
ret
_Quit endp
_WndProc Proc uses ebx esi edi hWnd,uMsg,wParam,lParam ;窗口过程
local @stPs:PAINTSTRUCT ;PAINTSTRUCT包含绘制应用程序所有窗口的信息
local @stRect:RECT ;用来存储成对出现的参数,比如某个点的坐标,高度宽度等
local @hDc
local @stPos:POINT
local @hSysMenu
mov eax,uMsg
.if eax== WM_CREATE
invoke LoadIcon,hInstance,ICO_BIG
mov hIcoBig, eax
invoke LoadIcon,hInstance,ICO_SMALL
mov hIcoSmall,eax
invoke LoadCursorFromFile,addr szCursorFile
mov hCur1,eax
invoke LoadCursor,hInstance,CUR_2
mov hCur2,eax
invoke SendMessage,hWnd,WM_COMMAND,IDM_BIG,NULL
invoke SendMessage,hWnd,WM_COMMAND,IDM_SMALL,NULL

invoke GetSubMenu,hMenu,1
mov hSubMenu,eax;取得第一个菜单的的子菜单的句柄,1代表是第一个,比如菜单
;第一个是文件,那么就是取文件这个菜单的弹出式菜单的句柄
;同理,入股第二个是查看,那么将1改成2,就是取查看这个菜单
;的弹出式菜单的句柄
;-----------------
;在系统菜单中增加菜单项
;-----------------
invoke GetSystemMenu,hWnd,FALSE ;如果参数为TRUE,则将原窗口菜单回复为default,原
;窗口菜单会销毁,用FALSE表示一个窗口菜单的copy,
;并且返回值就是这个copy的句柄,可以进行修改
mov @hSysMenu,eax
invoke AppendMenu,@hSysMenu,MF_SEPARATOR,0,NULL
invoke AppendMenu,@hSysMenu,0,IDM_HELP,addr szSysmenuAppen
;----------------------
;处理菜单和加速键信息,wm_command信息
;--------------------------
.elseif eax==WM_COMMAND
invoke _DisplayMenuItem,wParam
mov eax,wParam
movzx eax,ax
.if eax==IDM_EXIT
call _Quit
;.elseif eax>=IDM_TOOLBAR && eax<=IDM_STATUSBAR
; mov ebx, eax
; invoke GetMenuState,hMenu,ebx,MF_CHECKED
; .if eax==MF_CHECKED
; mov eax, MF_UNCHECKED
; .else
; mov eax,MF_CHECKED
; .endif
; invoke CheckMenuItem,hMenu,ebx,eax
; .elseif eax>=IDM_BIG && eax<=IDM_DETAIL
; invoke CheckMenuRadioItem,hMenu,IDM_BIG,IDM_DETAIL,eax,MF_BYCOMMAND
.elseif eax==IDM_BIG
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,hIcoBig
invoke CheckMenuRadioItem,hMenu,IDM_BIG,IDM_SMALL,\
IDM_BIG,MF_BYCOMMAND
.elseif eax==IDM_SMALL
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,hIcoSmall
invoke CheckMenuRadioItem,hMenu,IDM_BIG,IDM_SMALL,\
IDM_SMALL,MF_BYCOMMAND
.elseif eax==IDM_CUR1
invoke SetClassLong,hWnd,GCL_HCURSOR,hCur1
invoke CheckMenuRadioItem,hMenu,IDM_CUR1,IDM_CUR2,IDM_CUR1,MF_BYCOMMAND
.elseif eax==IDM_CUR2
invoke SetClassLong,hWnd,GCL_HCURSOR,hCur2
invoke CheckMenuRadioItem,hMenu,IDM_CUR1,IDM_CUR2,IDM_CUR2,MF_BYCOMMAND


.endif
;--------------------
;处理系统菜单
;-------------------
.elseif eax==WM_SYSCOMMAND
;invoke _DisplayMenuItem,wParam
mov eax,wParam
movzx eax,ax
.if eax==IDM_HELP || eax==IDM_ABOUT
invoke _DisplayMenuItem,wParam
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
;--------------------------
;处理右键弹出菜单问题
;----------------------------
.elseif eax==WM_RBUTTONDOWN
invoke GetCursorPos,addr @stPos
invoke TrackPopupMenu,hSubMenu,TPM_LEFTALIGN,\
@stPos.x,@stPos.y,NULL,hWnd,NULL
.elseif eax==WM_CLOSE
call _Quit
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret

_WndProc endp


_WinMain Proc
local @stWndClass:WNDCLASSEX
local @stMsg:MSG
local @hAccelerator
invoke GetModuleHandle,NULL
mov hInstance,eax ;得到模块句柄,放入hInstance
invoke LoadMenu,hInstance,IDM_MAIN;成功则返回菜单资源的句柄
mov hMenu,eax
invoke LoadAccelerators,hInstance,IDA_MAIN
mov @hAccelerator,eax
invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
invoke LoadCursor,0,IDC_ARROW
mov @stWndClass.hCursor,eax
invoke LoadIcon,hInstance,ICO_MAIN
mov @stWndClass.hIcon,eax
mov @stWndClass.hIconSm,eax
push hInstance
pop @stWndClass.hInstance
mov @stWndClass.cbSize,sizeof @stWndClass
mov @stWndClass.lpfnWndProc,offset _WndProc
mov @stWndClass.lpszClassName,offset szClassName
mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW
mov @stWndClass. hbrBackground,COLOR_WINDOW+1 ;完成WndClassEx设置
;开始注册窗口
invoke RegisterClassEx,addr @stWndClass
;开始创立窗口
invoke CreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,\
offset szCaption,WS_OVERLAPPEDWINDOW,\
100,100,200,300,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 TranslateAccelerator,hWinMain,@hAccelerator,addr @stMsg
.if eax==0
invoke TranslateMessage,addr @stMsg
invoke DispatchMessage,addr @stMsg
.endif
.endw
ret
_WinMain endp
start:
call _WinMain
invoke ExitProcess,NULL
end start






这个是源文件,其他文件不好上传,就不传了,有2个问题,期待大神解答一下:
1.双击生成的exe文件,首先弹出2个对话框,如图所示,然后再弹出窗口,我闹不明白这对话框先于窗口之前弹出,是哪几段代码出现了问题从而导致的!
2.在主程序winmain里面:invoke LoadIcon,hInstance,ICO_MAIN,这一句代码,编译器怎么就知道ICO_MAIN代表了哪个呢?asm文件里它就出现过一次,在rc文件里,也不过是个等值定义,它怎么知道要加载图标给程序呢?
...全文
163 点赞 收藏 1
写回复
1 条回复

还没有回复,快来抢沙发~

发动态
发帖子
汇编语言
创建于2007-08-27

1.9w+

社区成员

4.1w+

社区内容

汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区公告
暂无公告