如何改变CFormView大小

pango99 2001-08-26 11:40:49
VC中生成的CFormView窗口,我想让他在主窗口大小改变时也跟着改变大小,于是我重载CMainFrame::OnSize()函数,代码为:
if(GetActiveView())
GetActiveView()->MoveWindow(0,h1,cx,cy-h1-h2);
h1为工具条窗口高度,h2为状态条窗口高度,可执行后,主窗口老是会出现水平和垂直滚动条,好象CFormView没有缩小到规定大小,我的代码什么地方错了?关键是怎么把滚动条去掉.
...全文
324 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
羊叔不乖 2001-09-05
  • 打赏
  • 举报
回复
羊叔不乖 2001-09-05
  • 打赏
  • 举报
回复
羊叔不乖 2001-09-05
  • 打赏
  • 举报
回复
lily311 2001-09-04
  • 打赏
  • 举报
回复
其实这个问题的解决很简单.
如下所示,重载OnInitialUpdate后,加上SetScrollSizes(MM_TEXT,CSize(20,20)),让滚动条以为CFormView尺寸很小(CSize(20,20)),即可消失.
void CMyFormView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();

SetScrollSizes(MM_TEXT,CSize(20,20));
}
蒋晟 2001-08-27
  • 打赏
  • 举报
回复
你的程序是MDI的还是SDI的?要是MDI,CMainFrame::GetActiveView()返回NULL。应该在子框架的OnGetMinMaxInfo里面固定窗口大小。

Q I have a form-based app written in MFC. When the app starts up, the window is larger than the form. Because most of the form is blank, this doesn't look very good. I want to make the window just big enough to hold the form, but I don't know how big the form is until it's loaded. If I change the window size after the app comes up, then you can see the old size briefly, which looks even worse. Currently, I'm using trial and error to hardwire the size, but now every time someone changes the form, I have to manually edit the window size to fit. Isn't there some way I can make the window size fit the form automatically?

Ben Mundy

A This is a typical Windows chicken-and-egg problem, one of those things that seems like it should be easy but isn't. One way to find the size of a dialog is to load the dialog resource into memory—that is, into a DLGTEMPLATE structure—and look at the cx/cy parameters. Unfortunately, these values are in dialog base units, not pixels. So you have to call GetDialogBaseUnits and do some coordinate conversions, which depend on whether your dialog uses the system font or its own font.
Fortunately, there's an easy and surefire way to find out the size of a dialog, even if it is a bit kludgy: just create the dialog, and see how big it is! Of course, you have to create it invisibly if you don't want the user to see what you're doing. But the dialog used for a form-based app is invisible by default, so in this case making the dialog invisible doesn't require extra effort.
To see how it works, I added the automatic sizing feature to the FormSwap program mentioned in the answer to the previous question. The core of the autosize feature is a static function, GetDialogSize, which gets the size of the form. Yes, even in the days of C++, I still write static functions from time to time. But GetDialogSize is implemented using a C++ class, CTempDlg:

class CTempDlg : public CDialog {
public:
CRect m_rcDlg;
protected:
virtual BOOL OnInitDialog();
};

BOOL CTempDlg::OnInitDialog()
{
GetWindowRect(&m_rcDlg);
m_rcDlg -= m_rcDlg.TopLeft();
EndDialog(0);
return TRUE;
}


