MFC获取窗口大小

1、获取屏幕分辨率

//下边两个函数获取的是显示屏幕的大小,但不包括任务栏等区域
int cx = GetSystemMetrics(SM_CXFULLSCREEN);
int cy = GetSystemMetrics(SM_CYFULLSCREEN);
//下边这两个函数获取的是真正屏幕的大小:屏幕分辨率
int nWidth=GetSystemMetrics(SM_CXSCREEN);  //屏幕宽度    
int nHeight=GetSystemMetrics(SM_CYSCREEN); //屏幕高度
CString strScreen;
strScreen.Format(L"%d,%d",nWidth,nHeight);
MessageBox(strScreen);

2、获取对话框窗体大小及其屏幕坐标

//对话框窗体大小及其屏幕坐标
CRect rectDlg;
//法1:
GetClientRect(rectDlg);//获得窗体的大小
//法2:
//GetWindowRect(rectDlg);//获得窗体在屏幕上的位置
//ScreenToClient(rectDlg);
CString strDlg;
strDlg.Format(L"%d,%d,%d,%d",rectDlg.left,rectDlg.top,rectDlg.Width(),rectDlg.Height());
MessageBox(strDlg);

3、获取控件大小和位置

//控件大小和位置
CRect rectCtrl;
CStatic *p=(CStatic*)GetDlgItem(IDC_STATIC_TEST);
p->MoveWindow(100,100,100,100);//更改控件大小并移动其到指定位置
p->GetWindowRect(rectCtrl);
this->ScreenToClient(rectCtrl);
//GetDlgItem(IDC_STATIC_TEST)->GetClientRect(rectCtrl);
CString str;
str.Format(L"%d,%d,%d,%d",rectCtrl.left,rectCtrl.top,rectCtrl.Width(),rectCtrl.Height());
MessageBox(str);

总结:

1、GetSystemMetrics()  -->  用于得到被定义的系统数据或者系统配置信息;

2、GetClientRect() -->  得到客户区的位置和大小,一般用于获取窗口大小;

3、GetWindowRect() -->  得到窗口(对话框或控件)的屏幕坐标,一般用于获取对话框的屏幕坐标或和ScreenToClient()配合使用获取控件的客户区坐标;

4、ScreenToClient()  -->  屏幕坐标转换为客户区坐标,一般和GetWindowRect()配合使用获取控件的客户区坐标

猜你喜欢

转载自blog.csdn.net/xueluowutong/article/details/81324216