我把网上收集的 创建窗口的 方法 给大家看看吧 其实都是 调用 api 实现的
1 汇编
;-----------------------------------------------
;用汇编语言创建一个简单的窗口
;学过C++ WIN32编程的一定一看就懂呵呵
;程序:Iczelion
;翻译:Lxx
;校对:罗云彬
;注释:Conan
;-----------------------------------------------
.386 ;使用386指令集
.model flat,stdcall ;使用flat模式管理内存(不分段),并用C方式传递参数
option casemap:none ;使用区分大小写模式
;以上3行为必须的
;-----------------------------------------------
;包含文件
;必须包含 window.inc 文件,因为其中包含大量要用到的常量和结构的定义
;调用user32.dll和kernel32.dll内的函数,所以包含以下的库文件
include \masm32\include\windows.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
;-----------------------------------------------
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD ;WinMain 的原型声明,因为稍后要用到该函数,故必须先声明
;-----------------------------------------------
.DATA ;定义初始化过的变量
ClassName db "SimpleWinClass",0 ;窗口类名
AppName db "Our First Window",0 ;应用程序名
;-----------------------------------------------
;-----------------------------------------------
.DATA? ;定义未经初始化过的变量
hInstance HINSTANCE ? ;应用程序的句柄
CommandLine LPSTR ? ;命令行参数
;-----------------------------------------------
;-----------------------------------------------
.CODE ;代码分段开始
start:
invoke GetModuleHandle, NULL ;调用 GetModuleHandle 函数查找当前应用程序的句柄
mov hInstance,eax ;WIN32 中函数的函数的返回值是通过 eax 寄存器来传递的。
;其他的值可以通过传递进来的参数地址进行返回
invoke GetCommandLine ;获得命令行参数,
mov CommandLine,eax ;如果你的应用程序不需要命令行参数则不必写这两句
invoke WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
;调用主函数WinMain
invoke ExitProcess, eax ;在主函数返回时,把返回值放到eax中,
;然后在应用程序结束时通过ExitProcess函数把该返回值传递给Windows
;-----------------------------------------------
;以下是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+1
mov wc.lpszMenuName,NULL
mov wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,NULL,IDI_APPLICATION
mov wc.hIcon,eax
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,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
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) ;如果获得了WM_QUIT消息则退出消息循环
;(程序接受到WM_QUIT消息时会返回0-----见后面消息回调函数)
invoke TranslateMessage, ADDR msg ;TranslateMessage函数是一个是实用函数
;它从键盘接受原始按键消息,然后解释成WM_CHAR
;在把WM_CHAR放入消息队列
;由于经过解释后的消息中含有按键的ASCII码
;这比原始的扫描码好理解得多
invoke DispatchMessage, ADDR msg ;把消息发送给负责该窗口过程的函数
.ENDW ;消息循环结束
mov eax, msg.wParam ;将返回值送到EAX寄存器(符合win32一贯标准)
ret ;返回返回值
WinMain endp
;-----------------------------------------------
;WinMain函数结束
;-----------------------------------------------
;-----------------------------------------------
;以下是WndProc函数(消息回调函数)
;-----------------------------------------------
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
.IF uMsg==WM_DESTROY ;如果接受到了WM_DESTROY消息(关闭窗口)
invoke PostQuitMessage,NULL ;则退出程序
.ELSE ;其他消息
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
;如果是其他消息则调用默认消息处理函数
;在这里可以添加自定义消息处理函数
ret ;返回消息处理结果
.ENDIF
xor eax,eax ;将EAX寄存器清零
ret ;返回0
WndProc endp
;-----------------------------------------------
;消息回调函数结束
;-----------------------------------------------
end start
;-----------------------------------------------
;程序结束
;-----------------------------------------------
2 VB
Option Explicit
' Window Functions
Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function LoadResImage Lib "user32" Alias "LoadImageA" (ByVal hinst As Long, ByVal lpsz As Long, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
' Message
Public Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Public Declare Function TranslateMessage Lib "user32" (lpMsg As MSG) As Long
Public Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As MSG) As Long
Public Declare Function RegisterClassEx Lib "user32" Alias "RegisterClassExA" (pcWndClassEx As WNDCLASSEX) As Integer
Public Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
Public Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
' Class Reg And Del
Public Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance As Long) As Long
'---------------------------------------------------------
'CreateWindowEx And Dialog Window Style
'--------------------------------------------------------
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
Public Const WS_OVERLAPPED = &H0&
Public Const WS_THICKFRAME = &H40000
Public Const WS_CHILD = &H40000000
Public Const WS_VISIBLE = &H10000000
Public Const WS_TABSTOP = &H10000
Public Const WS_DISABLED = &H8000000
Public Const WS_SYSMENU = &H80000
Public Const WS_POPUP = &H80000000
Public Const WS_GROUP = &H20000
Public Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
' Window Message
Public Const WM_TIMER = &H113
Public Const WM_USER = &H400
Public Const WM_NOTIFY = &H4E
Public Const WM_MOUSEMOVE = &H200
Public Const WM_RBUTTONUP = &H205
Public Const WM_COMMAND = &H111
Public Const WM_ENABLE = &HA
Public Const WM_INITDIALOG = &H110
Public Const WM_PAINT = &HF
Public Const WM_CLOSE = &H10
Public Const WM_CREATE = &H1
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_SIZE = &H5
Public Const WM_MEASUREITEM = &H2C
Public Const WM_DRAWITEM = &H2B
Public Const WM_INITMENUPOPUP = &H117
Public Const WM_DESTROY = &H2
' WNDCLASSEX
Public Const CS_HREDRAW = &H2
Public Const CS_VREDRAW = &H1
Public Const CS_DBLCLKS = &H8
' Window Color
Public Const COLOR_WINDOW = 5
' DefSystem Cursor
Public Const IDC_HAND = 32649&
' ShowWindow
Public Const SW_NORMAL = 1
Public Const IMAGE_ICON = 1
' DefSystem Cursor
Public Const IDC_ARROW = 32512&
' Reg Window
Public Type WNDCLASSEX
cbSize As Long
Style As Long
lpfnWndProc As Long
cbClsExtra As Long
cbWndExtra As Long
hInstance As Long
hIcon As Long
hCursor As Long
hbrBackground As Long
lpszMenuName As String
lpszClassName As String
hIconSm As Long
End Type
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
Times As Long
pt As POINTAPI
End Type
Option Explicit
'========================================================================================
' Form 1.05
' 代码编号:000001
' 版权所有(C) 2001-2002 江建及其两位女友
'========================================================================================
Dim lpwcx As WNDCLASSEX
Dim RegClass As Long
Public Function RegWinClass(lpClassName As String)
'注册窗口类
With lpwcx
.cbSize = Len(lpwcx)
.Style = CS_HREDRAW Or CS_VREDRAW Or CS_DBLCLKS
.lpszClassName = lpClassName
.hInstance = App.hInstance
.cbClsExtra = 0
.cbWndExtra = 0
.hCursor = LoadCursor(0, IDC_ARROW)
.lpfnWndProc = FnPtrToLong(AddressOf MainWinProc)
.lpszMenuName = 0
.hbrBackground = COLOR_WINDOW
End With
Call RegisterClassEx(lpwcx)
End Function
Private Function MainWinProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case WM_DESTROY
Call PostQuitMessage(0)
End Select
MainWinProc = DefWindowProc(hwnd, uMsg, wParam, lParam)
End Function
Public Function FnPtrToLong(ByVal lngFnPtr As Long) As Long
FnPtrToLong = lngFnPtr
End Function
Public Function CreateMainForm(title As String, nWidth As Long, nHeight As Long) As Long
Dim hWndMain As Long
Dim lpMsg As MSG
Call RegWinClass("Form")
hWndMain = CreateWindowEx(0, "Form", "Windows", WS_OVERLAPPEDWINDOW, 0, 0, nWidth, nHeight, 0, 0, App.hInstance, ByVal 0&)
If hWndMain <> 0 Then
ShowWindow hWndMain, SW_NORMAL
Do While GetMessage(lpMsg, 0, 0, 0)
TranslateMessage lpMsg
DispatchMessage lpMsg
Loop
End If
UnregisterClass "Form", App.hInstance
End Function
Public Sub Main()
CreateMainForm "Windows", 500, 388
End Sub
3 delphi
使用Delphi,SDK编写Windows简单程序 dyroro(原作)
program Project2;
uses
Windows,
Messages;
var
Msg:TMsg;
wc:TWndClass; //RegisterClass()所需要的参数
hWnd:THandle; //主窗体的句柄
const
ClassName='MainWClass';
OutText='Hello ';
Function MainWndProc(Handle:THandle;MsgID:UINT;wParam,lParam:Integer):LRESULT;stdcall;
var
pDC:HDC;
lpRect:TRECT;
begin
Result:=1;
case MsgID of
WM_CLOSE:
begin //关闭窗体所产生的消息
if MessageBox(Handle,'要关闭这个程序吗?','程序示例',MB_ICONQUESTION or MB_YESNO)=IDYES then
DestroyWindow(hWnd)
else
Result:=0;
Exit;
end;
WM_DESTROY: //DestroyWindow()所产生的消息
begin
PostQuitMessage(0);
end;
WM_PAINT:
begin
pDC:=GetDC(hWnd);
GetClientRect(hWnd,lpRect);
DrawText(pDC,OutText,5,lpRect,DT_SINGLELINE or DT_CENTER or DT_VCENTER);
//TextOut(pDC,10,10,OutText,5);
ReleaseDC(hWnd,pDC);
end;
end;
//剩下的消息交给Windows预设的处理函数就可以了,比如画窗体的WM_NCPAINT消息等等
Result:=DefWindowProc(Handle,MsgID,wParam,lParam);
end;
//初始化,注册窗口类
Function InitApplication(hInstance:THANDLE):Boolean;
begin
//首先使用RegisterClass()注册窗体类,这可不是Delphi数据类型中的类哦
wc.style:=CS_HREDRAW OR CS_VREDRAW;
wc.lpfnWndProc:=@MainWndProc; //消息处理函数的地址
wc.hInstance:=hInstance; //程序的句柄,同时也是基地址
wc.hIcon:=LoadIcon(0,PChar(IDI_APPLICATION));
wc.hCursor:=LoadCursor(0,IDC_ARROW); //图标
wc.hbrBackground:=GetStockObject(WHITE_BRUSH); //背景画刷
wc.lpszClassName:=ClassName; //前面定义的常量
Result:=Boolean(RegisterClass(wc));//注册这个窗口类
end;
//InitInstance 生成窗口
Function InitInstance(hInstance:THANDLE;nCmdShow:Integer):Boolean;
begin
hWnd:=CreateWindowEx(0,
ClassName, //刚才注册的类的名字
'示例', //窗体的标题
WS_OVERLAPPEDWINDOW, //窗体的类型,有标题栏、系统菜单、最大化最小化菜单,以及拉伸边框
Integer(CW_USEDEFAULT),
Integer(CW_USEDEFAULT),
Integer(CW_USEDEFAULT),
Integer(CW_USEDEFAULT),
0,
0,
hInstance,
nil
);
if hWnd=0 then
begin
Result:=false;
Exit;
end;
ShowWindow(hWnd,CmdShow);
UpdateWindow(hWnd);
Result:=true;
end;
//主程序开始
begin
if Not InitApplication(hInstance) then Halt(0); //初始化注册窗口类
if Not InitInstance(hInstance,CmdShow) then Halt(0); //产生窗口
while GetMessage(Msg,0,0,0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg); //该API将消息分派到相应的窗体消息处理函数
end;
ExitCode:=Msg.wParam;
end.
4 vc sdk
#include <windows.h>
//示范程序HelloWin的主文件HelloWin.cpp
/**********************************************
*
* 程序:HelloWin.cpp
* 目的:显示一个简单的窗口
*
**********************************************/
//函数原型
//窗口过程,处理主窗口消息
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
//初始化函数,初始化窗口数据,并注册窗口
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
HWND hwnd ; //主窗口句柄
/***************************************************
*
* 函数:WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
* 用途:调用初始化函数,处理消息循环
*
***************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, //当前实例句柄
HINSTANCE hPrevInstance, //前一个实例句柄
LPSTR lpCmdLine, //命令行字符串
int nCmdShow) //窗口显示方式
{
// TODO: Place code here.
MSG msg;
//初始化窗口数据,并注册窗口类
if (!InitApplication(hInstance))
return (FALSE);
//创建和显示窗口,对应用程序进行必要的初始化
if (!InitInstance(hInstance,nCmdShow))
return (FALSE);
//进入消息循环;从应用程序消息队列中检取消息
//当检取的消息是一条WM_QUIT消息时,就退出消息循环
while (GetMessage (&msg, NULL, 0, 0))
{
//把虚拟键消息翻译为字符消息
TranslateMessage (&msg);
//把消息分配到相应的窗口中去
DispatchMessage (&msg);
}
return msg.wParam ;
}
/***************************************************
*
* 函数:InitApplication(HINSTANCE)
* 用途:初始化窗口数据函数,并注册窗口类
*
***************************************************/
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wndclass;
//填写窗口类结构,使得其参数描述主窗口各方面的属性
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC) WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_WINLOGO);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "HelloWin";
//对窗口进行注册
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"), "HelloWin", MB_ICONERROR);
return 0;
}
return (TRUE);
}
/***************************************************
*
* 函数:InitInstance(HINSTANCE,int)
* 用途:创建主窗口
*
***************************************************/
BOOL InitInstance(
HINSTANCE hInstance, //实例句柄
int nCmdShow) //窗口显示方式
{
hwnd = CreateWindow (
"HelloWin", //窗口类名
TEXT ("VC大本营"), //窗口标题
WS_OVERLAPPEDWINDOW, //窗口的风格
CW_USEDEFAULT,
CW_USEDEFAULT, //窗口左上角坐标为缺省值
CW_USEDEFAULT,
CW_USEDEFAULT, //窗口的高和宽为缺省值
NULL, //父窗口的句柄
NULL, //窗口无主菜单
hInstance, //创建此窗口的应用程序的当前句柄
NULL); //不使用该值
//如果创建窗口失败,则返回FALSE
if (!hwnd)
return (FALSE);
//让窗口显示出来;并更新其客户区;最后返回TRUE
ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);
return (TRUE);
}
/***************************************************
*
* 函数:MainWndProc(HWND, UINT, WPARAM, LPARAM)
* 用途:处理主窗口消息。
*
***************************************************/
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rect);
DrawText (hdc, TEXT ("窗口外的世界很精彩,窗口内的天地也很奇妙!--欢迎您来到VC大本营!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint (hwnd, &ps);
break;
case WM_DESTROY: //消息:本窗口正将被销毁
//请求退出窗口和应用程序
PostQuitMessage (0);
break;
default:
//调用默认窗口过程对未处理的消息进行必要的处理
return DefWindowProc (hwnd, message, wParam, lParam);
}
return (0);
}
本文地址:http://com.8s8s.com/it/it37595.htm