|
|
九、进一步封装,面向对象的方法编写
更进一步的话,我们就可以考虑用面向对象的方法来编写。
可以看到如果我们要用上面的方法编写程序的话,工作量是十分繁重的,尤其在程序规模越来越大的情况下,出错的可能性也就会成指数性的增长。即使我们使用粘贴拷贝仍然要对程序进行修改,出错的可能人不会避免。而面向对象的编程方法可以有效的克服以上的不足,大大的提高了代码的利用率。所以下面我们就用面向对象的方法来看一看同样的程序我们应该怎样编写。
面向对象的核心就是要用类的方法来建立程序框架,然后用类的实例调用类的方法属性等手段来实现最终的效果。所以第一步我们首先又来建立一个窗口程序类,其中共有成员函数Create和 Destroy分别用来创建和销毁一个我们建立的窗口程序类的实例。共有成员函数WinCreate则调用私有成员函数WinRegister和 CreateMyWindow来注册和创建窗口。其中的属性变量ApplicationName 和 WindowProcedure 则是用来获得窗口程序类名和窗口消息处理函数的。下面是修改后的源代码:
//用面向对象的方法来编写
program TMyWindowClass;
uses
Windows,
Messages,
SysUtils;
//创建窗口类
type
TMyWindow = class ( TObject )
private
{ 定义私有变量 }
WindowClass : WndClass;
hWindow : HWnd ;
AMessage : TMsg ;
FAppName : String ;
FWndProc : TFNWndProc ;
function WinRegister : Boolean ; virtual ;
procedure CreateMyWindow ;
public
{定义公有变量 }
constructor Create ;
destructor Destroy ; override ;
procedure WinCreate ; virtual ;
procedure Run ;
{定义属性 }
property ApplicationName : String read FAppName write FAppName ;
property WindowProcedure : TFNWndProc read FWndProc write FWndProc ;
end ;
const
AppName = ' MyClassshili ' ;
var
myWindow : tMyWindow ;
{ TMyWindow类中公有函数的实现 }
constructor TMyWindow . Create ;
begin
end ;
destructor TMyWindow . Destroy ;
begin
inherited ;
end ;
procedure TMyWindow . CreateMyWindow ; //创建窗口
begin
hWindow : = CreateWindow ( AppName , ' hello , world ' ,
ws_OverlappedWindow , cw_UseDefault , cw_UseDefault ,
cw_UseDefault , cw_UseDefault , 0 , 0 , system . MainInstance , nil ) ;
if hWindow <> 0 then begin
ShowWindow ( hWindow , CmdShow ) ;
ShowWindow ( hWindow , SW_SHOW ) ;
UpdateWindow ( hWindow ) ;
end ;
end ;
procedure TMyWindow . WinCreate ;
begin
if WinRegister then
begin
CreateMyWindow ;
end ;
end ;
function TMyWindow . WinRegister : Boolean ; //注册窗口类
begin
WindowClass.Style : = cs_hRedraw or cs_vRedraw ;
WindowClass . lpfnWndProc : = FWndProc ;
WindowClass . cbClsExtra : = 0 ;
WindowClass . cbWndExtra : = 0 ;
WindowClass . hInstance : = system.MainInstance ;
WindowClass . hIcon : = LoadIcon ( 0 , idi_Application ) ;
WindowClass . hCursor : = LoadCursor ( 0 , idc_Arrow ) ;
WindowClass . hbrBackground : = GetStockObject ( WHITE_BRUSH ) ;
WindowClass . lpszMenuName : = nil ;
WindowClass . lpszClassName : = PChar ( FAppName ) ;
Result : = RegisterClass ( WindowClass ) <> 0 ;
end ;
function WindowProc ( Window : HWnd ; Amessage : UINT ; WParam : WPARAM ;
LParam : LPARAM ) : LRESULT ; stdcall ; export ;
//消息处理函数
var
dc : hdc ;
ps : TPaintStruct ;
r : TRect ;
begin
WindowProc : = 0 ;
case AMessage of
WM_PAINT :
begin
dc : = BeginPaint ( Window , ps ) ;
GetClientRect ( Window , r ) ;
DrawText ( dc , ' hello , world ! ! ' , -1 , r ,
DT_SINGLELINE or DT_CENTER or DT_VCENTER ) ;
EndPaint ( Window , ps ) ;
Exit ;
end ;
wm_Destroy :
begin
PostQuitMessage ( 0 ) ;
Exit ;
end ;
end ;
WindowProc : = DefWindowProc ( Window , AMessage , WParam , LParam ) ;
end ;
procedure TMyWindow . MyRun ;
begin
while GetMessage ( AMessage , 0 , 0 , 0 ) do begin
TranslateMessage ( AMessage ) ;
DispatchMessage ( AMessage ) ;
end ;
Halt ( AMessage . wParam ) ;
end ;
//主程序,程序运行的进入口
begin
myWindow : = TMyWindow . Create ;
myWindow . ApplicationName : = AppName ;
myWindow . WindowProcedure : = TFNWndProc ( @WindowProc ) ;
myWindow . WinCreate ;
try
myWindow . MyRun ;
finally
FreeAndNil ( myWindow ) ;
end ;
end .
好了,就这些吧。相信有了这些基础应该在看delphi源代码时容易了许多,毕竟很多代码在此都很熟悉了。
|
|