CTempDlg has just one function override: OnInitDialog. OnInitDialog saves the position of the dialog in a data member, then quits. (MFC does a bit of trickery to allow this; in straight C you can't call EndDialog from within WM_INITDIALOG.) GetDialogSize uses CTempDlg to get the dialog's size, which it returns as a CSize.

CSize GetDialogSize(LPCTSTR pszTemplName,
CWnd* pParent)
{
CTempDlg dlg;
dlg.Create(pszTemplName, pParent);
return dlg.m_rcDlg.Size();
}


Pretty simple. If you try this at home, just remember that the dialog should have no borders and should be invisible (dialogs used in form views always are).
If you want to get the size of a normal dialog, you'll have to do a bit more coding. To first load the dialog template into memory, turn off the WS_VISIBLE and WS_BORDER styles in the DLGTEMPLATE structure and call CDialog:: CreateIndirect instead of Create. If you're already loading the dialog into memory, you may as well just convert the cx and cy values using GetDialogBaseUnits. In passing, I should also mention another function, MapDialogRect, that converts dialog box units to screen pixels. However, this function requires an HDLG, which is an already created dialog.
Once you have the size of the dialog, you've solved the main problem of sizing your frame—but you aren't out of the woods yet. Now the problem is: given that you know how big the dialog is (that is, how big the client view area should be), how big must you create your main window to get a client area exactly that size? This sounds simple but it turns out to be a real pain in the keyboard. There's a virtual function, CWnd::CalcWindowRect, that's supposed to accomplish this feat. Given a certain-sized client rectangle, it calculates the corresponding frame rectangle. Unfortunately, no one in Redmond bothered to implement this function for CFrameWnd. So that leaves it to you.
Implementing CalcWindowRect is an exercise in nit-picking, or should I say pixel-picking. Given a certain-sized client rectangle, you have to add the heights and widths of all the toolbars, splitter borders, menus, window frames, and what-have-you. In the case of FormSwap, there are toolbars, the splitter window, and the left-pane view that must be taken into account. To calculate the height of the toolbars, you can use the MFC function CWnd::RepositionBars. You feed it a rectangle and a magic code, reposQuery—which says you only want to calculate the toolbars, not actually move them—and presto, you have the numbers you need. If you want to understand RepositionBars better, read the documentation. Good luck.
After calculating the heights of the toolbars, the next troublesome task is calculating the height and width of all the splitter window components: the border with which it surrounds each pane and the splitter bar itself. All these magic numbers are contained within data members in CSplitterWnd, but naturally the data is protected, which means you can't access it! Sigh. So what do you do? Simple: just derive a new class with public functions to export the protected data, and use it instead of CSplitterWnd in your main frame.

class CMySplitterWnd : public CSplitterWnd {
public:
CSize GetBorderSize()
{ return CSize(m_cxBorder,m_cyBorder); }
CSize GetSplitterSize()
{ return CSize(m_cxSplitter,m_cySplitter); }
};


If you're lazy (the best programmers are), you might be tempted to just hardwire the border size and other measurements instead of going to all this trouble. But be warned: the values m_cyBorder and its sisters are different under Windows 3.1, Windows 95, and Windows NT®. So if you want the exact values, you must get them from an instantiated CSplitterWnd object.
By now you've probably fallen asleep just thinking of all this tedious work, but once you've calculated how big your main frame should be, the next step is to actually modify the frame's size. Where do you do it? There are oh, so many places. You could override PreCreateWindow and set the size in CREATESTRUCT, implement a WM_GETMINMAXINFO handler, or alter the size in CFrameWnd::OnCreate. In this particular case, however, there's only one option because the magic RepositionBars function works only after the toolbars have been created. This means the winner is door number three: OnCreate.

int CMainFrame::OnCreate(...)
{
// normal stuff



// Make frame fit dialog
static CSize sz = GetDialogSize(MAKE
INTRESOURCE(IDD_DIALOG1), this);
CRect rc(CPoint(0,0), sz);
CalcWindowRect(&rc);
SetWindowPos(NULL, 0, 0,rc.Width(), rc.Height(),
SWP_NOMOVE|SWP_NOZORDER);
return 0;
}


With this and all the other code in place, the window now appears as in Figure 1, just large enough to enclose the form, and not one pixel larger. Whew!
Before signing off, let me spend a few words on WM_GETMINMAXINFO. Windows sends this message just before creating a window, and again whenever the user attempts to move or size it. WM_GETMINMAXINFO is your opportunity to specify the minimum and maximum sizes allowed for your window. If, after going to all that trouble to calculate the window size, you want to prevent the user from decreasing the size of the window, you should handle WM_GETMINMAXINFO. Just follow the directions in your Win32® Owner's Manual. Don't forget that Windows sends the first WM_GETMINMAXINFO before it creates your window, so RepositionBars won't work. You either have to implement CalcWindowRect another way, without using RepositionBars, or ignore the first WM_GETMINMAXINFO message. One way to do this would be to make the dialog size a data member, m_szDlg, which you initialize to zero and then set in OnCreate. Then your WM_GETMINMAXINFO handler would do nothing if m_szDlg is zero.
第8章 实例173——在视图中使用鼠标进行绘图操作(涂鸦) 实例174——在文档中记录绘图数据,并实现窗口重绘 实例175——通过序列化保存文档 实例176——当文档被修改时在标题上给出提醒 实例177——使用对话框与文档视图进行数据交换 实例178——多文档程序实现为新建的文档设置显示字体 实例179——在滚动窗口中实现绘图 实例180——实现动态滚动窗口 实例181——在窗体视图(CFormView)中使用控件 实例182——在列表视图(CListView)中使用列表控件 实例183——改变视图窗口的背景色 实例184——改变MDI框架窗口背景色 实例185——动态设置主框架窗口的图标 实例186——动态设置子框架窗口的图标 实例187——使窗口总在最前显示 实例188——MDI启动时不创建新文档,且限定框架窗口的大小和位置 实例189——限定框架窗口的大小和位置 实例190——限定MDI子框架窗口的最大、最小尺寸 实例191——实现客户窗口的全屏显示 实例192——为程序制作启动界面 实例193——动画启动、关闭窗口并添加位图背景 实例194——改变主窗口上的标题 实例195——为程序创建系统托盘图标 实例196——为程序创建类似迅雷的辅助隐藏窗口 实例197——动态分割窗口,不同窗口对应同一视图 实例198——静态分割窗口,实现窗口的任意切分 实例199——固定分割窗口的分隔线 实例200——动态改变分割窗口的大小 实例201——定制分隔条的外观特性 实例202——分割窗口形成的多视图实现与文档的交互
目录 (1) 如何通过代码获得应用程序主窗口的 指针? 5 (2) 确定应用程序的路径 6 (3) 如何在程序中获得其他程序的 图标? 6 (4) 获得各种目录信息 7 (5) 如何自定义消息 8 (6) 如何改变窗口的图标? 8 (7) 如何改变窗口的缺省风格? 8 (8) 如何将窗口居中显示? 9 (9) 如何让窗口和 MDI窗口一启动就最大化和最小化? 10 (10) 如何限制窗口的大小? 10 (11) 如何使窗口不可见? 10 (12) 如何创建一个字回绕的CEditView 10 (13) 如何使程序保持极小状态? 11 (14) 移动窗口 11 (15) 通用控件的显示窗口 12 (16) 重置窗口的大小 12 (17) 如何单击除了窗口标题栏以外的区域使窗口移动 12 (18) 如何改变视窗的背景颜色 14 (19) 如何改变窗口标题 15 (20) 如何防止主框窗口在其说明中显示活动的文档名 17 (21) 如何获取有关窗口正在处理的当前消息的信息 17 (22) 如何在代码中获取工具条和状态条的指针 18 (23) 如何使能和禁止工具条的工具提示 18 (24) 如何创建一个不规则形状的窗口 19 (25) 如何获取应用程序的 实例句柄? 23 (26) 如何编程结束应用程序? 23 (27) 如何创建和使用无模式对话框 24 (28) 如何防止主框窗口在其说明中显示活动的文档名 26 (29) 如何在代码中获取工具条和状态条的指针 27 (30) 怎样加载其他的应用程序? 27 (31) 如何在代码中获取工具条和状态条的指针 28 (33) 如何设置工具条标题 29 (34) 如何使窗口始终在最前方? 30 (35) 如何在对话框中显示一个位图 30 (36) 如何改变对话或窗体视窗的背景颜色 30 (37) 如何获取一个对话控件的指针 32 (38) 如何禁止和使能控件 33 (39) 如何改变控件的字体 33 (40) 如何在OLE控件中使用OLE_COLOR数据类型 35 (41) 在不使用通用文件打开对话的情况下如何显示一个文件列表 35 (42) 为什么旋转按钮控件看起来倒转 36 (43) 为什么旋转按钮控件不能自动地更新它下面的编辑控件 37 (44) 如何用位图显示下压按钮 37 (45) 如何一个创建三态下压按钮 38 (46) 如何动态创建控件 38 (47) 如何限制编辑框中的准许字符 38 (48) 如何改变控件的颜色 40 (49) 当向列表框中添加多个项时如何防止闪烁 43 (50) 如何向编辑控件中添加文本 43 (51) 如何访问预定义的GDI对象 44 (52) 如何获取GDI对象的属性信息 45 (53) 如何实现一个橡皮区矩形 46 (54) 如何更新翻转背景颜色的文本 49 (55) 如何创建一个具有特定点大小的字体 50 (56) 如何计算一个串的大小 51 (57) 如何显示旋转文本 52 (58) 如何正确显示包含标签字符的串 54 (59) 如何快速地格式化一个CString对象 55 (60) 串太长时如何在其末尾显示一个省略号 55 (61) 为什么即使调用EnableMenuItem菜单项后,菜单项还处于禁止状态 56 (62) 如何给系统菜单添加一个菜单项 56 (63) 如何确定顶层菜单所占据的菜单行数 58 (64) 在用户环境中如何确定系统显示元素的颜色 59 (65) 如何查询和设置系统参数 59 (66) 如何确定当前屏幕分辨率 60 (67) 如何使用一个预定义的Windows光标 60 (68) 如何检索原先的Task Manager应用程序使用的任务列表 61 (70) 在哪儿创建临文件 63 (71) 我怎样才能建立一个等待光标? 64 (73) 如何访问桌面窗口 65 (74) 什么是COLORREF? 我该怎样用它? 66 (75) AppWizard所产生的STDAFX文件是干什么用的? 66 (76) 我在我的程序中是了CDWordArray。我向它添加了约10,000个整数,这使得它变得非常非常慢。为什么会这么糟? 67 (77) 我该如何改变MDI框架窗口的子窗口的大小以使在窗口以一定的大小打开? 67 (78) 在我的程序的某些部分,我可以调用 MessageBox 函数来建立一个信息对话框,例如在视类中。 67 (79) 我需要在我的程序中设置全局变量,以使文档中的所有类都能访问。我应该吧它放到哪儿? 68 (80) 我听说MFC可以发现内存漏洞,我怎样使用该特性? 68 (81) 我怎样才能在我的应用程序中循环浏览已经打开的文档? 68 (82)才能在我的应用程序中循环浏览已经打开的视? 68 (83)数PreCreateWindow是干什么用的? 69 (84)该怎样防止MFC在窗口标题栏上把文档名预置成应用程序名? 69 (85) 我应该怎样防止MFC在窗口标题栏上添加文档名? 69 (86)我应该如何改变视窗口的大小? 69 (87)我有一无模式对话框。我怎样才能在窗口退出时删除CDialog对象? 69 (88)为什么把“delete this”放在PostNcDestroy中而不是OnNcDestroy? 69 (89) File菜单中的MRU列表是从哪儿来的?列表中的名字放在哪儿了?我怎样才能改变列表中项目的最大值? 70 (90) 我在菜单中添加了新的项。但是,当我选该项时,在状态栏上没有出现任何提示信息。为什么? 70 (91) 我怎样才能在应用程序的缺省系统菜单中加上一些东西? 70 (92) 我建立了一个对话框。但是当我显示该对话框时,第一个编辑框总是不能获得焦点,我必须单击它来使它获得焦点。我怎样才能使第一个编辑框在对话框打开时就获得焦点? 71 (93) 我怎样才能使一个窗口具有“always on top”特性? 71 (94) 我要为我的form view添加文档模板。我先建立了对话框模板,然后使用ClassWizard建立了基于CFormView的新类,它也是从CDocument继承来的。我还建立了相应的资源并在InitInstance中添加了新的文档模板。但是,当我试图运行该程序时,出现了Assertion信息。为什么? 71 (95) 我在一对话框中有一列表框,我需要tabbed列表框中的项目。但是,当我处理含有tab字符(用AddString添加的)的列表项时,tab被显示成小黑块而没有展开。哪儿出错了? 72 (96) 我建立了一个应用程序,并使用了CRecordset类。但是,当我运行该程序时,它试图要访问数据库,并给出“Internal Application Error”对话框。我应该怎样做? 72 (97) 我用ClassWizard建立了一个类。但是,我把名字取错了,我想把它从项目中删除,应该如何做? 73 (98) 当我打开应用程序中的窗口时,我要传递该窗口的矩形尺寸。该矩形指定了窗口的外围大小,但是当我调用GetClientRect时,所得到的尺寸要比所希望的值要小(因为工具栏和窗口边框的缘故)。有其它方法来计算窗口的尺寸吗? 73 (99) 我在文档类中设置了一个整型变量。但是,当我试图把该变量写入Serialize函数中的archive文件中时,出现了类型错误。而文档中的其它变量没有问题。为什么? 73 (100) 如何控制菜单的大小? 74 (101) 改变LVIS_SELECTED的状态颜色? 75 (102) 如何只存储文档的某一部分? 76 (103) 保存工具条菜单有bug吗? 76 (104) Tip of the day的bug 76 (105) 如何可以让我的程序可以显示在其它的窗口上面? 77 (106) 如何控制窗口框架的最大最小尺寸? 79 (107) 如何改变窗口框架的颜色? 81 (108) 如何将应用程序窗口置于屏幕正中? 82 (1)当文档被修改时,如何在标题上加上标志'*'? 82 (2)VC6.0对VC5.0的兼容性? 83 (3)打印和打印机的问题? 83 (4)CRichEditCtrl滚动条的问题? 84 (5)从数据库中读大于32k的内容? 84 (6)如何获得CRichEditCtrl中字符的位置? 86 (7)如何限制mdi子框架最大化时的大小? 86 (8)如何切换视口而不破坏它们? 87 (9)改变列表控制时发生闪烁现象? 91 (10)处理列表控件可见项的问题? 91 (11)产生线程的问题? 91 (12)CFile使用了缓冲区吗? 94 (13)DAO的密码? 94 (15)视口的不活动性如何处理? 96 (16)如何使用COleClientItem的IDispatch接口? 97 (17)关于用户自定义的消息使用? 98 (18)在打开一个文档时退出? 99 (19)在CListCtrl控件中多选择项的删除? 99 (20)工作线程的登录状态? 99 (21)如何增加视图中ActiveX控件的事件处理函数? 101 (22)如何创建一个动态的Tree控件? 102 (23)SDI程序开始时不打开文档? 102 (24)List控件中整栏选择? 103 (25)如何重载MRU文件? 104 (26)CImageList控件中图象橙色被显示为黄色? 106 (27)无法正确改变应用程序的图标? 110 (28)工具条状态的问题? 110 (29)在SDI应用程序中使用Active控件? 112 (30)有RichEdit控件的对话框无法正常显示? 112 (31)DLL中的模板成员函数? 112 (32)CFormView中的上下文帮助? 114 (33)CArchive类的WriteObject函数问题? 115 (34)RegisterWindowMessage中的BroadcastSystemMessage如何处理? 115 (35)CListCtrl中选择变化时如何获得通知? 117 (36)如何向ATL-COM对象传送一个数组? 118 (37)如何选择CTreeCtrl中的节点文本进行编辑? 119 (38)如何改变默认的光标形状? 120 (39)如何用键盘滚动分割的视口? 121 (40)如何在线程中处理状态条? 123 (41)如何阻止WINDOWS关闭? 124 (42)如何使一个按钮Disable? 124 (43)怎样从MFC扩展动态链结库(DLL)中显示一个对话框? 125 (44)想隐藏用户界面怎么办? 127 (45)如何实现SDI与MDI的转换? 128 (46) CDC中的竖排文本? 130 (47)如何激活变灰的弹出菜单? 131 (48)线程消息? 132 (49)TreeCtrl控制的显示速度太慢? 133 (50)关于工具条? 135 (51)关于线程消息? 136 (52)关于控件的焦点? 136 (53)如何捕获键盘按键? 138 (54)怎样实现3D效果? 138 (55)怎样建立客户CSocket? 138 (56)Disable一个非模态对话框的客户区? 140 (57)关于使用SetClassLong和SetCapture问题 140 (58)动画控件? 142 (59)错误声明的消息? 143 (59)怎样模拟鼠标动作? 144 (60)改变对话框标题字体? 145 (61)怎样知道CWinThread对象的状态? 146 (62)如何调整控件对话框条的大小? 146 (63)如何顶端显示CStatic类文字? 147 (64)消息句柄出了什么事? 147 (65)树形控件为何闪烁? 148 (66)怎样才能关闭树形控件中的滚动条? 149 (67)如何建立一个带滚动条的窗口? 149 (68)如何实现对话框的拖放? 150 (69)TrackMouseEvent()怎么了 151 (70)奇怪的组合框控件 152 (71)关于使用MS SANS SERIF字体 152 (72)为什么DLL在字符串表中找不到字符串 157 (73)关于复选框的文本颜色 158 (74)系列化与版本的问题 159 (75)在一个控件内检测并使用ON_COMMAND消息 162 (76)为何MDI程序中有子窗口打开时主应用程序不能关. 163 (77)滚动视中LPtoDP失败 165 (78)ODBC许可问题 166 (79)怪异的字体 167 (80)自画列表框样例 170 (81)CWnd::GetMenu()的问题 173 (82)用MFC制作弹出窗口 174 (83)怎样取消一个弹出式菜单 175

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