ITPub博客

首页 > Linux操作系统 > Linux操作系统 > (译)win32asm实例-4 (转)

(译)win32asm实例-4 (转)

原创 Linux操作系统 作者:worldblog 时间:2007-12-12 10:57:42 0 删除 编辑
(译)win32asm实例-4 (转)[@more@] 

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: PHP?file=mosaic.mosaic1">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把大图标作为图标。

XML:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />Office:office" />ASPectratio="t" v:ext="edit">


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-991802/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    6241
  • 访问量
    2410387