简体   繁体   English

使用WinAPI计算按钮大小

[英]Calculating button size using winapi

I have button on toolbar that can have image or/and drop down menu and I have the following code 我在工具栏上有可以具有图像或/和下拉菜单的按钮,我具有以下代码

int text_width = CalcTextWidth(); // this function use GetTextExtentPoint32
int image_width = CalcImageWidth();

then I set set button width as text_width + image_width, but in such formula the spaces between text/image and border are missed and also missed the size of dropdown arrow element. 然后我将设置按钮的宽度设置为text_width + image_width,但是在这样的公式中,文本/图像和边框之间的空格会丢失,并且还会丢失下拉箭头元素的大小。

What is the right way to calculate button width on toolbar 什么是在工具栏上计算按钮宽度的正确方法

PS Button_GetIdealSize() gives me more width than I need, because it use the max width of elements in drop down menu PS Button_GetIdealSize()为我提供了超出所需宽度的宽度,因为它使用了下拉菜单中元素的最大宽度

Update : seems to me that there is no way to calc buttons correctly, but the BTNS_AUTOSIZE flag for button make most of what I need 更新 :在我看来,无法正确计算按钮,但是按钮的BTNS_AUTOSIZE标志可以满足我的大部分需求

Default button size? 默认按钮尺寸?

may be this is the answer, I didn't check 可能这就是答案,我没有检查

 HTHEME hTheme = NULL;
        if(_ThemeHelper::IsAppThemed())
          hTheme = _ThemeHelper::OpenThemeData(pInfo->hdr.hwndFrom, L"TOOLBAR");

        if (hTheme)
        {
           CSize size(0, 0);
           int state1 = (bHot && bSelected) ? TS_PRESSED : TS_HOT;
           _ThemeHelper::GetThemePartSize(hTheme, dc, TP_SPLITBUTTONDROPDOWN, state1, 0, TS_TRUE, &size);

There are intersting answers 有有趣的答案

Since you mentioned it, what is the correct way to get the dropdown width in Windows 7? 既然您提到过,在Windows 7中获取下拉宽度的正确方法是什么? So far we've just had to swap out the TP_SPLITBUTTONDROPDOWN code with a constant. 到目前为止,我们只需要用一个常数替换TP_SPLITBUTTONDROPDOWN代码。

Comment by Craig Peterson — Wednesday, 11 November 2009 @ 0:42 Craig Peterson的评论— 2009年11月11日,星期三@ 0:42

We use the ExpressBars library from Developer Express for the toolbars in RegexBuddy and RegexMagic. 我们将Developer Express的ExpressBars库用于RegexBuddy和RegexMagic中的工具栏。 They fixed the issue in build 47. The fix is that they're now using GetSystemMetrics(SM_CYHSCROLL) to get the drop-down button width when the operating system is Windows 7. I don't know if this is going to be relevant to your code because they weren't using TP_SPLITBUTTONDROPDOWN in the first place. 他们解决了内部版本47中的问题。解决方法是,他们现在正在使用GetSystemMetrics(SM_CYHSCROLL)来获取操作系统为Windows 7时下拉按钮的宽度。我不知道这是否与您的代码,因为他们首先没有使用TP_SPLITBUTTONDROPDOWN。 Note that I'm talking about an actual combo box on a toolbar, not about a tool button with a drop-down menu. 请注意,我在说的是工具栏上的实际组合框,而不是带有下拉菜单的工具按钮。

Comment by Jan Goyvaerts — Wednesday, 11 November 2009 @ 9:59 Jan Goyvaerts的评论— 2009年11月11日,星期三,9:59

from http://www.micro-isv.asia/2009/11/windows-7-is-to-vista-what-xp-was-to-2000/ http://www.micro-isv.asia/2009/11/windows-7-is-to-vista-what-xp-was-to-2000/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM