Delphi直接用WindowsAPI编程选读.doc

Delphi程序员往往习惯了用VCL元件编程,其实Delphi也能进行基于WINDOWS API SDK的编程。而且用Delphi在某些方面效果似乎比用Visual C++效果还要好。比如本例程,用Delphi 6编译出来只有9216字节(9k)而同样的Visual C++程序却有16896字节(17k)。(此例程是笔者从网上下载的c++源码例程,其中有c源程序,和编译好的.exe文件。源代码经笔者改写成Delphi代码。)这证明Delphi编译器的优化效果非常好。 API是(Application Programming Interface)的缩写,意为应用编程界面,它包含了编写Windows所有函数、数据类型。VCL就是以它为基础进行封装的,它是应用程序在Windows 上运行的基础。通过熟悉使用WINDOWS API SDK直接编制WINDOWS程序,程序员将对WINDOWS的执行机制有更深入的了解,从而编写出更高效、实用的程序。 下面是我们用API函数建立的第一个程序: 1 : program HELLOWIN; 2 : 3 : uses 4 : windows, Messages ,mmsystem; 5 : 6 : 7 : 8 : var 9 : sz_appname:array [0..8] of char=HelloWin#0; 10 : Win_Class: WNDCLASSEX; //窗口类 11 : w_Handle,inst:HWND;//w_Handle窗口句柄、程序句柄 12 : w_msg:TMSG; //消息数据 13 : 14 : function WindowProc(h_Wnd,u_Msg,w_Param,l_Param: LONGINT):LRESULT;stdcall; 15 : //回调函数 16 : var p_hdc:hdc; 17 : p_rect:trect; 18 : ps : PAINTSTRUCT ; 19 : begin 20 : 21 : 22 : case u_msg of 23 : WM_DESTROY : PostQuitMessage (0); 24 : WM_CREATE : PlaySound (pchar(hellowin.wav#0), 0, SND_FILENAME or SND_ASYNC) ; 25 : WM_PAINT :begin 26 : p_hdc := BeginPaint (h_wnd, ps) ; 27 : GetClientRect (h_wnd, p_rect); 28 : DrawText (p_hdc, pchar(Hello, Windows!#0), -1, p_rect, 29 : DT_SINGLELINE or DT_CENTER or DT_VCENTER) ; 30 : EndPaint (h_wnd, ps) ; 31 : end; 32 : 33 : end; 34 : Result := DefWindowProc(h_Wnd, u_Msg, w_Param, l_Param); 35 : end; 36 : 37 : 38 : 39 : 40 : begin 41 : Inst := hInstance; 42 : win_class.cbSize := sizeof (win_class) ; 43 : win_class.style := CS_HREDRAW or CS_VREDRAW ; 44 : win_class.lpfnWndProc := @WindowProc ; 45 : win_class.cbClsExtra := 0 ; 46 : win_class.cbWndExtra := 0 ; 47 : win_class.hInstance := Inst ; 48 : win_class.hIcon := LoadIcon (0, IDI_APPLICATION) ; 49 : win_class.hCursor := Load

文档评论(0)

1亿VIP精品文档

相关文档