[32位汇编系列]002 - 创建标准的windows窗口(1)
2009-07-13 23:51
363 查看
上一节
中我展示了一个用
MessageBox显示的
windows对话框,让读者对
32位汇编有一个感性的认识,本节通过创建一个
windows标准窗口,来展示一下相对完整的
windows程序的写法,同时熟悉一下汇编的基本语法。还是依旧,
先看例子,然后再讲述理论。
; FileName: NormalWindow.asm
; Function: Show a standar window
; Author: thinker
; Compile & Link:
; ml /c /coff NormalWindow.asm
; link /subsystem:windows NormalWindow.obj
.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
.data?
hInstance dd ?
hWinMain dd ?
.const
szAppName db 'NormalWindow', 0
szText db 'things
have changed from now', 0
szError db 'Error', 0
szErrorText db 'This program requires NT
!', 0
szErrCreateWnd db 'CreateWindow Error.', 0
.code
_WinProc proc hWnd, uMsg, wParam, lParam
local @stPS:PAINTSTRUCT
local @stRC:RECT
local @hDC
mov eax, uMsg
.if eax == WM_PAINT
invoke BeginPaint,
hWnd, addr @stPS
mov @hDC, eax
invoke SetTextColor,
@hDC, 0ff00h
invoke SetBkColor,
@hDC, 0
invoke GetClientRect,
hWnd, addr @stRC
invoke DrawText, @hDC,
offset szText, -1, addr @stRC, DT_SINGLELINE or DT_VCENTER or DT_CENTER
invoke EndPaint, hWnd,
addr @stPS
.elseif eax == WM_CLOSE
invoke DestroyWindow,
hWinMain
invoke PostQuitMessage,
0
.else
invoke DefWindowProc,
hWnd, uMsg, wParam, lParam
ret
.endif
xor eax, eax
ret
_WinProc endp
_WinMain proc
local @stWC:WNDCLASSEX
local @stMsg:MSG
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke RtlZeroMemory, addr @stWC, sizeof
@stWC
mov @stWC.cbSize, sizeof WNDCLASSEX
mov @stWC.style, CS_HREDRAW or CS_VREDRAW
mov @stWC.lpfnWndProc, offset _WinProc
push hInstance
pop @stWC.hInstance
invoke LoadIcon, NULL, IDI_APPLICATION
mov @stWC.hIcon, eax
mov @stWC.hIconSm, eax
invoke LoadCursor, NULL, IDC_ARROW
mov @stWC.hCursor, eax
invoke GetStockObject, BLACK_BRUSH
mov @stWC.hbrBackground, eax
mov @stWC.lpszClassName, offset szAppName
invoke RegisterClassEx, addr @stWC
.if !eax
invoke MessageBox,
NULL, offset szErrorText, offset szError, MB_OK or MB_ICONERROR
ret
.endif
invoke CreateWindowEx,
/
WS_EX_CLIENTEDGE,
/
offset
szAppName, /
offset
szAppName,
/
WS_OVERLAPPEDWINDOW, /
CW_USEDEFAULT,
/
CW_USEDEFAULT,
/
CW_USEDEFAULT,
/
CW_USEDEFAULT,
/
NULL,
/
NULL,
/
hInstance,
/
NULL
.if !eax
invoke MessageBox,
NULL, offset szErrCreateWnd, offset szError, MB_OK or MB_ICONERROR
ret
.endif
mov hWinMain, eax
invoke ShowWindow, hWinMain, SW_SHOWNORMAL
invoke UpdateWindow, hWinMain
.while TRUE
invoke GetMessage, addr
@stMsg, NULL, 0, 0
.break .if !eax
invoke
TranslateMessage, addr @stMsg
invoke DispatchMessage,
addr @stMsg
.endw
ret
_WinMain endp
start:
call _WinMain
invoke ExitProcess, NULL
end start
程序运行效果如下
(为了不占用太大空间,我把窗口拉小了
):
关于程序的解释说明, 请看下一篇文章
。
中我展示了一个用
MessageBox显示的
windows对话框,让读者对
32位汇编有一个感性的认识,本节通过创建一个
windows标准窗口,来展示一下相对完整的
windows程序的写法,同时熟悉一下汇编的基本语法。还是依旧,
先看例子,然后再讲述理论。
; FileName: NormalWindow.asm
; Function: Show a standar window
; Author: thinker
; Compile & Link:
; ml /c /coff NormalWindow.asm
; link /subsystem:windows NormalWindow.obj
.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
.data?
hInstance dd ?
hWinMain dd ?
.const
szAppName db 'NormalWindow', 0
szText db 'things
have changed from now', 0
szError db 'Error', 0
szErrorText db 'This program requires NT
!', 0
szErrCreateWnd db 'CreateWindow Error.', 0
.code
_WinProc proc hWnd, uMsg, wParam, lParam
local @stPS:PAINTSTRUCT
local @stRC:RECT
local @hDC
mov eax, uMsg
.if eax == WM_PAINT
invoke BeginPaint,
hWnd, addr @stPS
mov @hDC, eax
invoke SetTextColor,
@hDC, 0ff00h
invoke SetBkColor,
@hDC, 0
invoke GetClientRect,
hWnd, addr @stRC
invoke DrawText, @hDC,
offset szText, -1, addr @stRC, DT_SINGLELINE or DT_VCENTER or DT_CENTER
invoke EndPaint, hWnd,
addr @stPS
.elseif eax == WM_CLOSE
invoke DestroyWindow,
hWinMain
invoke PostQuitMessage,
0
.else
invoke DefWindowProc,
hWnd, uMsg, wParam, lParam
ret
.endif
xor eax, eax
ret
_WinProc endp
_WinMain proc
local @stWC:WNDCLASSEX
local @stMsg:MSG
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke RtlZeroMemory, addr @stWC, sizeof
@stWC
mov @stWC.cbSize, sizeof WNDCLASSEX
mov @stWC.style, CS_HREDRAW or CS_VREDRAW
mov @stWC.lpfnWndProc, offset _WinProc
push hInstance
pop @stWC.hInstance
invoke LoadIcon, NULL, IDI_APPLICATION
mov @stWC.hIcon, eax
mov @stWC.hIconSm, eax
invoke LoadCursor, NULL, IDC_ARROW
mov @stWC.hCursor, eax
invoke GetStockObject, BLACK_BRUSH
mov @stWC.hbrBackground, eax
mov @stWC.lpszClassName, offset szAppName
invoke RegisterClassEx, addr @stWC
.if !eax
invoke MessageBox,
NULL, offset szErrorText, offset szError, MB_OK or MB_ICONERROR
ret
.endif
invoke CreateWindowEx,
/
WS_EX_CLIENTEDGE,
/
offset
szAppName, /
offset
szAppName,
/
WS_OVERLAPPEDWINDOW, /
CW_USEDEFAULT,
/
CW_USEDEFAULT,
/
CW_USEDEFAULT,
/
CW_USEDEFAULT,
/
NULL,
/
NULL,
/
hInstance,
/
NULL
.if !eax
invoke MessageBox,
NULL, offset szErrCreateWnd, offset szError, MB_OK or MB_ICONERROR
ret
.endif
mov hWinMain, eax
invoke ShowWindow, hWinMain, SW_SHOWNORMAL
invoke UpdateWindow, hWinMain
.while TRUE
invoke GetMessage, addr
@stMsg, NULL, 0, 0
.break .if !eax
invoke
TranslateMessage, addr @stMsg
invoke DispatchMessage,
addr @stMsg
.endw
ret
_WinMain endp
start:
call _WinMain
invoke ExitProcess, NULL
end start
程序运行效果如下
(为了不占用太大空间,我把窗口拉小了
):
关于程序的解释说明, 请看下一篇文章
。
相关文章推荐
- [32位汇编系列]002 - 创建标准的windows窗口(2)
- [32位汇编系列]002 - 创建标准的windows窗口(3)
- windows下32位汇编语言学习笔记 第四章 第一个窗口程序 1 (消息的使用和入口代码)
- 微机原理课程设计32位汇编学习之二(创建简单的窗口)
- windows下32位汇编语言学习笔记 第四章 第一个窗口程序 (windows的消息机制)
- Windows窗口程序从创建到关闭产生的消息
- Windows窗口创建
- Windows 8 动手实验系列教程 实验1:创建Windows应用商店应用
- Windows 7的预备知识系列之二:认识Windows 7中的窗口
- OpenGL_002创建窗口
- windows下创建控制台窗口
- 简单的windows窗口创建实例
- 游戏编程之DirectX的修炼:二(创建属于自己的windows窗口程序:下)
- Qt在Windows下如何创建无CMD窗口控制台程序
- 记录我的Windows编程(一)创建窗口
- Windows环境下32位汇编程序设计C版code--第五章(二)
- Windows环境下32位汇编程序设计C版code--第五章(三)
- 谈谈windows窗口注册和创建
- Delphi使用Windows API 创建最基本的窗口程序Hello,Windows演示
- 【Windows编程】系列第二篇:Windows SDK创建基本控件