XP为主题的控制透明背景 [英] Transparent background of xp-themed control

查看:407
本文介绍了XP为主题的控制透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位图,以及对一个对话方框彩色背景画几个窗口控制。会不会有一些可能的方式,使窗口控件的背景透明?目前,他们显示一个对话框,对话框的默认彩色背景。

I have a few window controls drawn on a bitmap as well as on colored background on a dialog-box. Would there be some possible way to make the window controls' background transparent? Currently they show the default colored background of a dialog-box.

例子 - 我想粘贴纯蓝色的位图和两个按钮控件有明显的默认彩色矩形背景

Example - I tried to paste a solid blue bitmap and the two button controls have the noticeable default colored-rectangle background.

推荐答案

这是很容易通过提供的Windows有一个处理任何你想要刷其使用油漆的按钮的背景下解决。每当你收到你这样做的 WM_CTLCOLORBTN 消息 按钮的父窗口的消息处理程序。

This is easily solved by providing Windows with a handle to whichever brush you want it to use to paint your button's background. You do this whenever you receive the WM_CTLCOLORBTN message in the button's parent window's message handler.

我嘲笑起来比较两个不同的按钮并排侧一点点演示应用程序。两者都是标准的Win32 按钮控制,但在左边的处理 WM_CTLCOLORBTN 消息,并指定使用相同的刷子颜色作为窗口背景。你可以看到区别立即-浅灰色(或者,更precisely,为3D控件的默认颜色, COLOR_3DFACE )按钮周围的矩形条纹消失,按钮看起来对自定义背景色要好得多:

I mocked up a little demo application that compares two different buttons side-by-side. Both are standard Win32 BUTTON controls, but the one on the left handles the WM_CTLCOLORBTN message and specifies a brush with the same color as the window background. You can see the difference immediately—the light gray (or, more precisely, the default color for 3D controls, COLOR_3DFACE) fringes around the button's rectangle are gone and the button looks much better against the custom background color:

    

    

效果也能在Windows XP中启用,这里视觉主题是相同的应用程序的截图:

The effect also works in Windows XP with visual themes enabled—here's a screenshot of the same app:

     

     

而code,我用来创建上述效果几乎是可笑的简单。添加到您的应用程序的主窗口过程( MainWndProc ),如上所述。你不需要触摸你的按钮。

And the code that I used to create the above effect is almost ridiculously simple. Add this to your app's main window procedure (MainWndProc), as described above. You don't need to touch your buttons.

HBRUSH hButtonBackColor = NULL;

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
   {
      case WM_CTLCOLORBTN:
      {
         if (!hButtonBackColor)
         {
            // Create the brush, if it hasn't already been created.
            // (You can use any type of brush here; this is just an example.)
            hButtonBackColor = GetSysColorBrush(COLOR_3DDKSHADOW);
         }
         return (LRESULT)hButtonBackColor;
      }

      // ...
   }
}

不过,请确保您刷指定重新presents完全相同的颜色作为你的窗口的背景颜色,透明的刷子可能无法正常工作。同样,对于一个图案刷(有没有人使用这些了吗?)时,的刷的原点将需要被设置为匹配背景

However, make sure that the brush you specify represents exactly the same color as your window's background color—a transparent brush may not work properly. Similarly, for a patterned brush (does anyone use those anymore?), the brush's origin will need to be set to match the background.

始终确保您通过调用释放您创建的任何画笔 DeleteObject的 !! 在C ++中,你可以通过做一个 CBrush 对象做到这一点(或同等学历)对话框类的成员,这样它会自动销毁。在C语言中,你需要处理 WM_NCDESTROY 消息,并手动删除的画笔。

Always make sure that you release any brushes that you create by calling DeleteObject!! In C++, you would do this by making a CBrush object (or equivalent) a member of your dialog class so that it would be automatically destroyed. In C, you'd need to handle the WM_NCDESTROY message and delete the brush manually.

另外请注意,您需要指定的 BS_OWNERDRAW 为了风格获得这一招工作。上面的例子使用两个标准按钮控件,只用下面的窗口样式标志创建: WS_CHILD WS_VISIBLE BS_PUSHBUTTON

Also note that you do not need to specify the BS_OWNERDRAW style in order for this trick to work. The example above uses two standard button controls, created using only the following window style flags: WS_CHILD, WS_VISIBLE, and BS_PUSHBUTTON.

当然,如果你的设计是任何比上面的例子更复杂(例如,你的按钮重叠的多个背景),你可能必须去的所有者绘制的路线,来代替。我只是觉得这是矫枉过正,你似乎在描述一个简单的任务。

Of course, if your design is any more complex than the above example (e.g., your buttons overlap multiple backgrounds), you'll probably have to go the owner-draw route, instead. I just think that's overkill for a task as simple as the one you appear to be describing.

这篇关于XP为主题的控制透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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