分享
 
 
 

簡單的Windows API示例

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

說明:

一:新建一個工程,移除Form1。選擇Project\View source,拷貝下面的源代碼覆蓋已有的代碼。保存(如:D:\Projects\Source\WinAPI\WinAPI.dpr)。

二:新建WinAPI.inc文件到保存工程的目錄,用記事本打開,鍵入如下代碼:

const

cm_About = 101; //此處一定要回車,否則BRCC32 編譯時不能通過

保存(如:D:\Projects\Source\WinAPI\WinAPI.inc)。

三:新建AddRes.rc文件到保存工程的目錄,用記事本打開,鍵入如下代碼:

#include "WinAPI.inc"

WndMenu MENU

BEGIN

POPUP "Help"

BEGIN

MENUITEM "&About", cm_About

END

END

AboutDialog DIALOG 18, 18, 141, 58

STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU

CAPTION "About"

BEGIN

PUSHBUTTON "OK", IDOK, 45, 40, 50, 12,

WS_CHILD | WS_VISIBLE | WS_TABSTOP

CTEXT " Disigned by Swayi.F ", -1, 1, 9, 140, 8,

WS_CHILD | WS_VISIBLE | WS_GROUP

END

保存。

打開MS-DOS(Win9X: command; NT,2000: cmd)程序,輸入下列命令以編譯資源文件 BRCC32 -r FileName(如:D:\Projects\Source\WinAPI\addres.rc)回車。這時會在你輸入的文件名路徑下產生一個叫AddRes.res的文件。這是程序正常運行需要用到的資源文件。

四:編譯第一步保存的工程。運行。

五:程序運行時,你可以按鍵,其鍵值或相應提示會在窗口上有顯示﹔在移動鼠標時嘗試按住Shift, Ctrl,或左鍵,或右鍵,會顯示不同的顏色﹔雙擊﹔再嘗試調整窗口的大小看看效果。

六:如有疑問,歡迎與我討論:swayi@tom.com

program WinAPI;

uses

Windows, Messages, SysUtils;

{$R *.RES}

{$R AddRes.res}

{$I WinAPI.inc}

const

sCLS_NAME = 'WinAPI';

sSPACE = ' ';

sERR_REGISTER = 'Register instance failed.';

sERR_CREAT = 'Create instance failed.';

sON_CHAR = 'Char: %s ; Repeat: %d.' + sSPACE;

sON_SYS_KEY_DOWN = 'Alt key: %d ; Repeat: %d.' + sSPACE;

sON_KEY_DOWN = 'Key: %d ; Repeat: %d.' + sSPACE;

sON_MOUSE_MOVE = 'XPos: %d ; YPos: %d; Flags: %d' + sSPACE;

sON_LBTN_DBL_CLK = 'Double click pos: %d, %d; Flags: %d.' + sSPACE;

sMSG_TITLE = 'Window API program';

sMSG_TEXT_OUT = 'Press, move, double click and resize now! ';

sMSG_DRAW_TEXT = 'Printed out by DrawText.';

sMSG_HOME = 'Home key is pressed right now.' + sSPACE;

sMSG_LEFT = 'Left key is pressed right now.' + sSPACE;

sMSG_DELETE = 'Delete key is pressed right now.' + sSPACE;

sMSG_LWIN = 'Left Win key is pressed right now.' + sSPACE;

sMSG_F1 = 'F1 key is pressed right now.' + sSPACE;

nSYS_X = 100;

nSYS_Y = 100;

nWIDTH = 500;

nHEIGHT = 360;

nX = 20;

nY = 40;

nTOP = 240;

var

S: string;

procedure OnPaint(AHnd: HWND; var AMsg: TWMPaint);

var

DC: HDC;

AStruct: TPaintStruct;

ARect: TRect;

OldFont: HFont;

begin

DC := BeginPaint(AHnd,AStruct);

OldFont := SelectObject(DC, GetStockObject(SYSTEM_FIXED_FONT));

GetClientRect(AHnd, ARect);

SetBkMode(DC, TRANSPARENT);

ARect.Top := nTOP;

TextOut(DC, 5, 5, sMSG_TEXT_OUT, Length(sMSG_TEXT_OUT));

DrawText(DC, sMSG_DRAW_TEXT, -1, ARect, DT_SINGLELINE or DT_RIGHT);

SelectObject(DC, OldFont);

EndPaint(AHnd, AStruct);

end;

procedure OnChar(AHnd: HWND; var AMsg: TWMChar);

var

DC: HDC;

begin

DC := GetDC(AHnd);

S := Format(sON_CHAR, [Char(AMsg.CharCode), LoWord(AMsg.KeyData)]);

SetBkColor(DC, GetSysColor(COLOR_BTNFACE));

TextOut(DC, nX, nY, PChar(S), Length(S));

ReleaseDC(AHnd, DC);

end;

procedure OnSysKeyDown(AHnd: HWND; var AMsg: TWMSysKeyDown);

var

DC: HDC;

begin

DC := GetDC(AHnd);

S := Format(sON_SYS_KEY_DOWN, [AMsg.CharCode, LoWord(AMsg.KeyData)]);

SetBkColor(DC, GetSysColor(COLOR_BTNFACE));

TextOut(DC, nX, nY + 20, PChar(S), Length(S));

ReleaseDC(AHnd, DC);

end;

procedure OnKeyDown(AHnd: HWND; var AMsg: TWMKeyDown);

var

DC: HDC;

begin

DC := GetDC(AHnd);

case AMsg.CharCode of

VK_HOME: S := sMSG_HOME;

