[Win32SDK基本]Static Control(2)Image Static Control

来源:互联网 发布:新开淘宝店铺没生意 编辑:程序博客网 时间:2024/05/17 09:13
本文由CSDN用户zuishikonghuan所作,转载请注明出处:http://blog.csdn.net/zuishikonghuan/article/details/46620965

上一篇博文([Win32SDK基本]Static Control(1)Text Static Control 和 WM_CTLCOLORSTATIC,地址:http://blog.csdn.net/zuishikonghuan/article/details/46604945)中,介绍了Text Static Control的使用以及利用 WM_CTLCOLORSTATIC 消息修改其前景色和背景色,这篇将介绍 Image Static Control,他可以显示位图或图标

创建子窗口和 Static Control 相关的风格已经在上一篇中详细说了,因此这一篇中不再重复了。但把用到的几个样式再拿出来说一下:

SS_BITMAP:显示一个位图

SS_ICON:显示一个图标(MSDN: If the control is created via CreateWindow or a related function, the text is the name of an icon (not a filename) defined in the resource file associated with the module specified by the hInstance parameter to CreateWindow.  翻译:如果控件通过 CreateWindow 或有相关的函数创建的文本是图标 (而不是文件名) 在与 CreateWindow 的 hInstance 参数所指定的模块关联的资源文件中定义的名称)

SS_REALSIZECONTROL:调整位图以适应静态控件的大小

SS_CENTERIMAGE:位图或图标居中

还是以我的博客“[Win32SDK基本] 窗口详解(超详细)”(地址:http://blog.csdn.net/zuishikonghuan/article/details/46378475)为模板,进一步编写。

下面创建一个 Image Static Control

Image Static Control 使用以下消息来控制

STM_GETICON
STM_GETIMAGE
STM_SETICON
STM_SETIMAGE

具体用法很简单而且相似,可以参见MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ff486029(v=vs.85).aspx

下面以 STM_SETIMAGE 为例给 Image Static Control 设置一个图标

在窗口回调函数中添加

HBITMAP oldimage;HICON hicon;
在case WM_CREATE:里添加

static3 = CreateWindow(TEXT("STATIC"), NULL, WS_CHILD | WS_VISIBLE | SS_ICON, 250, 220, 50, 50, hwnd, (HMENU)3, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);hicon = LoadIcon((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), TEXT("ICON_1"));oldimage = (HBITMAP)SendMessage(static3, STM_SETIMAGE, IMAGE_ICON, (LPARAM)hicon);if (oldimage != NULL){DeleteObject(oldimage);}DeleteObject(hicon);

以前从来没有注意过,因为写文章又翻了一次MSDN,惊奇地发现已下一段话:

Important  

In version 6 of the Microsoft Win32 controls, a bitmap passed to a static control using the STM_SETIMAGE message was the same bitmap returned by a subsequent STM_SETIMAGE message. The client is responsible to delete any bitmap sent to a static control.

于是才知道用完必须释放,汗啊,以前从来没发现过,于是又百度了一下,发现有前辈也遇到过这个问题,于是我就“拿来主义”了,借鉴了那位前辈:http://blog.csdn.net/whitehack/article/details/6443369

效果图:

注意MSDN说最低支持的系统是Vista,下回有时间装个虚拟机看看XP的情况。

0 0
原创粉丝点击