📄 vc++50编程经验.htm
字号:
</big><font size="3">int* pInt=(int*)pv;//强制转换指针类型</font><big> <br></big><font size="3">num=*(pInt++);</font><big> <br></big><font size="3">double fd;</font><big> <br></big><font size="3">double *pDouble=(double*)pInt;</font><big> <br></big><font size="3">fd=*(pDouble++)</font><big> <br></big><font size="3">......</font><big> <br></big><font size="3">return TRUE;</font><big> <br></big><font size="3">}</font><big> <br> <br> </big></p><h4 align="center"><font size="3">二、创建可伸缩对话框</font></h4><p><big><br></big><font size="3">在进行对话框设计时,根据实际需要有时要设计成可以伸缩的对话框:当按钮按下时,对话框伸展成如图1所示的样子;再一次按下该按钮,则对话框收缩成如图2所示的样子。</font><big> <br><img SRC="imageJIS.JPG" WIDTH="514" HEIGHT="411"> <br> 图1 <br><img SRC="imageUIJ.JPG" WIDTH="279" HEIGHT="409"> <br> 图2 </big></p><p align="left"><font size="3">实现步骤如下:</font><big> <br></big><font size="3">(1)首先在资源文件中建立对话框控件,然后将一个Picture控件的ID设置为IDC_DIVIDER,Type设置为Rectangle,Color设置为Black,并将其设定为一线状,将其放在对话框的中间部位,属性设置为不可见,如图3所示。</font></p><p align="center"><font size="3"><img src="f3.gif" alt="f3.gif (4762 bytes)" WIDTH="522" HEIGHT="186"></font></p><p align="center"><font size="3">图3</font></p><p align="left"><font size="3">(2)然后执行程序代码。程序的实现原理是:首先获取对话框的尺寸大小,即RECT(left,right,top,bottom),然后根据IDC_DIVIDER的相对位置确定缩减后的对话框尺寸。其实在left、right、top、bottom四个参数中,缩减后的对话框与扩展后的对话框在尺寸上的唯一不同之处是right值。缩减后的对话框其right参数正好由IDC_DIVIDER来确定。对于缩减后的对话框,在原来对话框中不可见的控件应该被禁止,以禁止加速键和TAB键对控件的操作。而当对话框扩展后,原来被禁止的对话框控件又要使能。相应的程序代码可参考下面的EnableVisibleChildren()函数。明白了这个原理,程序的设计就很简单了。相应的程序代码段如下:<br>void CExpand::ExpandDialog(int nResourceID,BOOL bExpand)<br>{<br> //Expand the dialog to full size if bExpand is<br> //TRUE;otherwise contract it with the new bottom<br> //to the bottom of the divider control specified<br> //in nResourceID<br> static CRect rcLarge;<br> static CRect rcSmall;<br> //First time through,save the dialog's large<br> //and small sizes<br> if(rcLarge.IsRectNull())<br> {<br> CRect rcLandmark;<br> CWnd *pWndLandmark=GetDlgItem(nResourceID);<br> ASSERT(pWndLandmark);<br> GetWindowRect(rcLarge);<br> pWndLandmark->GetWindowRect(rcLandmark);<br> rcSmall=rcLarge;<br> rcSmall.right=rcLandmark.right;<br> }<br> if(bExpand)<br> {<br> //Expand the dialog;resize the dialog<br> //to its original size(rcLarge)<br> SetWindowPos(NULL,0,0,rcLarge.Width(),<br> rcLarge.Height(),<br> SWP_NOMOVE|SWP_NOZORDER);<br> EnableVisibleChildren();<br> }<br> else<br> {<br> //Contract the dialog to the small size<br> SetWindowPos(NULL,0,0,rcSmall.Width(),<br> rcSmall.Height(),<br> SWP_NOMOVE|SWP_NOZORDER);<br> EnableVisibleChildren();<br> }<br>}<br>void CExpand::EnableVisibleChildren()<br>{<br> //Disalbe all children not in the current<br> //dialog.This prevents tabbing to hidden<br> //controls and disable accelerators<br> //Get the first child control<br> CWnd *pWndCtrl=GetWindow(GW_CHILD);<br> CRect rcTest;<br> CRect rcControl;<br> CRect rcshow;<br> GetWindowRect(rcShow);<br> while(pWndCtrol!=NULL)<br> {<br> pWndCtrl->GetWindowRect(rcControl);<br> if(rcTest.IntersectRect(rcShow,rcControl))<br> pWndCtrl->EnableWindow(TRUE);<br> else<br> pWndCtrl->EnableWindow(FALSE);<br> //Get the next sibling window in this chain<br> pWndCtrl=pWndCtrl->GetWindow(GW_HWNDNEXT);<br> }<br>}</font></p><h4 align="center"><font size="3">三、创建风格独特的位图控件</font></h4><p align="left"><font size="3">如图4所示,无边框的对话框显示的是一幅自绘的位图图像,上面的“确定”及“取消”也完全是自绘的,那么是否能够做到当用鼠标单击“确定”及“取消”按钮时应用程序能够正确响应鼠标消息呢?答案是肯定的。</font></p><p align="left"><font size="3">实现步骤如下:<br>(1)在资源编辑器中创建对话框资源,将所有的对话框属性设置为Disable;<br>(2)在对话框中放置Picture控件,并将该控件属性页中的Type设置为Bitmap,然后选择一个合适的位图ID号;<br>(3)在“确定”和“取消”处放置两个Static Text控件,使其大小恰好包括两个圆圈为宜,并设置其中的一个控件的ID为IDOK,另一个为IDCANCEL,两个控件都设置为不可见,并且在控件的属性设定中允许Notify项,以使Static Text控件响应鼠标消息。<br>(4)然后在对话框类实现代码中重载成员函数OnOK()和OnCancel(),并在其中加入单击两个控件时的动作代码。<br>这样,当鼠标进入“确定”所包围的区域时,实际上进入了ID号为IDOK的Static Text控件所包围的区域。相应地,应用程序的该对话框类就会收到标识为IDOK的消息,并且引导程序进入重载的成员函数OnOK()中并执行相应的代码。如果没有重载成员函数OnOK(),则执行缺省的成员函数OnOK()。“取消”的处理与此相类似。<br>另外,如果重载了框架类CMainFrame的成员函数OnClose(),并且将上述的对话框类CExitDlg放在其中,则在应用程序退出时会出现此框,提醒用户是真的的要退出还是偶然的误操作,代码如下:<br></font>void CMainFrame::OnClose()<br>{<br> CExitDlg exitdlg;<br> if(exitdlg.DoModal()==IDCANCEL)<br> return;<br> CFrameWnd::OnClose();<br>}</p><p ALIGN="CENTER"><font face="黑体" lang="ZH-CN" size="5">回到<a href="../chinese.htm">中文教材</a></font></p><p> </p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -