首页  编辑  

自画Caption

Tags: /超级猛料/VCL/Form,窗体/标题栏和边框/   Date Created:

下面的代码改变标题栏字体

procedure WMNCPAint(var Mes : TWMNCPaint); message WM_NCPAINT;

procedure TForm1.WMNCPAint(var Mes : TWMNCPaint);

var

 ACanvas : TCanvas;

begin

 ACanvas := TCanvas.Create;

 try

   ACanvas.Handle := GetWindowDC(Form1.Handle);

   with ACanvas do begin

     Brush.Color := clActiveCaption;

     Font.Name := 'Times New Roman';

     Font.Size := 10;

     Font.Color := clCaptionText;

     Font.Style := [fsItalic, fsBold];

     TextOut(GetSystemMetrics(SM_CYMENU) + GetSystemMetrics(SM_CXBORDER),

             Round((GetSystemMetrics(SM_CYCAPTION) - Abs(Font.Height))/2) +1,

             ' Your title here!');

   end;

 finally

   ReleaseDC(Form1.Handle, ACanvas.Handle);

   ACanvas.Free;

 end;

end;

---------------------------------------

要实现自己绘制窗口的标题栏,必须拦截两个消息 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;

   }

 }

}

// 就这么简单