I
import
Unregistered / Unconfirmed
GUEST, unregistred user!
要实现自己绘制窗口的标题栏,必须拦截两个消息 WM_NCPAINT 和 WM_NCACTIVATE。我在
这里给出部分关键的 C++ Builder 代码,其实用 Delphi 实现也是很容易的。
class TForm1 : public TForm
{
// 窗体类声明细节省略
private:
void __fastcall WMNCPaint(TMessage &Msg);
void __fastcall WMNCActivate(TWMNCActivate &Msg);
protected:
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_NCPAINT, TMessage, WMNCPaint)
VCL_MESSAGE_HANDLER(WM_NCACTIVATE, TWMNCActivate, WMNCActivate)
END_MESSAGE_MAP(TForm)
};
void __fastcall TForm1::WMNCActivate(TWMNCActivate &Msg)
{
Msg.Result = true;
if(!Msg.Active)
{
DefWindowProc(this->Handle, Msg.Msg, Msg.active, 0);
return;
}
TMessage pm;
pm.Msg = Msg.Msg;
pm.WParam = Msg.Active;
WMNCPaint(pm);
}
void __fastcall TForm1::WMNCPaint(TMessage &Msg)
{
HDC hdc;
TRect cr;
TCanvas *wc = NULL;
DefWindowProc(this->Handle, Msg.Msg, Msg.WParam, Msg.LParam);
hdc = GetWindowDC(this->Handle);
try
{
wc = new TCanvas;
wc->Handle = hdc;
// 计算标题栏尺寸
cr.Left = GetSystemMetrics(SM_CXFRAME);
cr.Top = GetSystemMetrics(SM_CYFRAME);
cr.Right = this->Width - 4 * GetSystemMetrics(SM_CXSIZE);
cr.Bottom = cr.Top + GetSystemMetrics(SM_CYCAPTION) - 1;
// 获取标题栏的 Rect 后就可以直接在 wc 画布上画了,直接在标题栏上出效果!
// 绘画代码省略
// ... ...
}
__finally
{
ReleaseDC(this->Handle, hdc);
if(wc != NULL)
{
delete wc;
wc = NULL;
}
}
}
// 就这么简单
这里给出部分关键的 C++ Builder 代码,其实用 Delphi 实现也是很容易的。
class TForm1 : public TForm
{
// 窗体类声明细节省略
private:
void __fastcall WMNCPaint(TMessage &Msg);
void __fastcall WMNCActivate(TWMNCActivate &Msg);
protected:
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_NCPAINT, TMessage, WMNCPaint)
VCL_MESSAGE_HANDLER(WM_NCACTIVATE, TWMNCActivate, WMNCActivate)
END_MESSAGE_MAP(TForm)
};
void __fastcall TForm1::WMNCActivate(TWMNCActivate &Msg)
{
Msg.Result = true;
if(!Msg.Active)
{
DefWindowProc(this->Handle, Msg.Msg, Msg.active, 0);
return;
}
TMessage pm;
pm.Msg = Msg.Msg;
pm.WParam = Msg.Active;
WMNCPaint(pm);
}
void __fastcall TForm1::WMNCPaint(TMessage &Msg)
{
HDC hdc;
TRect cr;
TCanvas *wc = NULL;
DefWindowProc(this->Handle, Msg.Msg, Msg.WParam, Msg.LParam);
hdc = GetWindowDC(this->Handle);
try
{
wc = new TCanvas;
wc->Handle = hdc;
// 计算标题栏尺寸
cr.Left = GetSystemMetrics(SM_CXFRAME);
cr.Top = GetSystemMetrics(SM_CYFRAME);
cr.Right = this->Width - 4 * GetSystemMetrics(SM_CXSIZE);
cr.Bottom = cr.Top + GetSystemMetrics(SM_CYCAPTION) - 1;
// 获取标题栏的 Rect 后就可以直接在 wc 画布上画了,直接在标题栏上出效果!
// 绘画代码省略
// ... ...
}
__finally
{
ReleaseDC(this->Handle, hdc);
if(wc != NULL)
{
delete wc;
wc = NULL;
}
}
}
// 就这么简单