在窗口标题区添加按钮
【打印文章】
作者:南京 邹莉
ReadBook是一款优秀的软件,我相信读者中也有不少人在使用它。关于它的病毒自我监测功能的实现,《电脑爱好者》已经讨论过了。今天,我想给大家谈谈它是如何在窗口的标题区添加按钮的。有可能我的实现方法和ReadBook的不一样,没关系,大家互相学习吧!
程序主要用到了几个消息,除了程序中消息函数定义的方法外,大家还要注意每一个消息函数中都调用了inherited,原因不用我饶舌了吧?程序中还调用了GetSystemMetrics、DrawButtonFace等几个函数。我本来在程序中详细地加了注释,不过我后来都删除了,因为Delphi的帮助文件可以说是最好的注释了。如果你有什么不懂之处,别忘了按“F1”键,怎么样,一切都写得清清楚楚的吧?
把下边的程序输进计算机,自己用帮助文件研究吧。记住,学习编程可不能偷懒哦。
unit Unit1;
interface
uses
Windows, Buttons, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormResize(Sender: TObject);
private
{ Private declarations }
CaptionBtn:TRect;
procedure DrawCaptButton;
rocedure WMNCPaint(var Msg:TWMNCPaint);message WM_NCPaint;
procedure WMNCActivate(var Msg:TWMNCActivate);message WM_NCActivate;
procedure WMSetText(var Msg:TWMSetText);message WM_SetText;
procedure WMNCHitTest(var Msg:TWMNCHitTest);message WM_NCHittest;
procedure WMNCLButtonDown(var Msg:TWMNCLButtonDown);message WM_NCLButtonDown;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
const
htCaptionBtn=htSizeLast+1;
{$R *.DFM}
procedure TForm1.DrawCaptButton;
var
xFrame,yFrame,xSize,ySize:Integer;
R:TRect;
begin
xFrame:=GetSystemMetrics(SM_CXFRAME);
yFrame:=GetSystemMetrics(SM_CYFRAME);
xSize:=GetSystemMetrics(SM_CXSIZE);
ySize:=GetSystemMetrics(SM_CYSIZE);
CaptionBtn:=Bounds(Width-xFrame-4*xSize+2,yFrame+2,xSize+1,ySize-4);
Canvas.Handle:=GetWindowDC(Self.Handle);
Canvas.Font.Name:='宋体';
Canvas.Font.Color:=clBlack;
Canvas.Pen.Color:=clYellow;
Canvas.Brush.Color:=clBtnFace;
try
DrawButtonFace(Canvas,CaptionBtn,1,bsAutoDetect,False,False,False);
R:=Bounds(Width-xFrame-4*xSize+3,yFrame+3,xSize-2,ySize-7);
with CaptionBtn do
Canvas.TextRect(R,R.Left+2,R.Top+2,'↘');
finally
ReleaseDC(Self.Handle,Canvas.Handle);
Canvas.Handle:=0;
end;
end;
procedure TForm1.WMNCActivate(var Msg: TWMNCActivate);
begin
inherited;
DrawCaptButton;
end;
procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
begin
inherited;
with Msg do
if PtInRect(CaptionBtn,Point(xPos-Left,yPos-Top)) then
Result:=htCaptionBtn;
end;
procedure TForm1.WMNCLButtonDown(var Msg: TWMNCLButtonDown);
begin
inherited;
if(Msg.HitTest=htCaptionBtn)then
showmessage('成功了!');
end;
procedure TForm1.WMNCPaint(var Msg: TWMNCPaint);
begin
inherited;
DrawCaptButton;
end;
procedure TForm1.WMSetText(var Msg: TWMSetText);
begin
inherited;
DrawCaptButton;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Perform(WM_NCACTIVATE,Word(Active),0);
end;
end.
仔细研究一下程序中有关计算的几个句子,试着改变某个数字看一看,这将有助于你理解程序。
本文中的程序在Windows98/WindowsMe环境下使用Delphi5/ Delphi6调试通过。
ReadBook是一款优秀的软件,我相信读者中也有不少人在使用它。关于它的病毒自我监测功能的实现,《电脑爱好者》已经讨论过了。今天,我想给大家谈谈它是如何在窗口的标题区添加按钮的。有可能我的实现方法和ReadBook的不一样,没关系,大家互相学习吧!
程序主要用到了几个消息,除了程序中消息函数定义的方法外,大家还要注意每一个消息函数中都调用了inherited,原因不用我饶舌了吧?程序中还调用了GetSystemMetrics、DrawButtonFace等几个函数。我本来在程序中详细地加了注释,不过我后来都删除了,因为Delphi的帮助文件可以说是最好的注释了。如果你有什么不懂之处,别忘了按“F1”键,怎么样,一切都写得清清楚楚的吧?
把下边的程序输进计算机,自己用帮助文件研究吧。记住,学习编程可不能偷懒哦。
unit Unit1;
interface
uses
Windows, Buttons, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormResize(Sender: TObject);
private
{ Private declarations }
CaptionBtn:TRect;
procedure DrawCaptButton;
rocedure WMNCPaint(var Msg:TWMNCPaint);message WM_NCPaint;
procedure WMNCActivate(var Msg:TWMNCActivate);message WM_NCActivate;
procedure WMSetText(var Msg:TWMSetText);message WM_SetText;
procedure WMNCHitTest(var Msg:TWMNCHitTest);message WM_NCHittest;
procedure WMNCLButtonDown(var Msg:TWMNCLButtonDown);message WM_NCLButtonDown;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
const
htCaptionBtn=htSizeLast+1;
{$R *.DFM}
procedure TForm1.DrawCaptButton;
var
xFrame,yFrame,xSize,ySize:Integer;
R:TRect;
begin
xFrame:=GetSystemMetrics(SM_CXFRAME);
yFrame:=GetSystemMetrics(SM_CYFRAME);
xSize:=GetSystemMetrics(SM_CXSIZE);
ySize:=GetSystemMetrics(SM_CYSIZE);
CaptionBtn:=Bounds(Width-xFrame-4*xSize+2,yFrame+2,xSize+1,ySize-4);
Canvas.Handle:=GetWindowDC(Self.Handle);
Canvas.Font.Name:='宋体';
Canvas.Font.Color:=clBlack;
Canvas.Pen.Color:=clYellow;
Canvas.Brush.Color:=clBtnFace;
try
DrawButtonFace(Canvas,CaptionBtn,1,bsAutoDetect,False,False,False);
R:=Bounds(Width-xFrame-4*xSize+3,yFrame+3,xSize-2,ySize-7);
with CaptionBtn do
Canvas.TextRect(R,R.Left+2,R.Top+2,'↘');
finally
ReleaseDC(Self.Handle,Canvas.Handle);
Canvas.Handle:=0;
end;
end;
procedure TForm1.WMNCActivate(var Msg: TWMNCActivate);
begin
inherited;
DrawCaptButton;
end;
procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
begin
inherited;
with Msg do
if PtInRect(CaptionBtn,Point(xPos-Left,yPos-Top)) then
Result:=htCaptionBtn;
end;
procedure TForm1.WMNCLButtonDown(var Msg: TWMNCLButtonDown);
begin
inherited;
if(Msg.HitTest=htCaptionBtn)then
showmessage('成功了!');
end;
procedure TForm1.WMNCPaint(var Msg: TWMNCPaint);
begin
inherited;
DrawCaptButton;
end;
procedure TForm1.WMSetText(var Msg: TWMSetText);
begin
inherited;
DrawCaptButton;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
Perform(WM_NCACTIVATE,Word(Active),0);
end;
end.
仔细研究一下程序中有关计算的几个句子,试着改变某个数字看一看,这将有助于你理解程序。
本文中的程序在Windows98/WindowsMe环境下使用Delphi5/ Delphi6调试通过。
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】