一劳永逸:如何在 Windows API 中获得完全透明的复选框、按钮、单选按钮等,而不是黑色背景? [英] Once and for all: how do I get a fully transparent checkbox, button, radio button, etc. in Windows API, and not with a black background?

查看:39
本文介绍了一劳永逸:如何在 Windows API 中获得完全透明的复选框、按钮、单选按钮等,而不是黑色背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起,如果我在这里听起来很傲慢/粗鲁.

First, sorry if I sound arrogant/rude here.

好的,所以现在每个人都遇到了这个问题(我希望);我只是没有在任何地方找到任何足够的答案.我们从 Common Controls 6 清单开始,

All right, so everyone has run into this by now (I hope); I just haven't found any adequate answer anywhere. We start with a Common Controls 6 manifest and

case WM_CTLCOLORSTATIC:
    if (/* window has WS_EX_TRANSPARENT */) {
        SetBkMode((HDC) wParam, TRANSPARENT);
        return (LRESULT) GetStockObject(HOLLOW_BRUSH);
    }

并给我们的标签WS_EX_TRANSPARENT.它们变得透明;到目前为止,一切都很好.现在我们必须将该样式添加到我们的复选框中(因为出于某种原因,复选框会响应该样式而不是 WM_CTLCOLORBTN).而且……复选框变黑了!

and give our labels WS_EX_TRANSPARENT. They become transparent; so far so good. Now we have to add that style to our checkboxes (because checkboxes respond to that and not to WM_CTLCOLORBTN for some reason). And... the checkboxes become black!

有没有办法让它们完全透明而不诉诸所有者绘制?我宁愿不自己画复选框;我宁愿不必猜测它是否看起来正确或如果主题 API 在我身上失败,大小是多少(并且将来当我将自定义复选框添加到我的列表视图时,我将不得不自己绘制复选框我已经对所涉及的猜测数量不满意).

Is there any way to make them fully transparent without resorting to owner draw? I'd rather not draw the checkboxes myself; I'd rather not have to guess whether it looks right or what the sizes are should the theming API fail on me (and I'm going to have to draw check boxes by themselves in the future when I add custom checkboxes to my list views and I'm already not happy with the amount of guessing involved).

这些复选框被绘制在主题选项卡控件上.到目前为止,我在 Windows XP 中发现了五个对话框,主题选项卡上带有透明复选框:快捷方式属性的常规选项卡、任务栏和开始菜单属性的任务栏选项卡、系统属性的系统还原选项卡、文件夹选项的常规选项卡(单选按钮),和辅助功能选项的键盘选项卡.所以这肯定是可能的!我确信 Windows UI 作者不必在整个操作系统中使用自定义绘图...我们都缺少什么?

These checkboxes are being drawn over a themed tab control. So far, I've found five dialogs in Windows XP with transparent checkboxes on themed tabs: General tab of Shortcut Properties, Taskbar tab of Taskbar and Start Menu Properties, System Restore tab of System Properties, General tab of Folder Options (radio buttons), and Keyboard tab of Accessibility Options. So this certainly must be possible! I'm sure the Windows UI authors didn't have to use custom draw throughout the OS... What are we all missing?

如果我需要子类,那很好(无论如何我已经有一个用于事件处理目的的子类),但我仍然宁愿不必画自己.

If I need to subclass that's fine (I already have a subclass anyway for event handling purposes), but I still would rather not have to draw myself.

作为奖励,按钮呢?覆盖 WM_CTLCOLORBTN 会给按钮一个黑色边框,但我注意到上面提到的标准对话框都没有费心使按​​钮的角透明,所以 :/

As a bonus, what about push buttons? Overriding WM_CTLCOLORBTN gives buttons a black border, but I do notice that none of the standard dialogs mentioned above bother to make the corners of buttons transparent, so eh :/

谢谢!

推荐答案

你必须返回窗口注册类的hbrBackground元素如下:

You have to return the hbrBackground element of registered class of window as follow:

case WM_CTLCOLORBTN:
case WM_CTLCOLORSTATIC: {
    wchar_t  class_Name[100];
    GetClassName(hWnd, class_Name, 100);
    WNDCLASS lpcls{};
    GetClassInfo(g_hInstance, class_Name, &lpcls);

    return  (LRESULT)lpcls.hbrBackground;
}

这篇关于一劳永逸:如何在 Windows API 中获得完全透明的复选框、按钮、单选按钮等,而不是黑色背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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