您的位置:首页 > 其它

【解剖】每日一记API—一个窗口创建的过程

2013-06-07 23:39 435 查看
2013年4月6日16:31:56

大家好今天是清明节放假的最后一天明天我就要去学校了所以这几天不会更新日志了,不过星期天回家我就更新

好,我们开始

一个窗口的创建有一些几个步骤

注册窗口

WNDCLASS wnd;//定义窗口类
wnd.style=CS_HREDRAW | CS_VREDRAW;//窗口类型
wnd.lpfnWndProc=CallProc;//消息处理函数
wnd.cbClsExtra=0;//预留的
wnd.cbWndExtra=0;//预留的
wnd.hCursor=LoadCursor(NULL,IDC_ARROW);//鼠标的形态
wnd.hbrBackground=/*(HBRUSH)*/GetStockObject(WHITE_BRUSH);//设置背景 HBRUSH:转换为画刷的句柄
wnd.lpszMenuName=NULL;
wnd.lpszClassName=TEXT("第一个窗口");
if (!RegisterClass(&wnd))
{

MessageBox(NULL,TEXT("注册失败"),TEXT("WO NO !"),MB_OK|MB_ICONHAND);
return 0;
}

创建窗口

HWND hwin=CreateWindow(TEXT("第一个窗口"),TEXT("jh第一个窗口"),WS_OVERLAPPEDWINDOW,100,110,45,205,NULL,NULL,hInstance,NULL);

消息循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
return msg.lParam;

这就是一个窗口的创建过程我给大家看下完整的代码吧

/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/

#include <windows.h>
#pragma comment(lib, "Winmm.lib")
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{

static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{
aaa: case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rect);
DrawText (hdc, TEXT("hool"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
//MessageBox(NULL,TEXT("自绘中"),TEXT("OK"),0);
EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY:
if(MessageBox(NULL,TEXT("确定关闭?"),TEXT("OK"),MB_YESNO)==IDYES)
{
PostQuitMessage(0);
break;
}else
{
goto aaa;
}
case WM_QUIT:
if(MessageBox(NULL,TEXT("确定关闭?"),TEXT("OK"),MB_YESNO)==IDYES)
{
PostQuitMessage(0);
break;
}else
{
goto aaa;
}
case WM_CLOSE:
if(MessageBox(NULL,TEXT("确定关闭?"),TEXT("OK"),MB_YESNO)==IDYES)
{
PostQuitMessage(0);
break;
}else
{
goto aaa;
}

}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
借鉴一些windows程序设计的一句话

:看我们只要100行代码就创建除了一个可以拖动,可以缩放的可以在窗口中点来点去的窗口了

=========华丽分割线=================================汇编的创建过程,比较长====================

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

.386

.model flat,stdcall

option casemap:none

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

; Include 文件定义

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

includewindows.inc

includegdi32.inc

includelibgdi32.lib

includeuser32.inc

includelibuser32.lib

includekernel32.inc

includelibkernel32.lib

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

; 数据段

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

.data?

hInstancedd?

hWinMaindd?

.const

szClassNamedb'MyClass',0

szCaptionMaindb'My first Window !',0

szTextdb'Win32 Assembly, Simple and powerful !',0

szButtondb'button',0

szButtonTextdb'&OK',0

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

; 代码段

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

.code

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

; 窗口过程

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

_ProcWinMainprocuses ebx edi esi,hWnd,uMsg,wParam,lParam

local@stPs:PAINTSTRUCT

local@stRect:RECT

local@hDc

moveax,uMsg

;********************************************************************

.ifeax ==WM_PAINT

invokeBeginPaint,hWnd,addr @stPs

mov@hDc,eax

invokeGetClientRect,hWnd,addr @stRect

invokeDrawText,@hDc,addr szText,-1,\

addr @stRect,\

DT_SINGLELINE or DT_CENTER or DT_VCENTER

invokeEndPaint,hWnd,addr @stPs

;********************************************************************

; 建立一个按钮

;********************************************************************

.elseifeax ==WM_CREATE

invokeCreateWindowEx,NULL,\

offset szButton,offset szButtonText,\

WS_CHILD or WS_VISIBLE,\

10,10,65,22,\

hWnd,1,hInstance,NULL

;********************************************************************

.elseifeax ==WM_CLOSE

invokeDestroyWindow,hWinMain

invokePostQuitMessage,NULL

;********************************************************************

.else

invokeDefWindowProc,hWnd,uMsg,wParam,lParam

ret

.endif

;********************************************************************

xoreax,eax

ret

_ProcWinMainendp

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

_WinMainproc

local@stWndClass:WNDCLASSEX

local@stMsg:MSG

invokeGetModuleHandle,NULL

movhInstance,eax

invokeRtlZeroMemory,addr @stWndClass,sizeof @stWndClass

;********************************************************************

; 注册窗口类

;********************************************************************

invokeLoadCursor,0,IDC_ARROW

mov@stWndClass.hCursor,eax

pushhInstance

pop@stWndClass.hInstance

mov@stWndClass.cbSize,sizeof WNDCLASSEX

mov@stWndClass.style,CS_HREDRAW or CS_VREDRAW

mov@stWndClass.lpfnWndProc,offset _ProcWinMain

mov@stWndClass.hbrBackground,COLOR_WINDOW + 1

mov@stWndClass.lpszClassName,offset szClassName

invokeRegisterClassEx,addr @stWndClass

;********************************************************************

; 建立并显示窗口

;********************************************************************

invokeCreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,offset szCaptionMain,\

WS_OVERLAPPEDWINDOW,\

100,100,600,400,\

NULL,NULL,hInstance,NULL

movhWinMain,eax

invokeShowWindow,hWinMain,SW_SHOWNORMAL

invokeUpdateWindow,hWinMain

;********************************************************************

; 消息循环

;********************************************************************

.whileTRUE

invokeGetMessage,addr @stMsg,NULL,0,0

.break.if eax== 0

invokeTranslateMessage,addr @stMsg

invokeDispatchMessage,addr @stMsg

.endw

ret

_WinMainendp

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

start:

call_WinMain

invokeExitProcess,NULL

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

endstart
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: