分享
 
 
 

(译)win32asm实例-4

王朝system·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

4.0 - Creating the main frame创建主框架

Now we will create the main frame of our program:

现在我们将创建我们程序的主框架:

Add two prototypes after the includes:

在include后加入两个原型

WinMain PROTO STDCALL :DWORD, :DWORD, :DWORD, :DWORD

WndProc PROTO STDCALL :DWORD, :DWORD, :DWORD, :DWORD

These prototypes will tell masm how many parameters these functions take (4 DWORD's in this case)

这些原型将告诉masm这些函数需要多少个参数(本例中为4个DWORD)

Add these to the .data and .data? sections of your program:

把这些加入到你的程序的.data和.data?中:

.data

AppName db "Mosaic",0

ClassName db "Mosaic32",0

.data?

hInstance dd ?

AppName and ClassName are strings used for the main window, hInstance is a DWORD that will hold the application instance handle.

AppName和ClassName是用于主窗口的字符串。HInstance 是一个将装有程序实例句柄的DWORD。

Start of code:

代码部分的开始:

.code

start:

; Get module handle and save it

invoke GetModuleHandle, NULL

mov hInstance, eax

; Init Common Controls library

invoke InitCommonControls

; Run winmain procedure and exit program

invoke WinMain, hInstance, NULL, NULL, SW_SHOWNORMAL

invoke ExitProcess,eax

GetModuleHandle retreives the module handle, which is stored in hInstance. InitCommonControls is an initialization function for the common control library. This library contains several controls that we will use in our program, and it should be initialized by calling InitCommonControls. After that, WinMain is called (a function we will create soon). WinMain will return when the user quits the program, whereafter ExitProcess is called to quit the process.

GetModuleHandle获得模块句柄,储存在hInstance中。InitCommonControls是一个用于通用控件库的初始化函数。这个库包含了几个我们将要在我们那的程序中使用的控件,而且它们应该由InitCommonControls初始化。在此之后,调用WinMain(一个我们马上要创建的函数)。WinMain在用户退出程序时返回,在那之后调用ExitProcess退出进程。

The WinMain procedure:

WinMain过程:

;===============================================================================

; WinMain

;===============================================================================

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD

LOCAL wc:WNDCLASSEX

LOCAL msg:MSG

LOCAL hwnd:HWND

mov wc.cbSize,SIZEOF WNDCLASSEX

mov wc.style, CS_HREDRAW or CS_VREDRAW

mov wc.lpfnWndProc, OFFSET WndProc

mov wc.cbClsExtra,NULL

mov wc.cbWndExtra,NULL

push hInstance

pop wc.hInstance

mov wc.hbrBackground,COLOR_WINDOW

mov wc.lpszMenuName, NULL

mov wc.lpszClassName,OFFSET ClassName

Here our icons are loaded from the resource file and their handles are stored in wc.hIcon and wc.hIconSm. Windows will use these handles to display the icons in the main window:

这儿,我们的图标从资源文件中装载而且它们的句柄储存于wc.hIcon和wc.hIconSm中。Windows将用这些句柄来把图标显示在主窗口上:

invoke LoadIcon, hInstance, ICON1_BIG

mov wc.hIcon, eax

invoke LoadIcon, hInstance, ICON2_SMALL

mov wc.hIconSm, eax

invoke LoadCursor,NULL,IDC_ARROW

mov wc.hCursor,eax

invoke RegisterClassEx, addr wc

INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName, WS_OVERLAPPEDWINDOW-WS_MAXIMIZEBOX-WS_SIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT,258,350,NULL,NULL, hInst,NULL

mov hwnd,eax

invoke ShowWindow, hwnd, CmdShow

invoke UpdateWindow, hwnd

.WHILE TRUE

invoke GetMessage, ADDR msg,NULL,0,0

.BREAK .IF (!eax)

invoke TranslateMessage, ADDR msg

invoke DispatchMessage, ADDR msg

.ENDW

mov eax,msg.wParam

ret

WinMain endp

The WinMain functions initializes a WNDCLASSEX structure that includes parameters of the windows class, then RegisterClassEx is called to register the window class named 'Mosaic32'. CreateWindowEx is called to create a new window based on the new class, ShowWindow and UpdateWindow will make the window visible. Then the program goes in an infinite loop until the user closes the window. This loop is called the message pump (see my tutorials for more info).

WinMain函数初始化一个包含窗口类参数的WNDCLASSEX结构,然后调用RegisterClassEx来注册一个名为“Mosaic32”的窗口类名。CreateWindowEx被调用来创建一个基于新类的新窗口,ShowWindow和UpdateWindow将使窗口可见。然后程序进入一个无止尽的循环直到用户关闭窗口。这个循环被称为消息泵(Message Pump)(要获得更多信息,看我前面的教程)

The window style is WS_OVERLAPPEDWINDOW without a Maximize-button and without a sizebox (=not resizeable). The window is 258x350 pixels.

窗口样式是没有最大化按钮与Sizebox(=不可调整大小)的WS_OVERLAPPEDWINDOW。窗口是258×350象素。

The window also needs a window procedure that handles all the messages sent to the window. In our program this is the function WndProc, which takes 4 parameters: hWnd contains the window handle, uMsg the message, wParam and lParam the message parameters.

窗口还需要一个窗口过程来处理发给窗口的所有消息。在我们的程序中,这是WndProc函数。它带四个参数:hWnd包含窗口句柄,uMeg消息,wParam和iPararm消息参数。

;===============================================================================

; Window procedure

;===============================================================================

WndProc proc hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD

mov eax, uMsg

.IF eax==WM_CREATE

; yet to do

.ELSEIF eax==WM_DESTROY

invoke PostQuitMessage, NULL

.ELSE

invoke DefWindowProc,hWnd,uMsg,wParam,lParam

ret

.ENDIF

ret

WndProc endp

The window procedure processes the WM_DESTROY message to quit when the user quits.

After you've done all this, your files should look like this: mosaic1.zip

在你做完了一切之后,你的文件应该像这样:mosaic.zip

Now run make.bat to assemble & link your program. You should have no errors (you might get a warning about gdi32 but never mind about that) and you should be able to run the program. It has a small icon in the upper left corner, the big icon is visible in the task list (ALT+TAB) and your mosaic.exe has the big icon as icon.

现在运行make.bat来汇编并链接你的程序。你应该不会得到错误(你可能得到一个关于gdi32的警告,但不要在意)而且你应该能运行程序。它在左上角有个小图标,大图标在任务类标中可见(ALT+TAB)而且你的mosaic.exe把大图标作为图标。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有