CResizeCtrl






4.94/5 (16投票s)
2000年7月2日

200438

3662
一个用于使用 MFC 实现可调整大小对话框的调整大小控件。
引言
CResizeCtrl
实现了 WCL 的 ResizePercentage
方法(Optima++别名Power++的框架,已被 Sybase 放弃)。
为每个控件指定了左边距、上边距、宽度和高度,以确定当父窗口的大小发生变化时,控件窗口的位置和大小将如何变化。
左边距指定了对象左边缘的位置相对于父窗口宽度总变化的变化量。
例如:假设左边距为 50,窗口的宽度增加了 200 像素,则对象的左边缘向右移动 100 像素(200 的 50%)。
上边距指定了对象顶部位置相对于父窗口高度总变化的变化量。
例如:假设上边距为 40,窗口的高度减少了 20 像素,则对象的上边缘向上移动 8 像素(20 的 40%)。
宽度指定了对象的宽度相对于父窗口宽度总变化的变化量。
例如:假设宽度为零。那么对象的宽度不会改变,无论父窗口的宽度改变多少。或者假设宽度为 100,窗口的宽度减少了 50 像素,那么对象的宽度也减少了 50 像素(50 的 100%)。
高度指定了对象的高度相对于父窗口高度总变化的变化量。
例如:假设高度为 20,父窗口的高度减少了 50 像素,那么对象的高度也减少了 10 像素(50 的 20%)。
每个参数的有效范围是0 到 maxPart,但左边距 + 宽度和上边距 + 高度的总和必须小于或等于 maxPart。
一般来说,使用以下公式
newValue = oldValue + (( deltaValueParent * partValue) / maxPart )
其中
newValue | 是新的左边距或上边距位置,或者新的宽度或高度 |
oldValue | 是旧的左边距或上边距位置,或者旧的宽度或高度 |
deltaValueParent | 是父窗口宽度或高度的变化量 |
partValue | 是在 Add 方法中指定的左边距、上边距、宽度或高度值 |
maxPart | 是由构造函数或 Create 方法的 maxPart 参数指定的值 |
maxPart 的默认值为 100,为了更好的粒度,可以在构造函数或 Create 方法中指定另一个值
例子
// Create the control m_resize.Create( this ); // Add the controls to be resized // l t w h m_resize.Add( IDC_EDIT1, 0, 0, 100, 50 ); m_resize.Add( IDC_LIST1, 0, 50, 100, 50 ); m_resize.Add( IDOK, 50, 100, 0, 0 ); // Use the current width and height // for minimum tracking size m_resize.SetMinimumTrackingSize();
当父窗口的宽度增加 40 像素,高度增加 20 像素时
IDC_EDIT1: left += 0 top += 0 width += 40 height += 10 IDC_LIST1: left += 0 top += 10 width += 40 height += 10 IDOK : left += 20 top += 20 width += 0 height += 0
用法
将文件 'ResizeCtrl.cpp' 和 'ResizeCtrl.h' 添加到您的项目
因为 CResizeCtrl
可以改变对话框的样式,所以没有必要向对话框模板添加可调整大小的边框。
在关联的头文件中包含 "ResizeCtrl.h',并将 CResizeCtrl
添加到对话框类的实例数据中
#include "ResizeCtrl.h" class CDemoDialog : public CDialog { // other stuff CResizeCtrl m_resize; //....
在 OnInitDialog
中,将应该调整大小的控件添加到 CResizeCtrl
对象
BOOL CDemoDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here // Create the control m_resize.Create( this ); // Add the controls to be resized // l t w h m_resize.Add( IDC_EDIT1, 0, 0, 100, 50 ); m_resize.Add( IDC_LIST1, 0, 50, 100, 50 ); m_resize.Add( IDOK, 50, 100, 0, 0 ); // Use the current width and height // for minimum tracking size m_resize.SetMinimumTrackingSize();
或
BOOL CDemoDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here // Create the control m_resize.Create( this ); // Add the controls to be resized CResizeInfo rInfo[] = { // id l t w h { IDC_EDIT1, 0, 0, 100, 50 }, { IDC_LIST1, 0, 50, 100, 50 }, { IDOK, 50, 100, 0, 0 }, { 0 }, }; m_resize.Add( rInfo ); // Use the current width and height // for minimum tracking size m_resize.SetMinimumTrackingSize();
因为 CResizeCtrl
不是从 CDialog
派生的类,所以可以在不更改对话框模板的情况下使用它。
它甚至可以与公共控件一起使用。演示项目包含一个示例,说明如何将其与 GetOpenFileName
一起使用。