- 浏览: 60954 次
- 性别:
- 来自: 株洲
文章分类
最新评论
-
yuvista:
礼顶膜拜,我心中的大神!一个在生活中迷失的程序员向您致敬!
一个程序员的奋斗历程(再发经典,这是我见过最牛的程序员了
Introduction
Recently I've been reading up on WTL, and I came across a rather interesting class that I hadn't seen mentioned anywhere, CDialogResize
. Given the large number of MFC implementations of resizable dialogs, it's great that WTL provides its own, so you only have to learn one class and one way of specifying what gets resized. In this article, I will outline WTL's resizing support and provide a sample program to illustrate some of the features. You should already be familiar with WTL and how to install it. If you need help with this, there are articles on those subjects in the WTL section.
Using CDialogResize
The basics
As with many other WTL features, you begin by adding CDialogResize
to the inheritance list of your dialog class. So, if you make a dialog-based app with the WTL AppWizard, you would add the line listed here in red:
class CMainDlg : public CAxDialogImpl<CMainDlg>, public CDialogResize<CMainDlg>
CDialogResize
is declared in atlframe.h, so add that header to your includes if it isn't there already.
The next step is to initialize the CDialogResize
code in your dialog's OnInitDialog
handler:
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { // Init the CDialogResize code DlgResize_Init(); ... }
DlgResize_Init()
has a few optional parameters, which I'll cover later.
Next, add an entry in the dialog's message map that passes sizing messages to CDialogResize
:
BEGIN_MSG_MAP(CMainDlg) MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog) ... CHAIN_MSG_MAP(CDialogResize<CMainDlg>) END_MSG_MAP()
Finally, add a new map to the dialog class that lists which controls in the dialog will be resized:
class CMainDlg : public CAxDialogImpl<CMainDlg>, public CDialogResize<CMainDlg> { ... public: BEGIN_DLGRESIZE_MAP(CMainDlg) END_DLGRESIZE_MAP() ... };
I'll describe how to fill in this map in the "Setting up the resize map" section.
Initializing CDialogResize
CDialogResize
is initialized by calling to DlgResize_Init()
. Its prototype is:
void CDialogResize::DlgResize_Init ( bool bAddGripper = true, bool bUseMinTrackSize = true, DWORD dwForceStyle = WS_THICKFRAME | WS_CLIPCHILDREN );
The parameters are:
-
bAddGripper
: This controls whetherCDialogResize
adds a size box to the bottom-right corner of the dialog. Passtrue
to add the size box, orfalse
to not add it. -
bUseMinTrackSize
: This parameter controls whetherCDialogResize
restricts the minimum size of the dialog. If you passtrue
, the dialog is not allowed to be sized smaller than its initial size (as stored in the resource file). Passfalse
if you don't want to restrict the dialog's minimum size. -
dwForceStyle
: Specifies window styles to apply to the dialog. The default value is usually sufficient.
Setting up the resize map
The dialog resize map tells CDialogResize
which controls to move or size. An entry looks like this:
DLGRESIZE_CONTROL(ControlID, Flags)
ControlID
is the ID of the dialog control. The possible flags and their meanings are:
-
DLSZ_SIZE_X
: Resize the width of the control as the dialog resizes horizontally. -
DLSZ_SIZE_Y
: Resize the height of the control as the dialog resizes vertically. -
DLSZ_MOVE_X
: Move the control horizontally as the dialog resizes horizontally. -
DLSZ_MOVE_Y
: Move the control vertically as the dialog resizes vertically. -
DLSZ_REPAINT
: Invalidate the control after every move/resize so it repaints every time.
Note that you cannot move and size a control in the same dimension. If you specify, say DLSZ_MOVE_X
and DLSZ_SIZE_X
together, the size flag is ignored.
You can also group controls together so that they move and size proportionally to each other. I will cover this subject later.
Sample Project
The sample project included with this article is a simple dialog-based app that functions as a browser (using the WebBrowser ActiveX control). The control IDs are listed below; you should refer back to this diagram later when I discuss moving and resizing these controls.
The controls will move and size according to these rules:
- The Location edit box will resize horizontally.
- The Go, Exit, and About buttons will move horizontally.
- The Back, Forward, Stop, and Refresh buttons will resize horizontally in a group.
- The browser control will resize horizontally and vertically
The browser's OnInitDialog()
function initializes CDialogResize
like this:
LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) { // Init the CDialogResize code DlgResize_Init(); ... }
This uses the default parameters to DlgResize_Init()
, which results in a size box being added. Also, the dialog cannot be sized smaller than its initial size. Here's what the dialog looks like on startup:
Here is the resize map, which lists the moving and sizing behavior for the controls. Note the new macros - BEGIN_DLGRESIZE_GROUP()
and END_DLGRESIZE_GROUP()
- that put the four browser control buttons into a resizing group.
BEGIN_DLGRESIZE_MAP(CMainDlg) // Location edit box DLGRESIZE_CONTROL(IDC_URL, DLSZ_SIZE_X) // Go, Exit, About buttons DLGRESIZE_CONTROL(IDC_GO, DLSZ_MOVE_X) DLGRESIZE_CONTROL(IDC_EXIT, DLSZ_MOVE_X) DLGRESIZE_CONTROL(ID_APP_ABOUT, DLSZ_MOVE_X) // IE control buttons BEGIN_DLGRESIZE_GROUP() DLGRESIZE_CONTROL(IDC_BACK, DLSZ_SIZE_X) DLGRESIZE_CONTROL(IDC_FORWARD, DLSZ_SIZE_X) DLGRESIZE_CONTROL(IDC_STOP, DLSZ_SIZE_X) DLGRESIZE_CONTROL(IDC_REFRESH, DLSZ_SIZE_X) END_DLGRESIZE_GROUP() // WebBrowser control DLGRESIZE_CONTROL(IDC_BROWSER, DLSZ_SIZE_X|DLSZ_SIZE_Y) END_DLGRESIZE_MAP()
Here is the dialog after being resized:
Notice how the edit box is wider, and the browser control is wider and taller. The behavior of the four grouped buttons is a bit tough to put into words, and the WTL code offers little guidance since there are few comments. But the basic idea is: imagine a bounding rectangle that surrounds all four buttons. That rectangle resizes like any other control, and all the buttons are sized proportionally so they remain within the bounding rectangle. If the buttons were to be moved, instead of resized, they would be positioned to always be evenly spaced apart. Note that all the controls in a group should have the same DLSZ_*
flags to produce meaningful behavior.
Bugs and Problems with CDialogResize
So far, I've only seen two problems. One is that there seems to be an off-by-one bug somewhere, because the first time you resize the dialog, some of the controls shift the wrong direction by one pixel. The other, more serious problem, is a bug in the WTL AppWizard that is exposed when you add CDialogResize
as a base class of your dialog. The AppWizard-generated code that displays the dialog looks like this:
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow) { // ... CMainDlg dlgMain; int nRet = dlgMain.DoModal(); _Module.Term(); ::CoUninitialize(); return nRet; }
The trouble with that is the CMainDlg
destructor is called after _Module.Term()
. This causes a crash in a release build if it is built with the _ATL_MIN_CRT
symbol defined. The solution is to put the CMainDlg
object in an enclosing block:
int nRet;
{
CMainDlg dlgMain;
nRet = dlgMain.DoModal();
}
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
发表评论
-
CListCtrl和CImageList显示缩略图,图片自动排列。
2011-04-06 14:30 8385的CListCtrl风格设置为ICON风格 CImag ... -
有用的vc获取当前目录的代码
2011-03-11 16:02 1109//获取当前目录CString GetModuleDir(){ ... -
Visual C++多媒体设计及图形、图像处理
2011-03-07 13:14 911在VC下显示JPEG、GIF格式图像的一种简便方法一、 引言 ... -
vc制作异形窗口的方法
2011-02-26 15:01 1040http://wenku.baidu.com/view/d4a ... -
add a status bar to an MFC dialog
2011-02-08 11:48 1666we can get the method from the ... -
QQ找茬外挂 开源
2011-01-12 11:31 931http://hi.baidu.com/blue236146_ ... -
LPTSTR详解
2011-01-07 10:02 1146LPTSTR 与char*等价,表示普通字符/字符串变量 ... -
Unicode下MFC获取命令行参数,执行脚本制作
2011-01-06 15:07 2440在MFC程序中,可以用以下几种方法来获取命令行参数。 ... -
INT_PTR介绍
2011-01-06 09:07 2726不知道是从哪个版本的SDK开始,微软引入了一个新的类型——IN ... -
VC++多线程编程
2011-01-05 12:48 1351多线程编程之一——问题提出 一、问题的提出编写一个耗时的单线 ... -
VC++文件操作
2010-12-30 17:40 905文件属性相关 1.判断文件是否存在 利用CFile类和CF ... -
vc++ 如何使radio button ,checkbox初始为已选状态?
2010-12-29 11:28 1704CheckDlgButton(IDC_CHECK1, BS ... -
vc++ 开源异步socket通信项目
2010-12-21 17:28 1103http://www.codeproject.com/KB/I ... -
smtp小记录,慢慢记录
2010-12-17 08:46 661我的smtp小研究,正在开发一个vc++的邮件客户端,goog ...
相关推荐
**WTL CDialogResize扩展**是Windows Template Library (WTL)的一个增强功能,它使得对话框在用户调整大小时能够保持其元素的相对位置和比例,从而提供更友好的用户体验。在传统的Windows应用程序中,对话框通常在...
`CDialogResize`类提供了一种便捷的方法来实现对话框和属性表的大小调整功能,使得用户可以根据需要自由地改变窗口尺寸。这个技术尤其适用于希望在不大量修改代码的情况下实现窗口自适应大小的应用程序。 标题...
当对话框的尺寸改变时,CDialogResize可以自动调整其子控件的位置和大小,使得界面布局始终保持美观和合理。这对于创建响应用户界面大小调整的应用程序非常有用。 3. **CFontButton类**: 这个源代码的核心是...
基于springboot大学生就业信息管理系统源码数据库文档.zip
基于java的驾校收支管理可视化平台的开题报告
时间序列 原木 间隔5秒钟 20241120
毕业设计&课设_基于 Vue 的电影在线预订与管理系统:后台 Java(SSM)代码,为毕业设计项目.zip
基于springboot课件通中小学教学课件共享平台源码数据库文档.zip
基于java的网上购物商城的开题报告
Delphi人脸检测与识别Demo1fdef-main.zip
基于java的咖啡在线销售系统的开题报告
基于java的自助医疗服务系统的开题报告.docx
内容概要:本文档全面介绍了Visual Basic(VB)编程语言的基础知识和高级应用。首先概述了VB的基本特性和开发环境,随后详细讲述了VB的数据类型、变量、运算符、控制结构、数组、过程与函数、变量作用域等内容。接着介绍了窗体设计、控件使用、菜单与工具栏的设计,文件操作、数据库访问等关键知识点。最后讨论了VB的学习方法、发展历史及其在桌面应用、Web应用、数据库应用、游戏开发和自动化脚本编写等领域的广泛应用前景。 适合人群:初学者和中级程序员,尤其是希望快速掌握Windows桌面应用开发的人群。 使用场景及目标:①掌握VB的基础语法和开发环境;②学会使用VB创建复杂的用户界面和功能完整的应用程序;③理解数据库操作、文件管理和网络编程等高级主题。 其他说明:Visual Basic是一种简单易学且功能强大的编程语言,尤其适合用于开发Windows桌面应用。文中不仅覆盖了基础知识,还包括了大量的实用案例和技术细节,帮助读者快速提升编程技能。
基于java的疫情期间高校防控系统开题报告.docx
基于springboot+vue社区老年人帮扶系统源码数据库文档.zip
基于java的超市商品管理系统的开题报告.docx
基于SpringBoot房屋买卖平台源码数据库文档.zip
xdu限通院23微处理器系统与应用大作业(两只老虎),适应于汇编语言keil软件,
<项目介绍> - 新闻类网站系统,基于SSM(Spring、Spring MVC、MyBatis)+MySQL开发,高分成品毕业设计,附带往届论文 - 不懂运行,下载完可以私聊问,可远程教学 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
基于java的学生网上请假系统的开题报告.docx