如何在样式为SS_WHITERECT的静态控件中加载图标或位图? [英] How to load icon or bitmap in a static control with style SS_WHITERECT ?
问题描述
大家好!
我想首先感谢所有花时间查看此主题并尝试提供帮助的人。
我在WM_CREATE中使用以下代码进行了静态控制:
HWND hStatic1 = CreateWindowEx(NULL, STATIC, ,WS_CHILD | WS_VISIBLE | SS_ICON | SS_WHITERECT, 250 , 70 , 50 , 50 ,hwnd,NULL,GetModuleHandle(NULL),NULL) ;
控件的背景正如MSDN参考中所描述的那样工作,所以我认为我需要做的就是添加在上述声明之后的代码:
SendMessage(hStatic1,STM_SETIMAGE,(WPARAM)IMAGE_ICON,(LPARAM)hIcon);
但是,图标未加载。然而,如果我从窗口创建中删除SS_WHITERECT,图标会正确加载,但静态控件背景并不像我想要的那样。
所以我的问题是:
如何保留SS_WHITERECT提供的好处,同时能够加载图标以便显示?
虽然我没有经验,但我仍然有强烈的感觉,如果不使用区域和矩形,这是不可能的。
如果所以,我也会接受这些类型的解决方案。
我只是希望在其中心有一个带有图标的静态控件,其背景表现得好像它有风格SS_WHITERECT。
我使用WIN32 API在MS Visual Studio Express 2008,Windows XP,C ++中工作。如果需要任何其他信息(源代码或类似的东西),请求它,我将非常乐意提供它。
静态控件的样式是分为两部分:低位到0x1F(SS_TYPEMASK
)是定义控件类型的枚举,而高位定义样式。查看 WinUser.h 中的SS _ *
定义。如果你结合SS_ICON
(0x03)和SS_WHITERECT
(0x06)你会得到0x07,这是SS_BLACKFRAME
。
要实现想要的行为,请使用SS_ICON | SS_CENTERIMAGE
并将控件的背景颜色设置为白色。另请注意,您必须使用STM_SETICON
消息显示透明图标而不是STM_SETIMAGE
。
我已经解决了!
首先,我必须向成员Jochen Arndt说谢谢,建议我让车主画出控件,在他的评论中提供详细的建议。
更新#1:
*************** *********************************************
如果窗口的背景不是位图,Jochen Arndt的解决方案是完美的。
我没有''当位图是窗口的背景时,找到了一种应用它的方法。
非常感谢任何对此的帮助,因为我相信他的解决方案更好(和我宁愿以这种方式解决我的问题!)。
********************** ************************************** *
我已经转换了我需要加载为静态c的图标ontrol'的背景在线位图,并用函数TransparentBlt(...)绘制。
对于有相同或类似问题的人,这里是代码摘录:
<前lang =c ++> // 需要TransparentBlt(...),请参阅MSDN文档!
#pragma comment(lib,Msimg32.lib)
// 存储位图的主窗口过程中的静态变量
static HBITMAP hbSymbol = LoadBitmap(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDB_BITMAP2));
// create owner drawn static control
case WM_CREATE:
{
// 下面的parametar(HMENU)2000设置此控件的ID
HWND hStatic1 = CreateWindowEx(NULL, STATIC, ,WS_CHILD | WS_VISIBLE | SS_NOTIFY | SS_OWNERDRAW, 450 , 150 , 25 , 25 ,hwnd,(HMENU) 2000 ,GetModuleHandle(NULL),NULL);
}
休息;
// 所有者绘制
case WM_DRAWITEM:
{
switch ((UINT)wParam)
{
case 2000 :
{
BITMAP bp;
LPDRAWITEMSTRUCT lpDIS =(LPDRAWITEMSTRUCT)lParam;
HDC hMemDC = CreateCompatibleDC(lpDIS-> hDC);
GetObject(hbSimbol, sizeof (bp),& bp);
SelectObject(hMemDC,hbSimbol);
// 用于我的面具的背景是RGB(255,0,255)
TransparentBlt(lpDIS-> hDC,lpDIS-> rcItem.top,
lpDIS-> rcItem.left,
lpDIS-> rcItem.right - lpDIS- > rcItem.left,
lpDIS-> rcItem.bottom - lpDIS-> rcItem.top,
hMemDC, 0 , 0 ,bp.bmWidth,bp.bmHeight,
RGB( 255 , 0 , 255 ));
DeleteDC(hMemDC);
}
break ;
默认:
break ;
}
}
break ;
// cleanup
case WM_CLOSE:
DeleteObject(hbSimbol);
DestroyWindow(hwnd);
break ;
case WM_DESTROY:
DeleteObject(hbSimbol);
PostQuitMessage( 0 );
break ;
Hello everyone!
I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.
I have made a static control with following code in WM_CREATE:
HWND hStatic1 = CreateWindowEx( NULL, "STATIC", "", WS_CHILD | WS_VISIBLE | SS_ICON | SS_WHITERECT, 250,70, 50, 50, hwnd, NULL, GetModuleHandle(NULL), NULL);
The background of the control is working as described in MSDN reference, so I thought that all I needed to do is to add the following code after the above statement:
SendMessage( hStatic1, STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hIcon);
However, the icon is not loaded. Yet, if i remove SS_WHITERECT from window creation, the icon loads properly, but static controls background doesn''t behave as I want it to.
So my question is:
How do I keep the benefits offered by SS_WHITERECT, and at the same time am able to load icon so it can be shown?
Although I am inexperienced, I still have a strong feeling that this will not be possible without working with regions and rectangles.
If so, I will accept those types of solutions as well.
I just want to have a static control with icon in its center and its background behaving as if it has style SS_WHITERECT.
I work in MS Visual Studio Express 2008, on Windows XP, in C++, using WIN32 API. If any other information is required ( source code or something similar ), please ask for it, I will more than gladly supply it.
The styles for static controls are divided into two sections: The lower bits up to 0x1F (SS_TYPEMASK
) are an enumeration defining the type of the control while the upper bits define styles. Have a look at theSS_*
definitions in WinUser.h. If you combineSS_ICON
(0x03) andSS_WHITERECT
(0x06) you will get 0x07 which isSS_BLACKFRAME
.
To achieve the wanted behaviour, useSS_ICON | SS_CENTERIMAGE
and set the background color of the control to white. Note also that you must use theSTM_SETICON
message to display a transparent icon rather thanSTM_SETIMAGE
.
I have solved it!
First, I must say BIG "thank you" to member Jochen Arndt for suggesting me to owner draw the control, and providing detailed suggestions in his comment.
UPDATE #1:
************************************************************
Jochen Arndt''s solution works perfect if the background of a window is not a bitmap.
I haven''t found a way to apply it when bitmap is window''s background.
Any help on this one is greatly appreciated,since I believe his solution is better ( and I would prefer to solve my problem this way!).
************************************************************
I have converted icon that I needed to load as static control''s background into bitmap online, and drew it with function TransparentBlt( ... ).
For people with same, or similar problems, here is the code snippet:
// needed for TransparentBlt( ... ), see MSDN documentation! #pragma comment( lib, "Msimg32.lib")
// static variable in main window procedure that stores the bitmap static HBITMAP hbSymbol = LoadBitmap( GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP2) );
// create owner drawn static control case WM_CREATE: { // The parametar (HMENU)2000 below sets ID for this control HWND hStatic1 = CreateWindowEx( NULL, "STATIC", "", WS_CHILD | WS_VISIBLE | SS_NOTIFY | SS_OWNERDRAW, 450,150, 25, 25, hwnd, (HMENU)2000, GetModuleHandle(NULL), NULL); } break;
// owner draw it case WM_DRAWITEM: { switch( (UINT)wParam) { case 2000: { BITMAP bp; LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam; HDC hMemDC = CreateCompatibleDC( lpDIS->hDC); GetObject( hbSimbol, sizeof(bp), &bp); SelectObject(hMemDC, hbSimbol); // background used for my mask was RGB(255,0,255) TransparentBlt( lpDIS->hDC, lpDIS->rcItem.top, lpDIS->rcItem.left, lpDIS->rcItem.right - lpDIS->rcItem.left, lpDIS->rcItem.bottom - lpDIS->rcItem.top, hMemDC, 0, 0, bp.bmWidth, bp.bmHeight, RGB( 255, 0, 255) ); DeleteDC(hMemDC); } break; default: break; } } break;
// cleanup case WM_CLOSE: DeleteObject(hbSimbol); DestroyWindow(hwnd); break; case WM_DESTROY: DeleteObject(hbSimbol); PostQuitMessage(0); break;
这篇关于如何在样式为SS_WHITERECT的静态控件中加载图标或位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!