自己在写MFC小工具时的一点小小的心得:
1. 列表控件
(1)列表控件有报告、图标、列表等多种显示风格,如果想像Excel那样显示风格,则必须选择报告风格;
(2)在往列表框里填写数据的时候,必须先插入列才行。
2. 树形控件
3. 关于控件随着窗口 的变化而改变大小和位置
只要重载整个对话框的OnSize(UINT nType, int cx, int cy)函数,在函数里加入处理位置和大小的代码即可。代码大概可以分为以下几个大的步骤:(假设存在6个控件)
void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CWnd *pStatic, *pEdit,*pButton,*pTree,*pCtrlList,*pProcess;
CRect rect,rStatic, rEdit,rButton,rTree,rCtrlList,rProcess;
//获取各控件的句柄
pStatic = GetDlgItem(IDC_STATIC);
pEdit = GetDlgItem(IDC_EDIT1);
pButton = GetDlgItem(IDC_BUTTON1);
pTree = GetDlgItem(IDC_TREE1);
pCtrlList = GetDlgItem(IDC_LIST1);
pProcess = GetDlgItem(IDC_PROGRESS1);
//检查各句柄都存在,因为创建对话框时也会调用此函数
if (pStatic->GetSafeHwnd() && pEdit->GetSafeHwnd() && pButton->GetSafeHwnd()
&& pTree->GetSafeHwnd() && pCtrlList->GetSafeHwnd() && pProcess->GetSafeHwnd())
{
GetClientRect(&rect);//获取整个对话框的客户区
//获取各控件的客户区,保存在矩形变量中
pStatic->GetClientRect(&rStatic);
pEdit->GetClientRect(&rEdit);
pButton->GetClientRect(&rButton);
pTree->GetClientRect(&rTree);
pCtrlList->GetClientRect(&rCtrlList);
pProcess->GetClientRect(&rProcess);
//设定各矩形的位置大小
rStatic.left = rect.left+15;
rStatic.right = rect.Width()/10;
rStatic.top =rect.top+10;
rStatic.bottom = rect.top+40;
rEdit.left = rStatic.right;
rEdit.right = rect.Width()*4/5;
rEdit.top =rect.top+8;
rEdit.bottom = rect.top+35;
rButton.left = rEdit.right+20;
rButton.right = rect.right-20;
rButton.top =rect.top+8;
rButton.bottom = rButton.top+30;
rTree.left = rect.left;
rTree.right = rect.Width()*1/5;
rTree.top =rStatic.bottom+10;
rTree.bottom = rect.bottom-20;
rCtrlList.left = rTree.right;
rCtrlList.right = rect.right;
rCtrlList.top = rTree.top;
rCtrlList.bottom = rTree.bottom;
rProcess.left = rect.left;
rProcess.right = rect.right;
rProcess.top =rTree.bottom;
rProcess.bottom = rect.bottom;
//重新设置各控件的位置
pStatic->MoveWindow(&rStatic);
pEdit->MoveWindow(&rEdit);
pButton->MoveWindow(&rButton);
pTree->MoveWindow(&rTree);
pCtrlList->MoveWindow(&rCtrlList);
pProcess->MoveWindow(&rProcess);
//重绘各控件
pStatic->RedrawWindow();
pEdit->RedrawWindow();
pButton->RedrawWindow();
pTree->RedrawWindow();
pCtrlList->RedrawWindow();
pProcess->RedrawWindow();
}
}
4. 让对话框可以在边缘拖动改变大小
在OnInitDialog()函数中加入一个语句即可:ModifyStyle(NULL, WS_THICKFRAME);
5. 让MFC对话框有最大最小化按钮:在整个对话框的属性中把最大最小化复选框选上即可。