⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vc++50编程经验.htm

📁 VC的一些技巧性文档
💻 HTM
📖 第 1 页 / 共 2 页
字号:
</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>&nbsp; <br>&nbsp; </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 图1 <br><img SRC="imageUIJ.JPG" WIDTH="279" HEIGHT="409"> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 图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>&nbsp;&nbsp;&nbsp; //Expand the dialog to full size if bExpand is<br>&nbsp;&nbsp;&nbsp; //TRUE;otherwise contract it with the new bottom<br>&nbsp;&nbsp;&nbsp; //to the bottom of the divider control specified<br>&nbsp;&nbsp;&nbsp; //in nResourceID<br>&nbsp;&nbsp;&nbsp; static CRect rcLarge;<br>&nbsp;&nbsp;&nbsp; static CRect rcSmall;<br>&nbsp;&nbsp;&nbsp; //First time through,save the dialog's large<br>&nbsp;&nbsp;&nbsp; //and small sizes<br>&nbsp;&nbsp;&nbsp; if(rcLarge.IsRectNull())<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CRect rcLandmark;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CWnd *pWndLandmark=GetDlgItem(nResourceID);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ASSERT(pWndLandmark);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetWindowRect(rcLarge);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pWndLandmark-&gt;GetWindowRect(rcLandmark);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rcSmall=rcLarge;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rcSmall.right=rcLandmark.right;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; if(bExpand)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Expand the dialog;resize the dialog<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //to its original size(rcLarge)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetWindowPos(NULL,0,0,rcLarge.Width(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rcLarge.Height(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SWP_NOMOVE|SWP_NOZORDER);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EnableVisibleChildren();<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Contract the dialog to the small size<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetWindowPos(NULL,0,0,rcSmall.Width(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rcSmall.Height(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SWP_NOMOVE|SWP_NOZORDER);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EnableVisibleChildren();<br>&nbsp;&nbsp;&nbsp; }<br>}<br>void CExpand::EnableVisibleChildren()<br>{<br>&nbsp;&nbsp;&nbsp; //Disalbe all children not in the current<br>&nbsp;&nbsp;&nbsp; //dialog.This prevents tabbing to hidden<br>&nbsp;&nbsp;&nbsp; //controls and disable accelerators<br>&nbsp;&nbsp;&nbsp; //Get the first child control<br>&nbsp;&nbsp;&nbsp; CWnd *pWndCtrl=GetWindow(GW_CHILD);<br>&nbsp;&nbsp;&nbsp; CRect rcTest;<br>&nbsp;&nbsp;&nbsp; CRect rcControl;<br>&nbsp;&nbsp;&nbsp; CRect rcshow;<br>&nbsp;&nbsp;&nbsp; GetWindowRect(rcShow);<br>&nbsp;&nbsp;&nbsp; while(pWndCtrol!=NULL)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pWndCtrl-&gt;GetWindowRect(rcControl);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(rcTest.IntersectRect(rcShow,rcControl))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pWndCtrl-&gt;EnableWindow(TRUE);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pWndCtrl-&gt;EnableWindow(FALSE);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Get the next sibling window in this chain<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pWndCtrl=pWndCtrl-&gt;GetWindow(GW_HWNDNEXT);<br>&nbsp;&nbsp;&nbsp; }<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>&nbsp;&nbsp;&nbsp; CExitDlg exitdlg;<br>&nbsp;&nbsp;&nbsp; if(exitdlg.DoModal()==IDCANCEL)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>&nbsp;&nbsp;&nbsp; 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 + -