当前位置:网站首页>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 ); //编辑控件的大小和位置都改变以上方法也适用于各种窗口。
边栏推荐
猜你喜欢
随机推荐
ESP32 ESP-IDF看门狗TWDT
[postman] dynamic variable (also known as mock function)
2022 software testing workflow to know
IPv6 comprehensive experiment
【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
Luogu p1460 [usaco2.1] healthy Holstein cows
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
全链路压测:构建三大模型
【Postman】Monitors 监测API可定时周期运行
Detailed explanation of P problem, NP problem, NPC problem and NP hard problem
B - The Suspects
测试周期被压缩?教你9个方法去应对
联合索引的左匹配原则
Amazon Engineer: eight important experiences I learned in my career
JWT-JSON WEB TOKEN
Fault, error, failure of functional safety
Thoughts on data security (Reprint)
win10无法操作(删除、剪切)文件
isam2运行流程
黑猫带你学UFS协议第4篇:UFS协议栈详解