程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> 自畫標題欄的一個例子

自畫標題欄的一個例子

編輯:vc教程

.h File

//---------------------------------------------------------------------------
#ifndef wmncH
#define wmncH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
     TBitBtn *BitBtn1;
     void __fastcall FormPaint(TObject *Sender);
     void __fastcall FormResize(TObject *Sender);
     void __fastcall BitBtn1Click(TObject *Sender);
private:// User declarations
     void __fastcall OnNcHitTest(TMessage &Message);
public: // User declarations
     __fastcall TForm1(TComponent* Owner);
     BEGIN_MESSAGE_MAP
      VCL_MESSAGE_HANDLER(WM_NCHITTEST,TMessage,OnNcHitTest);
     END_MESSAGE_MAP(TForm);
protected:
     virtual void __fastcall WndProc(TMessage &Message);
     virtual void __fastcall CreateParams(TCreateParams &Params);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
.cpp File
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "wmnc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CreateParams(TCreateParams &Params)
{
  TForm::CreateParams(Params);
  Params.Style|=WS_POPUP;
  Params.Style^=WS_DLGFRAME;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnNcHitTest(TMessage &Message)
{
  TPoint PT;
  PT.x=LOWORD(Message.LParam);
  PT.y=HIWORD(Message.LParam);
  PT=ScreenToClient(PT);
  RECT rect;
  ::SetRect(&rect,0,0,20,ClientHeight);
  if(PtInRect(&rect,PT))
  {
   Message.Result=HTCAPTION;
   //ShowMessage("How are you");
  }
  else
  {
   DefaultHandler(&Message);
  }
}
void __fastcall TForm1::FormPaint(TObject *Sender)
{
  RECT rect;
  ::SetRect(&rect,0,0,ClientWidth,ClientHeight);
  Canvas->Pen->Color=clBlue;
  Canvas->Brush->Color=clBlue;
  Canvas->Rectangle(0,0,20,ClientHeight);
  char *msg="My Fellow Citizens!";
  LOGFONT fontRect;
  memset(&fontRect,0,sizeof(LOGFONT));
  fontRect.lfHeight=-13;
  fontRect.lfWeight=FW_NORMAL;
  fontRect.lfEscapement=900;
  lstrcpy(fontRect.lfFaceName,"宋體");
  HFONT hFont=CreateFontIndirect(&fontRect);
  HFONT hOld=::SelectObject(Canvas->Handle,hFont);
  ::SetRect(&rect,0,0,20,ClientHeight);
  ::SetTextColor(Canvas->Handle,RGB(255,255,255));
  ::SetBkColor(Canvas->Handle,RGB(255,0,0));
  ::TextOut(Canvas->Handle,3,ClientHeight-3,msg,lstrlen(msg));
  ::SelectObject(Canvas->Handle,hOld);
  ::DeleteObject(hFont);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WndProc(TMessage &Message)
{
  if(Message.Msg==WM_SYSCOMMAND)
  {
   if(Message.WParam==SC_MAXIMIZE || Message.WParam==61490)
   {
    Invalidate();
   }
  }
  TForm::WndProc(Message);
}
void __fastcall TForm1::FormResize(TObject *Sender)
{
  Invalidate(); 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
  int button;
  button=Application->MessageBox("Do you want to exit thisform?", \
   "Confirmation",MB_YESNO+MB_ICONQUESTION);
  if(button==IDYES)
  {
   Application->Terminate();
  }
}

  1. 上一頁:
  2. 下一頁:
欄目導航
Copyright © 程式師世界 All Rights Reserved