VK_LEFT: S := sMSG_LEFT;

VK_DELETE: S := sMSG_DELETE;

VK_LWIN: S := sMSG_LWIN;

VK_F1: S := sMSG_F1;

else

S := Format(sON_KEY_DOWN, [AMsg.CharCode, LoWord(AMsg.KeyData)]);

end;

SetBkColor(DC, GetSysColor(COLOR_BTNFACE));

TextOut(DC, nX, nY + 40, PChar(S), Length(S));

ReleaseDC(AHnd, DC);

end;

procedure OnMouseMove(AHnd: HWND; var AMsg: TWMMouseMove);

var

DC: HDC;

begin

DC := GetDC(AHnd);

S := Format(sON_MOUSE_MOVE, [AMsg.XPos, AMsg.YPos, AMsg.Keys]);

if ((AMsg.Keys and MK_CONTROL) = MK_CONTROL) then

SetTextColor(DC, RGB(0, 0, 255));

if ((AMsg.Keys and MK_LBUTTON) = MK_LBUTTON) then

SetTextColor(DC, RGB(127, 127, 0));

if ((AMsg.Keys and MK_RBUTTON) = MK_RBUTTON) then

SetTextColor(DC, RGB(255, 0, 0));

if ((AMsg.Keys and MK_SHIFT) = MK_SHIFT) then

SetTextColor(DC, RGB(255, 0, 255));

SetBkColor(DC, GetSysColor(COLOR_BTNFACE));

TextOut(DC, nX, nY + 60, PChar(S), Length(S));

ReleaseDC(AHnd, DC);

end;

procedure OnDblClk(AHnd: HWND; var AMsg: TWMLButtonDblClk);

var

DC: HDC;

begin

DC := GetDC(AHnd);

S := Format(sON_LBTN_DBL_CLK, [AMsg.XPos, AMsg.YPos, AMsg.Keys]);

SetBkColor(DC, GetSysColor(COLOR_BTNFACE));

SetTextColor(DC, RGB(127, 127, 127));

TextOut(DC, nX, nY + 80, PChar(S), Length(S));

ReleaseDC(AHnd, DC);

end;

function WndAbout(HndAbout: HWND; AMsg, WParam, LParam: LongInt): BOOL; stdcall;

begin

Result := True;

case AMsg of

WM_INITDIALOG: Exit;

WM_COMMAND:

if (WParam = idOK) or (WParam = idCancel) then

begin

EndDialog(HndAbout, 1);

Exit;

end;

end;

Result := False;

end;

function WndProc(HndWnd: HWND; AMsg, WParam, LParam: LongInt): LongInt; stdcall;

var

AMessage: TMessage;

begin

AMessage.Msg := AMsg;

AMessage.WParam := WParam;

AMessage.LParam := LParam;

AMessage.Result := 0;

case AMsg of

WM_PAINT: OnPaint(HndWnd, TWMPaint(AMessage));

WM_CHAR: OnChar(HndWnd, TWMChar(AMessage));

WM_SYSKEYDOWN: OnSysKeyDown(HndWnd, TWMSysKeyDown(AMessage));

WM_KEYDOWN: OnKeyDown(HndWnd, TWMKeyDown(AMessage));

WM_MOUSEMOVE: OnMouseMove(HndWnd, TWMMouseMove(AMessage));

WM_LBUTTONDBLCLK: OnDblClk(HndWnd, TWMLButtonDblClk(AMessage));

WM_COMMAND:

if WParam = cm_About then

begin

DialogBox(HInstance, 'AboutDialog', HndWnd, @WndAbout);

Result := DefWindowProc(HndWnd, AMsg, WParam, LParam);

Exit;

end;

WM_DESTROY: PostQuitMessage(WM_QUIT);

else begin

Result := DefWindowProc(HndWnd, AMsg, WParam, LParam);

Exit;

end;

end;

Result := AMessage.Result;

end;

function RegisterInstance: Boolean;

var

AWndCls: TWndClass;

begin

AWndCls.style := CS_VREDRAW or CS_HREDRAW or CS_DBLCLKS;

AWndCls.lpfnWndProc := @WndProc;

AWndCls.cbClsExtra := 0;

AWndCls.cbWndExtra := 0;

AWndCls.hInstance := HInstance;

AWndCls.hIcon := LoadIcon(0, IDI_WINLOGO);

AWndCls.hCursor := LoadCursor(0, IDC_ARROW);

AWndCls.hbrBackground := HBrush(Color_Window);

AWndCls.lpszMenuName := 'WndMenu';

AWndCls.lpszClassName := sCLS_NAME;

Result := RegisterClass(AWndCls) <> 0;

end;

function CreateInstance: THandle;

var

AHnd: THandle;

begin

AHnd := CreateWindow(sCLS_NAME, sMSG_TITLE, WS_OVERLAPPEDWINDOW, nSYS_X, nSYS_Y,

nWIDTH, nHEIGHT, 0, 0, HInstance, nil);

if AHnd <> 0 then

begin

ShowWindow(AHnd, SW_SHOW );

UpdateWindow(AHnd);

end;

Result := AHnd;

end;

var

AMsg: TMsg;

begin

if not RegisterInstance then

begin

MessageBox(0, sERR_REGISTER, nil, MB_OK);

Exit;

end;

if CreateInstance = 0 then

begin

MessageBox(0, sERR_CREAT, nil, MB_OK);

Exit;

end;

while GetMessage(AMsg, 0, 0, 0) do

begin

TranslateMessage(AMsg);

DispatchMessage(AMsg);

end;

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有