對於我們熟悉的標准windows窗口來講
下面我們就討論一下在delphi中如何給窗口的標題欄上添加新的按鈕
一
在過程中要使用win
A
B
C
D
E
二
我們結合源程序來講述過程的實現
unit Main;
interface
uses
Windows
type
TForm
procedure FormResize(Sender: TObject);
private
CaptionBtn : TRect;
procedure DrawCaptButton;
procedure 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
Form
implementation
const
htCaptionBtn = htSizeLast +
{$R *
procedure TForm
file://drawcapbuttton過程的具體實現
var
xFrame
yFrame
xSize
ySize : Integer;
R : TRect;
begin
xFrame := GetSystemMetrics(SM_CXFRAME);
yFrame := GetSystemMetrics(SM_CYFRAME);
file://把窗口的寬度置於變量xFrame
xSize:= GetSystemMetrics(SM_CXSIZE);
ySize:= GetSystemMetrics(SM_CYSIZE);
// 把標題欄按鈕的寬度置於變量xSize
CaptionBtn := Bounds(Width
yFrame +
file://定義出新的標題按鈕的位置
Canvas
file://得到窗口的句柄
Canvas
Canvas
Canvas
Canvas
Canvas
file://定義畫布的字體
try
DrawButtonFace(Canvas
file://在畫布上畫出定義的按鈕
R := Bounds(Width
yFrame +
file://在新按鈕上畫出一個小矩形
with CaptionBtn do
Canvas
file://在上面畫出的小矩形上填寫一個字符
finally
ReleaseDC(Self
Canvas
file://容錯處理
end;
end;
procedure TForm
//WMNCPaint過程的具體實現
begin
inherited;//繼承默認的消息處理程序
DrawCaptButton;//對按鈕進行重畫
end;
procedure TForm
// WMNCActivate過程與WMNCPaint過程實現方法相同
begin
inherited;
DrawCaptButton;
end;
procedure TForm
// WMSetText過程與WMNCPaint過程實現方法相同
begin
inherited;
DrawCaptButton;
end;
procedure TForm
file:// WMNCHitTest過程與WMNCPaint過程實現方法相同
begin
inherited;
with Msg do
if PtInRect(CaptionBtn
Result := htCaptionBtn;//判斷鼠標所在位置是否在新按鈕的矩形范圍內
end;
procedure TForm
// WMNCLButtonDown過程與WMNCPaint過程實現方法相同
begin
inherited;
if (Msg
ShowMessage(
file://判斷被點擊的是否是新按鈕
end;
procedure TForm
begin
Perform(WM_NCACTIVATE
file://如果窗口大小改變則重畫標題欄
end;
end
三
如圖
圖
通過以上的示例過程
From:http://tw.wingwit.com/Article/program/Delphi/201311/25072.html