MFC 根据窗口的变化来设置控件的大小

来源:互联网 发布:泛微oa系统 知乎 编辑:程序博客网 时间:2024/05/22 15:53

不过这个首先必须是你自己新建的控件,比如:

CEdit*p_edit = NULL,*p_url = NULL;
CStatic *p_picture = NULL;

p_edit = new CEdit;
p_url = new CEdit;
CRect rect;
this->GetClientRect(&rect);
rect.SetRect(rect.left,rect.bottom-200,rect.right,rect.bottom);
p_edit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_WANTRETURN | ES_READONLY | WS_VSCROLL ,
rect,this,IDC_EDIT_LOG);
rect.SetRect(rect.left+200,rect.top-40,rect.right-10,rect.top-20);
p_url->Create(WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL, rect ,this,IDC_EDIT_URL);


this->GetClientRect(&rect);
rect.SetRect(rect.left,rect.top+50,rect.right,rect.top+150);
p_picture = new CStatic;
p_picture->Create("",WS_VISIBLE|SS_BITMAP,rect,this,IDC_STATIC_PICTURE);
SwitchJpegToBmp();
HBITMAP h_bitmap = HBMP("./res/res.bmp",rect.right,50);
p_picture->SetBitmap(h_bitmap);

这些初始化完后,我们可以重载OnSize函数,

void CSelLessonDlg::OnSize(UINT nType, int cx, int cy) 
{
CDialog::OnSize(nType, cx, cy);
if(p_edit != NULL && p_url != NULL)
{
CRect rect;
this->GetClientRect(&rect);
rect.SetRect(rect.left,rect.bottom-200,rect.right,rect.bottom);
p_edit->MoveWindow(&rect);
rect.SetRect(rect.left+200,rect.top-40,rect.right-10,rect.top-20);
p_url->MoveWindow(&rect);
}
// TODO: Add your message handler code here

}

一点要记住初始化那几个控件的指针变量为NULL;

这里再拓展一点:

我们可以用p_edit->SetWindowText(m_logbuff);来显示内容;

用p_edit->GetWindowText();来获取输入的内容;

也可以设置字体,字体:

CFont LFont;
LFont.CreateFont(15, 7, 0, 0, FW_DONTCARE,  
        false,FALSE,FALSE,0,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_ROMAN,_T("courier new"));

p_url->SetFont(&LFont,false);

0 0
原创粉丝点击