一個用delphi寫的整合彙編與api的簡單的視窗程式
program Project1;
{ Types and Structures Definition }
type
WNDCLASSEX = packed record
cbSize: LongWord;
style: LongWord;
lpfnWndProc: Pointer;
cbClsExtra: Integer;
cbWndExtra: Integer;
hInstance: LongWord;
hIcon: LongWord;
hCursor: LongWord;
hbrBackground: LongWord;
lpszMenuName: PAnsiChar;
lpszClassName: PAnsiChar;
hIconSm: LongWord;
end;
POINT = packed record
X: Longint;
Y: Longint;
end;
MSG = packed record
hwnd: LongWord;
message: LongWord;
wParam: Longint;
lParam: Longint;
time: LongWord;
pt: POINT;
end;
{ Application Specific Variables and constants }
const
szBuf = 255; // Size of buffer used to handle strings
WndClsSize = SizeOf(WNDCLASSEX); // Size of WNDCLASSEX Structure
wnd_class: PChar = 'TMainWindow'; // Main window class name.
wnd_title: PChar = 'Closer to the Metal'; // Main window caption.
var
wcx : WNDCLASSEX; // Main window structure
msgbuf : MSG; // Message structure
hwnd : LongWord; // A window handle
{ Windows Specific Constants }
const
{ Class styles }
CS_VREDRAW = LongWord(1);
CS_HREDRAW = LongWord(2);
CS_GLOBALCLASS = $4000;
{ Color Types }
COLOR_WINDOW = 5;
{ Window Styles }
WS_OVERLAPPED = 0;
WS_CAPTION = $C00000; { WS_BORDER or WS_DLGFRAME }
WS_SYSMENU = $80000;
WS_THICKFRAME = $40000;
WS_MINIMIZEBOX = $20000;
WS_MAXIMIZEBOX = $10000;
WS_VISIBLE = $10000000;
{ Common Window Styles }
WS_OVERLAPPEDWINDOW = (WS_VISIBLE or WS_OVERLAPPED or WS_CAPTION
or WS_SYSMENU or WS_THICKFRAME or WS_MINIMIZEBOX or WS_MAXIMIZEBOX);
{ Messages }
WM_DESTROY = $0002;
{ Windows API's }
function GetModuleHandle(lpModuleName: PChar): HMODULE;
stdcall; external 'kernel32.dll' name 'GetModuleHandleA';
procedure ExitProcess(uExitCode: LongWord);
stdcall; external 'kernel32.dll' name 'ExitProcess';
function DefWindowProc(hWnd: LongWord; Msg: LongWord;
wParam: Longint; lParam: Longint): Longint;
stdcall; external 'user32.dll' name 'DefWindowProcA';
function RegisterClassEx(const WndClass: WNDCLASSEX): Word;
stdcall; external 'user32.dll' name 'RegisterClassExA';
function CreateWindowEx(dwExStyle: LongWord; lpClassName: PChar;
lpWindowName: PChar; dwStyle: LongWord;
X, Y, nWidth, nHeight: Integer; hWndParent: LongWord;
hMenu: LongWord; hInstance: LongWord; lpParam: Pointer): LongWord;
stdcall; external 'user32.dll' name 'CreateWindowExA';
function GetMessage(var lpMsg: MSG; hWnd: LongWord;
wMsgFilterMin, wMsgFilterMax: LongWord): LongBool;
stdcall; external 'user32.dll' name 'GetMessageA';
function DispatchMessage(const lpMsg: MSG): Longint;
stdcall; external 'user32.dll' name 'DispatchMessageA';
procedure PostQuitMessage(nExitCode: Integer);
stdcall; external 'user32.dll' name 'PostQuitMessage';
{ Windows Procedure }
function WindowProc(hWnd, Msg: LongWord; wParam, lParam: Longint): Longint; stdcall;
asm
// The inline assembler will take care of setting the stack,
// preserving the registers and returning.
mov EAX, [Msg]
// WM_DESTROY:
cmp EAX, WM_DESTROY
je @@m_destroy
// All Other Messages:
jmp @@defwndproc
@@m_destroy:
push 0
call PostQuitMessage // Quit.
jmp @@return
@@defwndproc:
push [lParam]
push [wParam]
push [Msg]
push [hWnd]
call DefWindowProc
jmp @@finish
@@return:
xor eax,eax
@@finish:
end;
{ Main Program Block }
asm
// GetModuleHandle with a NULL pointer gives us the instance handle
// of the EXE file. This is the module that will "own" the window class.
push 0
call GetModuleHandle
// Define our window properties:
mov [wcx.cbSize], WndClsSize;
mov [wcx.style], CS_VREDRAW or CS_HREDRAW or CS_GLOBALCLASS
mov [wcx.lpfnWndProc], offset WindowProc
mov [wcx.cbClsExtra], 0
mov [wcx.cbWndExtra], 0
mov eax, hInstance
mov [wcx.hInstance], EAX
mov [wcx.hIcon], 0
mov [wcx.hCursor], 0
mov [wcx.hbrBackground], COLOR_WINDOW + 1
mov dword ptr [wcx.lpszMenuName], 0
mov dword ptr [wcx.lpszClassName], offset wnd_class
mov [wcx.hIconSm], 0
mov EAX, wnd_class
mov [wcx.lpszClassName], EAX
// Register window class:
push offset wcx
call RegisterClassEx
// Create window:
push 0 // lpParam
push [wcx.hInstance] // hInstance
push 0 // hMenu
push 0 // hWndParent
push 200 // nHeight
push 200 // nWidth
push 100 // y (top)
push 100 // x (left)
push WS_OVERLAPPEDWINDOW // dwStyle
mov EAX, wnd_title // lpWindowName
push EAX
mov EAX, wnd_class // lpClassName
push EAX
push 0 // dwExStyle
call CreateWindowEx
mov hwnd, EAX
// Message Loop/Pump:
@@msg_loop:
push 0 // wMsgFileterMax
push 0 // wMsgFilterMin
push 0 // hWnd (0 = all windows)
push offset msgbuf // lpMsg
call GetMessage
cmp eax, 0 // Returns 0 (zero) if WM_QUIT
jz @@end_loop
push offset msgbuf
call DispatchMessage
jmp @@msg_loop
@@end_loop:
// Terminating the program:
push 0 // Error return code.
call ExitProcess
end.
{ Types and Structures Definition }
type
WNDCLASSEX = packed record
cbSize: LongWord;
style: LongWord;
lpfnWndProc: Pointer;
cbClsExtra: Integer;
cbWndExtra: Integer;
hInstance: LongWord;
hIcon: LongWord;
hCursor: LongWord;
hbrBackground: LongWord;
lpszMenuName: PAnsiChar;
lpszClassName: PAnsiChar;
hIconSm: LongWord;
end;
POINT = packed record
X: Longint;
Y: Longint;
end;
MSG = packed record
hwnd: LongWord;
message: LongWord;
wParam: Longint;
lParam: Longint;
time: LongWord;
pt: POINT;
end;
{ Application Specific Variables and constants }
const
szBuf = 255; // Size of buffer used to handle strings
WndClsSize = SizeOf(WNDCLASSEX); // Size of WNDCLASSEX Structure
wnd_class: PChar = 'TMainWindow'; // Main window class name.
wnd_title: PChar = 'Closer to the Metal'; // Main window caption.
var
wcx : WNDCLASSEX; // Main window structure
msgbuf : MSG; // Message structure
hwnd : LongWord; // A window handle
{ Windows Specific Constants }
const
{ Class styles }
CS_VREDRAW = LongWord(1);
CS_HREDRAW = LongWord(2);
CS_GLOBALCLASS = $4000;
{ Color Types }
COLOR_WINDOW = 5;
{ Window Styles }
WS_OVERLAPPED = 0;
WS_CAPTION = $C00000; { WS_BORDER or WS_DLGFRAME }
WS_SYSMENU = $80000;
WS_THICKFRAME = $40000;
WS_MINIMIZEBOX = $20000;
WS_MAXIMIZEBOX = $10000;
WS_VISIBLE = $10000000;
{ Common Window Styles }
WS_OVERLAPPEDWINDOW = (WS_VISIBLE or WS_OVERLAPPED or WS_CAPTION
or WS_SYSMENU or WS_THICKFRAME or WS_MINIMIZEBOX or WS_MAXIMIZEBOX);
{ Messages }
WM_DESTROY = $0002;
{ Windows API's }
function GetModuleHandle(lpModuleName: PChar): HMODULE;
stdcall; external 'kernel32.dll' name 'GetModuleHandleA';
procedure ExitProcess(uExitCode: LongWord);
stdcall; external 'kernel32.dll' name 'ExitProcess';
function DefWindowProc(hWnd: LongWord; Msg: LongWord;
wParam: Longint; lParam: Longint): Longint;
stdcall; external 'user32.dll' name 'DefWindowProcA';
function RegisterClassEx(const WndClass: WNDCLASSEX): Word;
stdcall; external 'user32.dll' name 'RegisterClassExA';
function CreateWindowEx(dwExStyle: LongWord; lpClassName: PChar;
lpWindowName: PChar; dwStyle: LongWord;
X, Y, nWidth, nHeight: Integer; hWndParent: LongWord;
hMenu: LongWord; hInstance: LongWord; lpParam: Pointer): LongWord;
stdcall; external 'user32.dll' name 'CreateWindowExA';
function GetMessage(var lpMsg: MSG; hWnd: LongWord;
wMsgFilterMin, wMsgFilterMax: LongWord): LongBool;
stdcall; external 'user32.dll' name 'GetMessageA';
function DispatchMessage(const lpMsg: MSG): Longint;
stdcall; external 'user32.dll' name 'DispatchMessageA';
procedure PostQuitMessage(nExitCode: Integer);
stdcall; external 'user32.dll' name 'PostQuitMessage';
{ Windows Procedure }
function WindowProc(hWnd, Msg: LongWord; wParam, lParam: Longint): Longint; stdcall;
asm
// The inline assembler will take care of setting the stack,
// preserving the registers and returning.
mov EAX, [Msg]
// WM_DESTROY:
cmp EAX, WM_DESTROY
je @@m_destroy
// All Other Messages:
jmp @@defwndproc
@@m_destroy:
push 0
call PostQuitMessage // Quit.
jmp @@return
@@defwndproc:
push [lParam]
push [wParam]
push [Msg]
push [hWnd]
call DefWindowProc
jmp @@finish
@@return:
xor eax,eax
@@finish:
end;
{ Main Program Block }
asm
// GetModuleHandle with a NULL pointer gives us the instance handle
// of the EXE file. This is the module that will "own" the window class.
push 0
call GetModuleHandle
// Define our window properties:
mov [wcx.cbSize], WndClsSize;
mov [wcx.style], CS_VREDRAW or CS_HREDRAW or CS_GLOBALCLASS
mov [wcx.lpfnWndProc], offset WindowProc
mov [wcx.cbClsExtra], 0
mov [wcx.cbWndExtra], 0
mov eax, hInstance
mov [wcx.hInstance], EAX
mov [wcx.hIcon], 0
mov [wcx.hCursor], 0
mov [wcx.hbrBackground], COLOR_WINDOW + 1
mov dword ptr [wcx.lpszMenuName], 0
mov dword ptr [wcx.lpszClassName], offset wnd_class
mov [wcx.hIconSm], 0
mov EAX, wnd_class
mov [wcx.lpszClassName], EAX
// Register window class:
push offset wcx
call RegisterClassEx
// Create window:
push 0 // lpParam
push [wcx.hInstance] // hInstance
push 0 // hMenu
push 0 // hWndParent
push 200 // nHeight
push 200 // nWidth
push 100 // y (top)
push 100 // x (left)
push WS_OVERLAPPEDWINDOW // dwStyle
mov EAX, wnd_title // lpWindowName
push EAX
mov EAX, wnd_class // lpClassName
push EAX
push 0 // dwExStyle
call CreateWindowEx
mov hwnd, EAX
// Message Loop/Pump:
@@msg_loop:
push 0 // wMsgFileterMax
push 0 // wMsgFilterMin
push 0 // hWnd (0 = all windows)
push offset msgbuf // lpMsg
call GetMessage
cmp eax, 0 // Returns 0 (zero) if WM_QUIT
jz @@end_loop
push offset msgbuf
call DispatchMessage
jmp @@msg_loop
@@end_loop:
// Terminating the program:
push 0 // Error return code.
call ExitProcess
end.
相關文章
- Win32彙編教程四 編寫一個簡單的視窗 (轉)Win32
- 如何編寫簡單的應用window視窗程式
- 用Java編寫一個最簡單的桌面程式Java
- 使用Delphi,SDK編寫Windows簡單程式 (轉)Windows
- 一個簡單的時間視窗設計與實現
- javascript編寫一個簡單的編譯器JavaScript編譯
- 用 Go 編寫一個簡單的 WebSocket 推送服務GoWeb
- 使用 Vala 編寫一個簡單的文字識別程式
- Delphi寫的讀狗程式的簡單逆向對比.
- 編寫一個簡單的智慧合約
- 編寫一個簡單的babel外掛Babel
- 編寫一個簡單的JavaScript模板引擎JavaScript
- 編寫一個非常簡單的 JavaScript 編輯器JavaScript
- 用 C 語言編寫一個簡單的垃圾回收器
- 用 Material Design 寫了一個簡單的 API 測試工具Material DesignAPI
- jquery 手寫一個簡單浮窗的反面教材jQuery
- 用Delphi編寫DelTree程式 (轉)
- 使用Node.js寫一個簡單的api介面Node.jsAPI
- 用java寫一個貪食蛇的簡單AIJavaAI
- 用 webpack 寫一個簡單的 JS SDKWebJS
- 用PHP寫一個簡單的日曆PHP
- 教你如何編寫第一個簡單的爬蟲爬蟲
- 寫了一個基於 API 的簡單圖床 AUXPIAPI圖床UX
- C編寫的簡單密碼程式密碼
- 排除Delphi5中編寫COM+程式的一個錯誤 (轉)
- 用Python寫一個簡單的Web框架PythonWeb框架
- Laravel 用 abstract 與 interface 寫一個簡單的支付回撥 demoLaravel
- 用VB編寫簡單的程式來清空文件選單 (轉)
- 用express寫個簡單的CRUDExpress
- 寫一個簡單的程式碼生成器
- 根據一個點獲取視窗控制程式碼的APIAPI
- 實現一個簡單的 jQuery 的 APIjQueryAPI
- 用Vue編寫一個簡單的仿Explorer檔案管理器Vue
- 用C++編寫一個簡單的釋出者和訂閱者C++
- 用C++編寫一個簡單的員工工資管理系統~C++
- 用Delphi編寫安裝程式(1) (轉)
- 編寫簡單的Java程式碼:HelloWoridJava
- 實現一個簡單的 RESTful APIRESTAPI