仿QQ自定义编辑框

外边框根据鼠标的移动显示出不同的颜色。内部的编辑区,则是偷懒使用的VCL的TEdit隐藏了边框,组合而成。基本上达到了QQ编辑框的效果。
#ifndef __TWINEDITCONTROL__
#define __TWINEDITCONTROL__
#include "gdi.h"
class TEditControl :public TCustomControl
{
   private:
      TCanvas * FCanvas;

      int m_left,m_top,m_width,m_height;
      Graphics::TBitmap * m_background_bmp;

      unsigned int m_background_color;//背景颜色
      unsigned int m_border_color;    //边缘颜色
      unsigned int m_text_color;      //文字颜色
      unsigned int window_color;      //设置背景色,用于控件透明,与窗口颜色相同
      unsigned int m_focus_color;     //焦点颜色

      TEdit * edit;

      bool isFocus;   //是否获得焦点
      bool isMoveLittle,isMoveBig;   //光标进入编辑区

      int m_in,m_out;        //计数器

      AnsiString  m_text;
      void __fastcall Proc(TMessage& Message);
   protected:
      void Draw();

      BEGIN_MESSAGE_MAP
          VCL_MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, MouseLeave)
          VCL_MESSAGE_HANDLER(CM_MOUSEENTER, TMessage, MouseEnter)
      END_MESSAGE_MAP(TCustomControl)

      void __fastcall MouseLeave(TMessage & msg);//鼠标离开控件
      void __fastcall MouseEnter(TMessage & msg);//鼠标进入控件

      void __fastcall TmyOnEnter(TObject  *Sender);
      void __fastcall TmyOnExit(TObject   *Sender);
      void __fastcall TmyOnChange(TObject *Sender);
      void __fastcall TmyOnClick(TObject  *Sender);

      void __fastcall setText(AnsiString value);
      AnsiString __fastcall getText();
   public:
      
      //功能函数
      void SetSize(TRect &r);
      void SetFont(AnsiString FontName,int size);
      void SelStart(int value);
      void SelLength(int value);
      AnsiString SelText();
      //构造函数
      __fastcall TEditControl(TComponent* Owner);
      //父类成员函数
      void __fastcall Paint(void);
      void __fastcall CreateParams(TCreateParams &Params);
      void __fastcall SetFocus(void);
      DYNAMIC void __fastcall MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
      DYNAMIC void __fastcall MouseMove(Classes::TShiftState Shift, int X, int Y);

      __property Graphics::TCanvas* Canvas = {read=FCanvas};
      __property Width;
      __property Height;
      __property Font;
      __property unsigned int BackgroundColor={read=m_background_color,write=m_background_color};
      __property unsigned int BorderColor    ={read=m_border_color,write=m_border_color};
      __property unsigned int TextColor      ={read=m_text_color,write=m_text_color};
      __property AnsiString Text={read=getText,write=setText};

};
 __fastcall  TEditControl::TEditControl(TComponent* Owner)
        : TCustomControl(Owner)
{
     TCustomControl * Win = (TCustomControl *)(Owner);
     this->Parent = Win;

     m_background_bmp =  new Graphics::TBitmap;

     FCanvas = TCustomControl::Canvas;//父类中的画布

     m_background_color = 0xffffffff;
     m_border_color     = 0xffC7C7C7;
     m_text_color       = 0xff000000;
     window_color       = 0xffff0000; //透明色
     m_focus_color      = 0xff1583DD;

     m_in  = 0;
     m_out = 0;

     isMoveLittle = false;
     isMoveLittle = false;
     
     GdiInit();
}
void __fastcall TEditControl::CreateParams(TCreateParams &Params)
{
     TWinControl::CreateParams(Params);
}
void TEditControl::SetSize(TRect &r)
{
    //控件大小
    this->Left   = r.left;
    this->Top    = r.top;
    this->Width  = r.Width();
    this->Height = r.Height();

    m_left = r.left;
    m_top  = r.top;
    m_width= r.Width();
    m_height=r.Height();

    m_background_bmp->Width = m_width;
    m_background_bmp->Height= m_height;
    //--------------------------------------------处理控件透明
    m_background_bmp->Canvas->Brush->Color = window_color;
    m_background_bmp->Canvas->FillRect(TRect(0,0,m_width,m_height));
    //--------------------------------------------
     int text_height = FCanvas->TextHeight("测试");
     edit = new TEdit(this);
     edit->Parent = this;

     edit->BorderStyle = bsNone;
     edit->Ctl3D       = false;
     
     edit->Left   = 5;
     edit->Top    = 0;
     edit->Width  = m_width-10;
     edit->Height = m_height;
     edit->Color   = clWhite;
     edit->OnEnter = TmyOnEnter;
     edit->OnExit  = TmyOnExit;
     edit->OnChange= TmyOnChange;
     edit->OnClick = TmyOnClick;
     edit->WindowProc = Proc;


     //-------------------------------
    Draw();
    Paint();
}
void TEditControl::Draw()
{
    GdiCreateHandle1(m_background_bmp->Canvas->Handle);

    GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,3,3);//圆角背景
    GdiPen(m_border_color,1);
    GdiDrawRoundRect(0,0,m_width-1,m_height-1,3,3);

    GdiReleaseGraphics();
}
void __fastcall TEditControl::Paint(void)
{
   TRect a(0,0,m_width,m_height);
   TRect b(0,0,m_width,m_height);

   m_background_bmp->Transparent = true;
   m_background_bmp->TransparentColor = window_color;

   FCanvas->Draw(0,0,m_background_bmp);

}
void __fastcall TEditControl::SetFocus(void)
{
    GdiCreateHandle1(m_background_bmp->Canvas->Handle);

    GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,3,3);//圆角背景
    GdiPen(m_border_color,1);
    GdiDrawRoundRect(0,0,m_width-1,m_height-1,3,3);

    GdiReleaseGraphics();

    Paint();
}
void __fastcall TEditControl::MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
{

}
//鼠标进入控件区
void __fastcall TEditControl::MouseEnter(TMessage & msg)
{

}
//鼠标离开控件区
void __fastcall TEditControl::MouseLeave(TMessage & msg)
{

}
void __fastcall TEditControl::TmyOnEnter(TObject * Sender)
{

}
void __fastcall TEditControl::TmyOnExit(TObject *Sender)
{
    isFocus = false;

    GdiCreateHandle1(m_background_bmp->Canvas->Handle);

    GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,3,3);//圆角背景
    GdiPen(m_border_color,1);
    GdiDrawRoundRect(0,0,m_width-1,m_height-1,3,3);

    GdiReleaseGraphics();

    Paint();

    edit->Invalidate();
}
void TEditControl::SetFont(AnsiString FontName,int size)
{
    Font->Name = FontName;
    Font->Size = size;
    edit->Font->Name = FontName;
    edit->Font->Size = size;
    edit->Invalidate();

    FCanvas->Font->Assign(Font);
    int text_height   = FCanvas->TextHeight("测试");
    edit->Top         = m_height/2-text_height/2;
    edit->Width       = m_width-10;
    edit->Height      = text_height+2;
}

void __fastcall TEditControl::MouseMove(Classes::TShiftState Shift, int X, int Y)
{

}
void __fastcall TEditControl::setText(AnsiString value)
{
   m_text = value;
   edit->Text = value;
   edit->Invalidate();
}
AnsiString __fastcall TEditControl::getText()
{
   return m_text;
}
void __fastcall TEditControl::TmyOnChange(TObject *Sender)
{
    m_text = edit->Text;
    edit->Invalidate();
}
//TEdit控件的子类过程
void __fastcall TEditControl::Proc(TMessage& Message)
{
    edit->Dispatch(&Message);
    if(Message.Msg  ==  CM_MOUSELEAVE)
    {
        if(!isFocus) //文本小区域
        {
            GdiCreateHandle1(m_background_bmp->Canvas->Handle);

            GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,3,3);//圆角背景
            GdiPen(m_border_color,1);
            GdiDrawRoundRect(0,0,m_width-1,m_height-1,3,3);

            GdiReleaseGraphics();

            edit->Invalidate();
            Paint();

            isFocus = false;
        }
    }
    if(Message.Msg  ==  CM_MOUSEENTER)
    {
       if(!isFocus )//文本区域
       {
          GdiCreateHandle1(m_background_bmp->Canvas->Handle);

          GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,3,3);//圆角背景
          GdiPen(m_focus_color,1);
          GdiDrawRoundRect(0,0,m_width-1,m_height-1,3,3);

          GdiReleaseGraphics();

          edit->Invalidate();
          Paint();

       }
    }
    if(Message.Msg == WM_KILLFOCUS)
    {
        GdiCreateHandle1(m_background_bmp->Canvas->Handle);

        GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,3,3);//圆角背景
        GdiPen(m_border_color,1);
        GdiDrawRoundRect(0,0,m_width-1,m_height-1,3,3);

        GdiReleaseGraphics();

        isFocus = false;
        Paint();

        edit->Invalidate();
    }
}
void __fastcall TEditControl::TmyOnClick(TObject  *Sender)
{
    GdiCreateHandle1(m_background_bmp->Canvas->Handle);

    GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,3,3);//圆角背景
    GdiPen(0xff1583DD,1);
    GdiDrawRoundRect(0,0,m_width-1,m_height-1,3,3);

    GdiPen(0xff9EC2DF,1);
    GdiDrawRoundRect(1,1,m_width-3,m_height-3,3,3);

    GdiReleaseGraphics();

    isFocus = true;

    Paint();
    edit->Invalidate();
}
//设置选中的长度
void TEditControl::SelStart(int value)
{
    edit->SelStart = value;
}
void TEditControl::SelLength(int value)
{
    edit->SelLength = value;
    edit->Invalidate();
}
AnsiString TEditControl::SelText()
{
   return edit->SelText;
}
#endif

仿QQ自定义编辑框_第1张图片

调用方法如下:

TEditControl * tControl;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
     tControl = new TEditControl(this);
     tControl->SetSize(TRect(100,150,300,175));
     tControl->SetFont("宋体",10);
     tControl->Text = "你好qwerqwr!";
}



你可能感兴趣的:(C++Builder6,图形图像)