MFC 改变控件的大小和位置

mfc 改变控件大小和位置用到的函数:

1 1) void MoveWindow(int x, int y, int nWidth, int nHeight);
2 2) void MoveWindow(LPCRECT lpRect);

第1)个函数用法需给出控件的新的坐标和宽度、高度;

第2)个函数用法需给出存放位置的CRect对象;

例如:

CWnd *pWnd = NULL;
pWnd = GetDlgItem(IDC_EDIT1); //获取控件指针,IDC_EDIT1为控件ID号
pWnd->MoveWindow(CRect(0, 0, 100, 100));//在窗口左上角显示一个宽100、高100的编辑控件

  

SetWindowPos()函数使用更灵活,多用于只修改控件位置而大小不变或只修改大小而位置不变的情况:

1 BOOL SetWindowPos(const CWnd* pWndInsertAfter,int x,int y,int cx,int cy,UINT nFlags);
2 第一个参数一般设为NULL;
3 x、y控件位置;
4 cx、cy控件宽度和高度;
5 nFlags常用取值:
6 SWP_NOZORDER:忽略第一个参数;
7 SWP_NOMOVE:忽略x、y,维持位置不变;
8 SWP_NOSIZE:忽略cx、cy,维持大小不变;

例:

1 CWnd *pWnd = NULL;
2 CWnd *pWnd;
3 pWnd = GetDlgItem( IDC_BUTTON1 ); //获取控件指针,IDC_BUTTON1为控件ID号
4 pWnd->SetWindowPos( NULL,50,80,0,0,SWP_NOZORDER | SWP_NOSIZE ); //把按钮移到窗口的(50,80)处
5 pWnd = GetDlgItem( IDC_EDIT1 );
6 pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER | SWP_NOMOVE ); //把编辑控件的大小设为(100,80),位置不变
7 pWnd = GetDlgItem( IDC_EDIT1 );
8 pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER ); //编辑控件的大小和位置都改变
9 以上方法也适用于各种窗口。

 

posted on 2016-07-29 13:46  新月时刻  阅读(348)  评论(0编辑  收藏  举报

导航