自画标题栏的一个例子
当前位置:以往代写 > C/C++ 教程 >自画标题栏的一个例子
2019-06-13

自画标题栏的一个例子

自画标题栏的一个例子

.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();
  }
}

    关键字:

在线提交作业