如何为静态控件设置透明背景? [英] How to set transparent background for static control?

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

问题描述

我使用CreateWindowEx()函数创建了一个窗口。

在这个窗口中我放置了这样创建的静态控件:

I created a window using CreateWindowEx() function.
On this window I place static control which was created like that:

HWND hStatic;
hStatic = CreateWindowEx( WS_EX_TRANSPARENT ,
TEXT("Static"),
TEXT("text"),
WS_CHILD ,
10,
80,
250,
70,
CtrlsWndHnd, //parent
NULL,
hInstance,
NULL);
ShowWindow(hStatic,SW_SHOW);
UpdateWindow(hStatic);



控件已成功创建。

现在我尝试使用此代码创建透明背景:


Control has been successfully created.
And now I try to make transparent background using this code:

//inside parent window procedure:
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC) wParam;
SetBkColor(hdcStatic,TRANSPARENT);
return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);
}
break;



但结果是静态控制的黑色背景。

我做错了什么?错误在哪里?


But result is black background of static control.
What I did wrong? Where is mistake?

推荐答案

我的初步猜测是你需要为控件处理WM_ERASEBKGND消息,并返回非零以指示背景不是需要删除。



它看起来好像你可能自己将背景颜色设置为黑色(SetBkColor采用COLORREF,所以TRANSPARENT可能被解释为黑色或接近黑色的颜色)。您需要将背景模式设置为透明:

My initial guess is that you need to handle the WM_ERASEBKGND message for the control, and return non-zero to indicate that the background doesn't need to be erased.

It also looks as if you are probably setting the background colour to black yourself (SetBkColor takes a COLORREF, so "TRANSPARENT" is probably interpreted as a black, or near black, colour). You need to set the background mode to transparent instead:
SetBkMode (hdcStatic, TRANSPARENT);





但是,根据以下链接,似乎你不应该返回一个空刷,这里提出了完全相同的问题,似乎已经解决了:http://www.cplusplus.com/forum/windows/73999/ [ ^ ]



问候,

Ian。



However, it seems you shouldn't be returning a null brush, according to the following link, where exactly the same question was asked, and seems to be solved: http://www.cplusplus.com/forum/windows/73999/[^]

Regards,
Ian.


Ian A Davidson ,非常感谢!问题在于:

1.而不是:

Ian A Davidson, great thanks! The problem was here:
1. instead of:
SetBkColor(hdcStatic,TRANSPARENT);



需要写:


needed to write:

SetBkMode(hdcStatic,TRANSPARENT);



2。这里我返回了空刷:


2. here I returned null brush:

return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);



但需要返回有效的背景颜色:


but needed to return valid background color:

HBRUSH BGColorBrush = CreateSolidBrush(RGB(230,230,230));
//...
return (LRESULT)BGColorBrush;


这篇关于如何为静态控件设置透明背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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