首页 > 代码库 > XE6 c++builder Edit垂直居中
XE6 c++builder Edit垂直居中
class TMyEdit : public Vcl::Stdctrls::TEdit{ virtual void __fastcall CreateParams(TCreateParams& Params); void __fastcall WMSize(TWMSize &Message); virtual void __fastcall SetParent(TWinControl *AParent);public: __fastcall TMyEdit(TComponent* Owner); void __fastcall SetCenter(void); inline __fastcall virtual ~TMyEdit(void) { } inline __fastcall TMyEdit(HWND ParentWindow) : Vcl::Stdctrls::TEdit(ParentWindow) { } BEGIN_MESSAGE_MAP MESSAGE_HANDLER(WM_SIZE, TWMSize, WMSize); END_MESSAGE_MAP(TEdit);};
// ---------------------------------------------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit26.h"#include "Unit27.hpp"// ---------------------------------------------------------------------------#pragma package(smart_init)#pragma link "RzEdit"#pragma resource "*.dfm"TForm25 *Form25;// ---------------------------------------------------------------------------__fastcall TForm25::TForm25(TComponent* Owner) : TForm(Owner){}__fastcall TMyEdit::TMyEdit(TComponent* Owner) : TEdit(Owner){}void __fastcall TMyEdit::CreateParams(TCreateParams& Params){ TEdit::CreateParams(Params); Params.Style = Params.Style | ES_MULTILINE;}void __fastcall TMyEdit::SetParent(TWinControl *AParent){ TEdit::SetParent(AParent); if (AParent != NULL) SetCenter();}// ---------------------------------------------------------------------------void __fastcall TMyEdit::WMSize(TWMSize &Message){ TEdit::Dispatch(&Message); SetCenter();}void __fastcall TMyEdit::SetCenter(void){ HDC DC; HFONT SaveFont; int Sin; TTextMetric SysMetrics, Metrics; TRect Rct; DC = GetDC(0); GetTextMetrics(DC, &SysMetrics); SaveFont = (HFONT) SelectObject(DC, Font->Handle); GetTextMetrics(DC, &Metrics); SelectObject(DC, SaveFont); ReleaseDC(0, DC); if (Ctl3D) Sin = 8; else Sin = 6; Rct = ClientRect; Sin = Height - Metrics.tmHeight - Sin; Rct.Top = Sin / 2; SendMessage(Handle, EM_SETRECT, 0, (LPARAM)(&Rct));}// ---------------------------------------------------------------------------void __fastcall TForm25::Button7Click(TObject *Sender){ // 起作用 // 首先把控件的属性设置为多行 RECT rect; rect = myedt->ClientRect; // 设置内容的,左边距与上边距 ::OffsetRect(&rect, 0, 5); ::SendMessage(myedt->Handle, EM_SETRECT, 0, (LPARAM)&rect);}// ---------------------------------------------------------------------------void __fastcall TForm25::Button1Click(TObject *Sender){ SetWindowLong(myedt->Handle, GWL_STYLE, GetWindowLong(myedt->Handle, GWL_STYLE) | ES_CENTER);}// ---------------------------------------------------------------------------void __fastcall TForm25::FormCreate(TObject *Sender){ myedt = new TMyEdit(this); myedt->AutoSize = false; myedt->Height = 50; myedt->Parent = this; myedt->Text = "hello"; myedt->Alignment = taCenter;}// ---------------------------------------------------------------------------void __fastcall TForm25::Button4Click(TObject *Sender){ myedt->SetCenter(); // 起作用}// ---------------------------------------------------------------------------
原帖是delphi的,转自http://www.cnblogs.com/ntearn/archive/2012/07/25/2607570.html
控件创建后不起作用,没有效果。显示调用SetCenter函数就有效果了。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。