为什么带有右锚定控件的Borland C ++ Builder 5表单在Vista上显示不正确? [英] Why do my Borland C++Builder 5 forms with right-anchored controls appear incorrectly on Vista?

查看:86
本文介绍了为什么带有右锚定控件的Borland C ++ Builder 5表单在Vista上显示不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了很短的时间寻找解决方案,现在找到了解决方案,所以我认为这对于堆栈溢出很有用.因此,我的答案将紧接在这个问题之后.

Having spent a small age looking for the solution and having now found it, I figured this would be good to document for Stack Overflow. So my answer will follow right after this question.

我使用的是Borland C ++ Builder5.这可能也适用于等效版本的Delphi.我在TPanel上有一个带有TButton的表单.该按钮设置为akRight,akBottom.在XP和以前的Windows上,一切都很好.在Vista上,使用Aero,该按钮向右偏出4个像素.锚定继续正常工作.

I was using Borland C++ Builder 5. This probably also applies to the equivalent version of Delphi. I had a form with a TButton on a TPanel. The button was set to akRight,akBottom. On XP and prior Windows, everything was fine. On Vista, using Aero, the button appeared 4 pixels too far to the right. The anchoring continued to work fine.

另一个示例是带有TComboBox的表单,该表单具有akTop,akRight,akLeft.该组合在Vista上显得太宽了4个像素.

Another example was a form with a TComboBox which had akTop,akRight,akLeft. The combo appeared 4 pixels too wide on Vista.

在Vista上恢复到经典"外观可以使所有内容正确显示.

Returning to the "classic" look on Vista made everything appear correctly.

推荐答案

我尝试的第一件事不起作用:我猜认为问题与Vista上更大的窗口边框有关.我发现VCL中的UpdateAnchorRules由于设计宽度与Vista上窗口的实际宽度之间的差异而在某种程度上计算不正确.从VCL的源代码来看,很明显,更改锚点将导致UpdateAnchorRules再次被调用并(希望)正确地进行计算,因为它现在具有可用表格的实际宽度.

The first thing I tried didn't work: I guessed that the problem had to do with the wider window borders on Vista. I figured UpdateAnchorRules in VCL was somehow calculating incorrectly due to the difference between the design width and the actual width of the window on Vista. Looking at the VCL source, it was clear that changing the anchors would cause UpdateAnchorRules to be called again and (hopefully) calculate correctly, since it now had the actual width of the form available.

我添加了

TAnchors t = BlahBtn->Anchors;
t >> akRight;
BlahBtn->Anchors = t;
t << akRight;
BlahBtn->Anchors = t;

到表单的构造函数.

不高兴.该行为完全不受影响.

No joy. The behaviour was entirely unaffected.

我认为这可能还为时过早,因此将相同的代码同样成功地移到了FormShow方法中.作为最后的尝试,我将表单的设计更改为不再具有akRight的按钮,并将代码更改为

I figured this might be too early in the process, so moved the same code to the FormShow method, equally unsuccessfully. As a last try, I changed the design of the form to no longer have akRight for the button and changed the code to

TAnchors t = BlahBtn->Anchors;
t << akRight;
BlahBtn->Anchors = t;

...也失败了-行为完全不受影响,除了在保存的表单大小(我从注册表中读取并应用于表单中的大小)的情况下,我中断了按钮在XP上的位置FormShow)不是默认设置.

...which failed too - behaviour entirely unaffected, other than that I broke the positioning of the button on XP in the case that the saved size of the form (which I read out of the registry and apply to the form in FormShow) wasn't the default.

添加了一公吨的调试代码,用于输出表单的宽度,按钮的宽度,按钮的左侧,按钮的左侧,表单的ClientRect等在表单的生存期内的各个时间点,我发现了问题.由于某种原因(可能仍然与窗口边框有关-我无法确切找出原因是什么),VCL正在打开宽度小于其应有4个像素的窗口.此后不久就纠正了宽度,但是到那时,锚点(和UpdateAnchorRules)已经将按钮的位置固定在距离右侧4个像素的位置.

Having added a metric tonne of debug code outputting the width of the form, width of the button, left of the button, ClientRect of the form, etc. at various points during the form's lifetime, I found the problem. For some reason (presumably still window-border-related - I didn't manage to find out exactly what the reason was), VCL was opening the window with the width 4 pixels below what it should have been. The width got corrected shortly thereafter, but by that point, the anchoring (and UpdateAnchorRules) had already fixed the positioning of the button 4 pixels too far to the right.

解决方法是:

void __fastcall TFooBarDlg::CreateParams(TCreateParams &Params)
{
    TForm::CreateParams(Params);
    int i = GetSystemMetrics(SM_CXSIZEFRAME);
    Params.Width=Params.Width+(2*(i-4));
}

这将使用Vista报告的不同边框大小来校正表格的初始宽度.它会在Vista上引起正确的行为,同时在其他Windows版本上(以及具有经典"外观的Vista)保留它.

This corrects the initial width of the form, using the differing size of the border as reported by Vista. It causes the correct behaviour on Vista while retaining it on other Windows versions (and Vista with "classic" look).

希望这对某人有帮助.

这篇关于为什么带有右锚定控件的Borland C ++ Builder 5表单在Vista上显示不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