DELPHI 开发原生WINDOW窗口程序(过程/面向对象)
来源:互联网 发布:阿里云买的域名在哪看 编辑:程序博客网 时间:2024/06/06 12:22
一.DELPHI 开发原生WINDOW窗口程序(过程)
1.定义消息结构体TMessage(系统)
2.定义消息回调函数 WndProc
3.注册窗口信息到WINDOWS中(用到回调函数地址)
4.CreateWindow创建窗口
5.消息循环处理
program Project2;
uses
Windows, Messages, SysUtils;
const
AppName ='PascalHello';
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, '使用PASCAL撰写的NATIVE WINDOW程序', -1, r,
DT_SINGLELINE or DT_CENTER or DT_VCENTER);
EndPaint(Window, ps);
Exit;
end;
WM_Destroy:
begin
PostQuitMessage(0);
end;
WM_LBUTTONDOWN:
begin
MessageBox(0, 'LBUTTONDOWN Click.', nil, mb_Ok);
end;
end;
WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
end;
function WinRegister: Boolean;
var
WindowClass: WndClass;
begin
WindowClass.style := cs_hRedraw or cs_vRedraw;
WindowClass.lpfnWndProc := TFNWndProc(@WindowProc);
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 := AppName;
Result := RegisterClass(WindowClass) <> 0;
end;
function WinCreate: HWnd;
var
hWindow: HWnd;
begin
hWindow := CreateWindow(AppName, 'Hell0 world Object Pascal Program',
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;
Result := hWindow;
end;
var
AMessage: TMsg;
hWindow: HWnd;
begin
if not WinRegister then
begin
MessageBox(0, 'Register Failed', nil, mb_Ok);
Exit;
end;
hWinDow := WinCreate;
if (Longint(hWindow) = 0 ) then
begin
MessageBox(0, 'WinCreate Failed', nil, mb_Ok);
Exit;
end;
While GetMessage(AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
Halt(AMessage.wParam);
end.
二.面向对象改造:
DELPHI 开发原生WINDOW窗口程序(面向对象)
1.定义消息结构体TMessage(系统)
2.定义消息回调函数 WndProc
3.注册窗口信息到WINDOWS中(用到回调函数地址)
4.CreateWindow创建窗口
5.消息循环处理
program Project2;
uses
Windows,
Messages,
SysUtils;
const
AppName ='PascalHello';
Type
TMyWindow = Class(TObject)
private
WindowClass: WndClass;
hWindow: HWnd;
AMessage: TMsg;
function Winregister : Boolean;
procedure CreateMyWindow;
public
FAppName: String;
FWndProc: TFNWndProc;
constructor Create;
destructor Destroy; override;
procedure WinCreate;
procedure MyRun;
property ApplicationName : string read FAppName;
property WindowProcedure : TFNWndProc read FWndProc;
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, '使用PASCAL撰写的NATIVE WINDOW程序, 利用面向对象技术进行编程。', -1, r,
DT_SINGLELINE or DT_CENTER or DT_VCENTER);
EndPaint(Window, ps);
Exit;
end;
WM_Destroy:
begin
PostQuitMessage(0);
end;
WM_LBUTTONDOWN:
begin
MessageBox(0, 'LBUTTONDOWN Click.', nil, mb_Ok);
end;
end;
WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
end;
var
MyWindow : TMyWindow;
{ TMyWindow }
constructor TMyWindow.Create;
begin
end;
destructor TMyWindow.Destroy;
begin
inherited;
end;
procedure TMyWindow.CreateMyWindow;
begin
hWindow := CreateWindow(AppName, 'Hell0 world Object Pascal Program',
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.MyRun;
begin
While GetMessage(AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
Halt(AMessage.wParam);
end;
procedure TMyWindow.WinCreate;
begin
if Winregister then
CreateMyWindow;
end;
function TMyWindow.Winregister: Boolean;
begin
WindowClass.style := cs_hRedraw or cs_vRedraw;
WindowClass.lpfnWndProc := TFNWndProc(@WindowProc);
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 := AppName;
Result := RegisterClass(WindowClass) <> 0;
end;
begin
MyWindow := TMyWindow.Create;
MyWindow.FAppName := AppName;
MyWindow.FWndProc := TFNWndProc(@WindowProc);
MyWindow.WinCreate;
try
MyWindow.MyRun;
finally
FreeAndNil(MyWindow);
end;
end.
- DELPHI 开发原生WINDOW窗口程序(过程/面向对象)
- 采用面向对象的方式创建window原生程序
- Delphi之面向对象程序开发
- delphi实现简单window窗口程序
- 面向对象开发过程
- 面向对象开发过程简介
- 面向对象软件开发过程
- 面向对象系统开发过程
- 面向对象开发程序思路
- 原生js面向对象
- 原生JavaScript面向对象
- 原生Javascript面向对象
- 原生JS面向对象
- 原生JS面向对象
- 原生JS面向对象
- delphi-x2d 原生开发android程序的框架图
- 面向对象开发方法的开发过程
- 面向对象程序的设计过程
- 老婆`!永远不要分开好吗!
- 《葵花宝典:WPF自学手册》三大网点书评集萃
- 在windows xp下编译WebKit
- 飞鸽在ubuntu上的安装方法
- ...摘自圣经~~~
- DELPHI 开发原生WINDOW窗口程序(过程/面向对象)
- Linux设备驱动开发环境的搭建
- 面对不断升级的内核,如何学习linux设备驱动)
- about Oops Messages
- VS2005 将unsigned char * 转化成 CString
- 我的艾泽拉斯档案
- Garbage Collection : Automatic Memory Management in the Microsoft .NET Framework 垃圾回收:在微软NET框架自动内存管理(一)
- SQL中CONVERT转化函数的用法
- 百度岳国峰首次披露移动搜索相关数据