【解剖】每日一记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
大家好今天是清明节放假的最后一天明天我就要去学校了所以这几天不会更新日志了,不过星期天回家我就更新
好,我们开始
一个窗口的创建有一些几个步骤
注册窗口
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
相关文章推荐
- 创建一个窗口的过程
- 面向对象,掌握Windows应用程序的创建过程:一个窗口的诞生
- WindowsAPI_001:创建一个不规则的窗口的方法(用到Region系列API)
- 不使用API,创建一个最简单的窗口
- 学习win32 API开发3-一个窗口创建的经典步骤
- Windows编程——实现创建一个窗口过程
- win32 api 创建窗口的一个简单例子
- 服务进程创建一个带窗口的进程
- 将创建进程的API-posix_spawn封装成一个进程类
- 用SDL创建一个窗口
- Android窗口创建过程
- c#中在DataGrid里创建一个弹出式窗口------可实现源代码
- NeHe_001_创建一个OpenGL窗口
- Duilib 入门教程: 怎么创建一个自定义的窗口
- [原创]java WEB学习笔记40:简单标签概述(背景,使用一个标签,标签库的API,SimpleTag接口,创建一个自定义的标签的步骤 和简单实践)
- win32 api CreateWindow创建窗口控件及设置字体
- Android入门之从输入设备中获取消息——窗口创建过程
- MFC应用程序中处理消息的顺序,创建窗口的过程关闭窗口的顺序(非模态窗口),打开模式对话框的函数调用顺序
- 分析Linux内核创建一个新进程的过程
- 学MFC之前必须会的金典创建窗口程序的过程代码