首页 > 编程语言 > 详细

XE6 c++builder Edit垂直居中

时间:2014-08-19 16:27:14      阅读:543      评论:0      收藏:0      [点我收藏+]
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函数就有效果了。

XE6 c++builder Edit垂直居中,布布扣,bubuko.com

XE6 c++builder Edit垂直居中

原文:http://www.cnblogs.com/cb168/p/3922156.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!