簡單的Windows API示例
2008-04-09 04:26:39来源:互联网 阅读 ()
說明:
一:新建一個工程,移除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);
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
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));
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash