在妖哥的提示下继承TCustomForm来创建新的对话框。我原来的对话是将一个TForm包含在DLL调用,虽然也达到想要的效果,但是工作量很大。这个dll就有500多K,现在写成一个类,相对来说,就是轻量级的。配合GDI函数自绘,模拟win8对话框效果。
#ifndef __TFORMCONTROL__
#define __TFORMCONTROL__
#include "gdi.h"
class TFormControl :public TCustomForm
{
private:
int m_left;
int m_top;
int m_width;
int m_height;
TRect m_enter_rect; //确定按钮位置区域
TRect m_cancel_rect;//取消按钮位置区域
TRect m_close_rect; //关闭按钮位置区域
TRect m_ok_rect; //OK按钮位置区域
TRect m_form_rect;
unsigned int m_out_rectangle_color; //外边框
unsigned int m_in_rectangle_color; //内边框
bool m_mouse_move_close; //鼠标在关闭按钮上
bool m_mouse_move_enter; //鼠标是否在确定按钮上
Graphics::TBitmap * background_bmp;
Graphics::TBitmap * foreground_bmp;
AnsiString m_caption; //窗口标题
AnsiString m_text;
AnsiString m_icon_png; //小图标名称
protected:
void Draw();
void DrawCloseBox(bool value); //画关闭按钮
void DrawEnterBox(bool value); //画确定按钮
void DrawCaption(AnsiString value);//画标题
void DrawText(AnsiString value); //画文本
void DrawIcon(AnsiString value); //画图标
void SetText(AnsiString value); //设置文本
void SetIcon(AnsiString value); //设置图标
void SetCaption(AnsiString value); //设置标题
void UpdateBackground(int width,int hegiht); //更新背景
public:
__fastcall virtual TFormControl(TComponent * AOwner):TCustomForm(AOwner){};
__fastcall virtual TFormControl(TComponent * AOwner,int Dummy);
DYNAMIC void __fastcall Paint(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);
DYNAMIC void __fastcall MouseUp(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
__property Font;
__property AnsiString Text={read = m_text,write=SetText};
__property AnsiString Icon={read=m_icon_png,write= SetIcon};
__property AnsiString Tip ={read=m_caption,write=SetCaption};
};
__fastcall TFormControl::TFormControl(TComponent * AOwner,int Dummy):TCustomForm(AOwner,Dummy)
{
TScreen * s = new TScreen(this);
m_form_rect.left = 0;
m_form_rect.top = 0;
m_form_rect.right = 450;
m_form_rect.bottom= 200;
this->BorderStyle = bsNone;
this->Left = s->Width/2-m_form_rect.Width()/2;
this->Top = s->Height/2-m_form_rect.Height()/2;
this->Width = m_form_rect.Width();
this->Height= m_form_rect.Height();
m_left = this->Left;
m_top = this->Top;
m_width = this->Width;
m_height=this->Height;
//---------------------------------
m_close_rect.left = m_width - 8-40;
m_close_rect.top = 0;
m_close_rect.right = m_width - 9;
m_close_rect.Bottom = 20;
//---------------------------------
m_out_rectangle_color = 0xff6BADF7;
m_in_rectangle_color = 0xffF0F0F0;
//---------------------------------
m_enter_rect.left = m_form_rect.Width()/2 - 100/2;//确的按钮宽100
m_enter_rect.top = 150;
m_enter_rect.Right = m_form_rect.Width()/2 + 100/2;
m_enter_rect.bottom= 175;
//---------------------------------------------------------
m_caption = "对话框";
Font->Name = "微软雅黑";
Font->Size = 10;
m_icon_png = "icon.png";
//---------------------------------------------------------
background_bmp = new Graphics::TBitmap;
background_bmp->Width = this->Width;
background_bmp->Height= this->Height;
foreground_bmp = new Graphics::TBitmap;
foreground_bmp->Width = this->Width;
foreground_bmp->Height= this->Height;
//----------------------------------------------------------
GdiInit();
//----------------------------------------------------------
UpdateBackground(m_width,m_height);
GdiReleaseGraphics();
Draw();
}
void TFormControl::Draw()
{
foreground_bmp->Assign(background_bmp);
GdiCreateHandle1(foreground_bmp->Canvas->Handle);
//----------------------------------------------------------
//关闭按钮
DrawCloseBox(m_mouse_move_close);
//确认按钮
DrawEnterBox(m_mouse_move_enter);
//画标题
DrawCaption(m_caption);
//画文字
DrawTextA(m_text);
//画图标
DrawIcon(m_icon_png);
//----------------------------------------------------------
GdiReleaseGraphics();
}
void __fastcall TFormControl::Paint(void)
{
TRect a(0,0,m_width,m_height);
this->Canvas->CopyRect(a,foreground_bmp->Canvas,a);
}
void TFormControl::DrawCloseBox(bool value)
{
if(value)
{
foreground_bmp->Canvas->Brush->Color = 0x5050C7;
foreground_bmp->Canvas->FillRect(m_close_rect);
//画交叉线
foreground_bmp->Canvas->Pen->Color = clWhite;
foreground_bmp->Canvas->Pen->Width = 2;
foreground_bmp->Canvas->MoveTo(m_close_rect.left+15,m_close_rect.top+5);
foreground_bmp->Canvas->LineTo(m_close_rect.right-14,15);
foreground_bmp->Canvas->MoveTo(m_close_rect.left+25,m_close_rect.top+5);
foreground_bmp->Canvas->LineTo(m_close_rect.left+15,15);
}
else
{
foreground_bmp->Canvas->Brush->Color = 0xF7AD6B;
foreground_bmp->Canvas->FillRect(m_close_rect);
//画交叉线
foreground_bmp->Canvas->Pen->Color = clWhite;
foreground_bmp->Canvas->Pen->Width = 2;
foreground_bmp->Canvas->MoveTo(m_close_rect.left+15,m_close_rect.top+5);
foreground_bmp->Canvas->LineTo(m_close_rect.right-14,15);
foreground_bmp->Canvas->MoveTo(m_close_rect.left+25,m_close_rect.top+5);
foreground_bmp->Canvas->LineTo(m_close_rect.left+15,15);
}
}
void __fastcall TFormControl::MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
{
//关闭按钮被点击
if(m_mouse_move_close)
this->Close();
//确定按钮被点击
if(m_mouse_move_enter)
this->Close();
}
void __fastcall TFormControl::MouseMove(Classes::TShiftState Shift, int X, int Y)
{
//判断是否在关闭区域
bool re = PtInRect(m_close_rect,TPoint(X,Y));
if(re)
m_mouse_move_close = true;
else
m_mouse_move_close = false;
//判断是否点击确的按钮
re = PtInRect(m_enter_rect,TPoint(X,Y));
if(re)
m_mouse_move_enter = true;
else
m_mouse_move_enter = false;
DrawText(m_text);
//重绘图形并显示
Draw();
Paint();
}
void __fastcall TFormControl::MouseUp(TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
{
}
void TFormControl::DrawEnterBox(bool value)
{
if(value)
foreground_bmp->Canvas->Pen->Color = 0xF7AD6B; //鼠标在确定按钮上
else
foreground_bmp->Canvas->Pen->Color = 0xACACAC;
foreground_bmp->Canvas->Brush->Color = 0xEAEAEA;
foreground_bmp->Canvas->Pen->Width = 1;
foreground_bmp->Canvas->FillRect(m_enter_rect);
foreground_bmp->Canvas->Rectangle(m_enter_rect);
foreground_bmp->Canvas->Font->Assign(Font);
long w = foreground_bmp->Canvas->TextWidth("确定");
long h = foreground_bmp->Canvas->TextHeight("确定")+2;
foreground_bmp->Canvas->TextOutA(m_enter_rect.left+m_enter_rect.Width()/2-w/2,m_enter_rect.top+m_enter_rect.Height()/2-h/2,"确定");
}
void TFormControl::DrawCaption(AnsiString value)
{
//标题位置
int w = foreground_bmp->Canvas->TextWidth(value);
int m_l = m_width/2-w/2;
int m_t = 6;
foreground_bmp->Canvas->Font->Assign(Font);
foreground_bmp->Canvas->Brush->Style = bsClear;
foreground_bmp->Canvas->Font->Color = clBlack;
foreground_bmp->Canvas->TextOutA(m_l,m_t,m_caption);
}
void TFormControl::DrawText(AnsiString value)
{
foreground_bmp->Canvas->Font->Assign(Font);
foreground_bmp->Canvas->Brush->Style = bsClear;
foreground_bmp->Canvas->Font->Color = clBlack;
foreground_bmp->Canvas->TextOutA(100,70,m_text);
}
void TFormControl::SetText(AnsiString value)
{
m_text = value;
int w = foreground_bmp->Canvas->TextWidth(value);
this->Width = w+200;
m_width = w+200;
UpdateBackground(m_width,m_height);
Draw();
Paint();
}
void TFormControl::SetIcon(AnsiString value)
{
m_icon_png = value;
Draw();
Paint();
}
void TFormControl::DrawIcon(AnsiString value)
{
if(FileExists(value))
{
GdiDrawImage(value,30,70,40,40);
}
}
void TFormControl::SetCaption(AnsiString value)
{
m_caption = value;
Draw();
Paint();
}
void TFormControl::UpdateBackground(int width,int height)
{
background_bmp->Width = width;
background_bmp->Height= height;
foreground_bmp->Width = width;
foreground_bmp->Height= height;
//---------------------------------------------------------
GdiCreateHandle1(background_bmp->Canvas->Handle);
GdiFillRectSolidBrush(m_out_rectangle_color,0,0,m_width,m_height);//外框
GdiFillRectSolidBrush(m_in_rectangle_color,7,30,m_width-16,m_height-37);//内矩形
//----------------------------------------------------------
GdiPen(0xff53A7B4,1);
GdiDrawRectangle(0,0,m_width-2,m_height-2); //外框线
GdiDrawRectangle(7,30,m_width-16,m_height-37); //内框线
//----------------------------------------------------------
//确定按钮位置更新
m_enter_rect.left = m_width/2 - 100/2;//确的按钮宽100
m_enter_rect.top = 150;
m_enter_rect.Right = m_width/2 + 100/2;
m_enter_rect.bottom= 175;
//关闭按钮位置更新
m_close_rect.left = m_width - 8-40;
m_close_rect.top = 0;
m_close_rect.right = m_width - 9;
m_close_rect.Bottom = 20;
}
//类似vcl风格的调用对话框函数
long ShowMessageBox(TForm * f,AnsiString str,AnsiString tip,AnsiString icon)
{
TFormControl * dialog = new TFormControl(f,0);
dialog->Text = str;
dialog->Tip = tip;
dialog->Icon = icon;
dialog->ShowModal();
delete dialog;
}
#endif
调用方法如下:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessageBox(this,"请选择您的设备型号","设备型号","icon.png");
}
显示如下图: