WIN32汇编: 3.创建简单的窗口[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 goddragon 的 blog

第三课 创建简单的窗口


在本课中我们将写一个 windows 程序,它会在桌面显示一个标准的窗口。

理论:

windows 程序中,在写图形用户界面时需要调用大量的标准 windows gui 函数。其实这对用户和程序员来说都有好处,对于用户,面对的是同一套标准的窗口,对这些窗口的操作都是一样的,所以使用不同的应用程序时无须重新学习操作。对程序员来说,这些 gui 源代码都是经过了微软的严格测试,随时拿来就可以用的。当然至于具体地写程序对于程序员来说还是有难度的。为了创建基于窗口的应用程序,必须严格遵守规范。作到这一点并不难,只要用模块化或面向对象的编程方法即可。

下面我就列出在桌面显示一个窗口的几个步骤:

  1. 得到您应用程序的句柄(必需);
  2. 得到命令行参数(如果您想从命令行得到参数,可选);
  3. 注册窗口类(必需,除非您使用 windows 预定义的窗口类,如 messagebox 或 dialog box;
  4. 产生窗口(必需);
  5. 在桌面显示窗口(必需,除非您不想立即显示它);
  6. 刷新窗口客户区;
  7. 进入无限的获取窗口消息的循环;
  8. 如果有消息到达,由负责该窗口的窗口回调函数处理;
  9. 如果用户关闭窗口,进行退出处理。

相对于单用户的 dos 下的编程来说,windows 下的程序框架结构是相当复杂的。但是 windows 和 dos 在系统架构上是截然不同的。windows 是一个多任务的操作系统,故系统中同时有多个应用程序彼此协同运行。这就要求 windows 程序员必须严格遵守编程规范,并养成良好的编程风格。

内容:

下面是我们简单的窗口程序的源代码。在进入复杂的细节前,我将提纲挈领地指出几点要点:

  1. 您应当把程序中要用到的所有常量和结构体的声明放到一个头文件中,并且在源程序的开始处包含这个头文件。这么做将会节省您大量的时间,也免得一次又一次的敲键盘。目前,最完善的头文件是 hutch 写的,您可以到 hutch 或我的网站下载。您也可以定义您自己的常量和结构体,但最好把它们放到独立的头文件中
  2. 用 includelib 指令,包含您的程序要引用的库文件,譬如:若您的程序要调用 "messagebox", 您就应当在源文件中加入如下一行: includelib user32.lib 这条语句告诉 masm 您的程序将要用到一些引入库。如果您不止引用一个库,只要简单地加入 includelib 语句,不要担心链接器如何处理这么多的库,只要在链接时用链接开关 /libpath 指明库所在的路径即可。
  3. 在其它地方运用头文件中定义函数原型,常数和结构体时,要严格保持和头文件中的定义一致,包括大小写。在查询函数定义时,这将节约您大量的时间;
  4. 在编译,链接时用makefile文件,免去重复敲键。

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib            ; calls to functions in user32.lib and kernel32.lib
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib

winmain proto :dword,:dword,:dword,:dword

.data                     ; initialized data
classname db "simplewinclass",0        ; the name of our window class
appname db "our first window",0        ; the name of our window

.data?                ; uninitialized data
hinstance hinstance ?        ; instance handle of our program
commandline lpstr ?
.code                ; here begins our code
start:
invoke getmodulehandle, null            ; get the instance handle of our program.
                                                                       ; under win32, hmodule==hinstance mov hinstance,eax
mov hinstance,eax
invoke getcommandline                        ; get the command line. you don't have to call this function if
                                                                       ; your program doesn't process the command line.
mov commandline,eax
invoke winmain, hinstance,null,commandline, sw_showdefault        ; call the main function
invoke exitprocess, eax                           ; quit our program. the exit code is returned in eax from winmain.

winmain proc hinst:hinstance,hprevinst:hinstance,cmdline:lpstr,cmdshow:dword
    local wc:wndclassex                                            ; create local variables on stack
    local msg:msg

本文关键:asm
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top