当前位置:网站首页>MFC 动态创建的对话框及改变控件的大小和位置
MFC 动态创建的对话框及改变控件的大小和位置
2022-07-06 06:09:00 【老白436196571】
1、动态创建对话框
void CXXX_DEMODlg::OnBnClickedBtntest()
{
if(dlg_num==0)//只打开一次
{
DLG_TEST *pDlg=new DLG_TEST;
pDlg->Create(IDD_DLG_TEST, this);//一直置顶
//pDlg->Create(IDD_DLG_TEST, GetDesktopWindow());
pDlg->ShowWindow(SW_SHOW);
dlg_num+=1;
}
}
2、删除指针在WM_NCDESTROY里进行delete this
void DLG_TEST::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
dlg_num-=1;
OnCancel();
}
void DLG_TEST::OnNcDestroy()
{
CDialog::OnNcDestroy();
// TODO: 在此处添加消息处理程序代码
delete this;
}
CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置。
void MoveWindow(int x,int y,int nWidth,int nHeight);
void MoveWindow(LPCRECT lpRect);
第一种用法需给出控件新的坐标和宽度、高度;
第二种用法给出存放位置的CRect对象;
例:
CWnd *pWnd;pWnd = GetDlgItem( IDC_EDIT1 ); //获取控件指针,IDC_EDIT1为控件ID号
pWnd->MoveWindow( CRect(0,0,100,100) ); //在窗口左上角显示一个宽100、高100的编辑控件
SetWindowPos()函数使用更灵活,多用于只修改控件位置而大小不变或只修改大小而位置不变的情况:
BOOL SetWindowPos(const CWnd* pWndInsertAfter,int x,int y,int cx,int cy,UINT nFlags);
第一个参数我不会用,一般设为NULL;
x、y控件位置;cx、cy控件宽度和高度;
nFlags常用取值:
SWP_NOZORDER:忽略第一个参数:
SWP_NOMOVE:忽略x、y,维持位置不变;
SWP_NOSIZE:忽略cx、cy,维持大小不变;
例:
CWnd *pWnd;pWnd = GetDlgItem( IDC_BUTTON1 ); //获取控件指针,IDC_BUTTON1为控件ID号
pWnd->SetWindowPos( NULL,50,80,0,0,SWP_NOZORDER | SWP_NOSIZE ); //把按钮移到窗口的(50,80)处
pWnd = GetDlgItem( IDC_EDIT1 );
pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER | SWP_NOMOVE ); //把编辑控件的大小设为(100,80),位置不变
pWnd = GetDlgItem( IDC_EDIT1 );
pWnd->SetWindowPos( NULL,0,0,100,80,SWP_NOZORDER ); //编辑控件的大小和位置都改变以上方法也适用于各种窗口。
边栏推荐
猜你喜欢
随机推荐
Function of activation function
Online and offline problems
功能安全之故障(fault),错误(error),失效(failure)
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
【API接口工具】postman-界面使用介绍
Gtest之TEST宏的用法
IPv6 comprehensive experiment
黑猫带你学UFS协议第4篇:UFS协议栈详解
[postman] dynamic variable (also known as mock function)
LeetCode 731. 我的日程安排表 II
D - How Many Answers Are Wrong
A complete collection of necessary learning websites for office programmers
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
Request forwarding and redirection
联合索引的左匹配原则
【Postman】Collections配置运行过程
ESP32 ESP-IDF看门狗TWDT
[web security] nodejs prototype chain pollution analysis
全链路压测:构建三大模型
How to use the container reflection method encapsulated by thinkphp5.1 in business code









