不规则窗口在程序界面设计中能提供非常好的用户体验,以下是我程序运行时的效果图:

以下是代码,注意需要修改一些简单的位置,如资源ID,项目的头文件等,这些是根据你创建的win32程序的项目名改变的,我的项目名为RgnWindow。

1 // RgnWindow.cpp : Defines the entry point for the application.
2 //
3  
4 #include "stdafx.h"
5 #include "RgnWindow.h"
6 #include <comdef.h>
7  
8  
9 #define ULONG_PTR ULONG
10 #include <gdiplus.h>
11 using namespace Gdiplus;
12 #pragma comment(lib, "gdiplus.lib") //注意,要保证vc路径的lib中,能够找到这个文件
13  
14 #define MAX_LOADSTRING 100
15  
16 // Global Variables:
17 HINSTANCE hInst; // current instance
18 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
19 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
20 GdiplusStartupInput m_gdiplusStartupInput;
21 ULONG_PTR m_pGdiToken;
22 HWND g_hWnd = 0;
23 Image *g_pImageBack=0;
24 //透明度颜色混合选项
25 BLENDFUNCTION g_Blend;
26 //背景图的宽度和高度
27 int g_BakWidth, g_BakHeight;
28  
29 // Forward declarations of functions included in this code module:
30 ATOM MyRegisterClass(HINSTANCE hInstance);
31 BOOL InitInstance(HINSTANCE, int);
32 BOOL ImageFromIDResource(UINT nID, LPCTSTR sTR, Image * & pImg);
33 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
34 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
35 void Update();
36  
37  
38 int APIENTRY _tWinMain(HINSTANCE hInstance,
39 HINSTANCE hPrevInstance,
40 LPTSTR lpCmdLine,
41 int nCmdShow)
42 {
43 GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);
44  
45 UNREFERENCED_PARAMETER(hPrevInstance);
46 UNREFERENCED_PARAMETER(lpCmdLine);
47  
48 // TODO: Place code here.
49 MSG msg;
50 HACCEL hAccelTable;
51  
52 // Initialize global strings
53 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
54 LoadString(hInstance, IDC_RGNWINDOW, szWindowClass, MAX_LOADSTRING);
55 MyRegisterClass(hInstance);
56  
57 // Perform application initialization:
58 if (!InitInstance (hInstance, nCmdShow))
59 {
60 return FALSE;
61 }
62  
63 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RGNWINDOW));
64  
65 // Main message loop:
66 while (GetMessage(&msg, NULL, 0, 0))
67 {
68 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
69 {
70 TranslateMessage(&msg);
71 DispatchMessage(&msg);
72 }
73 }
74 GdiplusShutdown(m_pGdiToken);
75 return (int) msg.wParam;
76 }
77  
78  
79  
80 //
81 // FUNCTION: MyRegisterClass()
82 //
83 // PURPOSE: Registers the window class.
84 //
85 // COMMENTS:
86 //
87 // This function and its usage are only necessary if you want this code
88 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
89 // function that was added to Windows 95. It is important to call this function
90 // so that the application will get 'well formed' small icons associated
91 // with it.
92 //
93 ATOM MyRegisterClass(HINSTANCE hInstance)
94 {
95 WNDCLASSEX wcex;
96  
97 wcex.cbSize = sizeof(WNDCLASSEX);
98  
99 wcex.style = CS_HREDRAW | CS_VREDRAW;
100 wcex.lpfnWndProc = WndProc;
101 wcex.cbClsExtra = 0;
102 wcex.cbWndExtra = 0;
103 wcex.hInstance = hInstance;
104 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDC_RGNWINDOW));
105 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
106 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
107 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_RGNWINDOW);
108 wcex.lpszClassName = szWindowClass;
109 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
110  
111 return RegisterClassEx(&wcex);
112 }
113  
114 //
115 // FUNCTION: InitInstance(HINSTANCE, int)
116 //
117 // PURPOSE: Saves instance handle and creates main window
118 //
119 // COMMENTS:
120 //
121 // In this function, we save the instance handle in a global variable and
122 // create and display the main program window.
123 //
124 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
125 {
126 HWND hWnd;
127  
128 hInst = hInstance; // Store instance handle in our global variable
129  
130 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
131 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
132  
133 //hWnd=CreateWindowEx(WS_EX_TOPMOST|WS_EX_LAYERED|WS_EX_TOOLWINDOW, szWindowClass,NULL,WS_POPUP|WS_SYSMENU,
134 // 0,0,300,400,NULL,NULL,hInstance,NULL);
135  
136 if (!hWnd)
137 {
138 return FALSE;
139 }
140 //初始化
141 g_Blend.SourceConstantAlpha = int(0 * 2.55);//1~255
142 g_Blend.BlendOp=0; //theonlyBlendOpdefinedinWindows2000
143 g_Blend.BlendFlags=0; //nothingelseisspecial...
144 g_Blend.AlphaFormat=1; //...
145 g_Blend.SourceConstantAlpha=255;//AC_SRC_ALPHA
146  
147 DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
148 //设置成工具窗口,无标题栏
149 SetWindowLong(hWnd, GWL_STYLE, dwExStyle ^ WS_EX_TOOLWINDOW);
150 //设置成层次窗口
151 dwExStyle = ::GetWindowLong(hWnd, GWL_EXSTYLE);
152 //if((dwExStyle & WS_EX_LAYERED) != WS_EX_LAYERED)
153 // SetWindowLong(hWnd, GWL_EXSTYLE, dwExStyle|WS_EX_TOPMOST|WS_EX_LAYERED);
154  
155 //加载图像
156 //ImageFromIDResource(IDR_CLOCK, L"PNG", g_pImageBack);
157 g_pImageBack = Image::FromFile(_T("D://launcher//bx.png"));
158 ImageType t = g_pImageBack->GetType();
159  
160 //bkImg.FromFile();
161 g_BakWidth = g_pImageBack->GetWidth();
162 g_BakHeight = g_pImageBack->GetHeight();
163  
164  
165 g_hWnd=hWnd;
166  
167  
168 //::SetWindowPos(g_hWnd, HWND_TOPMOST,0,0,g_BakWidth,g_BakHeight,SWP_NOSIZE|SWP_NOMOVE);
169  
170 ShowWindow(hWnd, nCmdShow);
171 UpdateWindow(hWnd);
172 Update();
173  
174 return TRUE;
175 }
176  
177 //
178 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
179 //
180 // PURPOSE: Processes messages for the main window.
181 //
182 // WM_COMMAND - process the application menu
183 // WM_PAINT - Paint the main window
184 // WM_DESTROY - post a quit message and return
185 //
186 //
187 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
188 {
189 int wmId, wmEvent;
190 PAINTSTRUCT ps;
191 HDC hdc;
192  
193  
194 switch (message)
195 {
196 case WM_COMMAND:
197 wmId = LOWORD(wParam);
198 wmEvent = HIWORD(wParam);
199 // Parse the menu selections:
200 switch (wmId)
201 {
202 case IDM_ABOUT:
203 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
204 break;
205 case IDM_EXIT:
206 DestroyWindow(hWnd);
207 break;
208  
209 default:
210 return DefWindowProc(hWnd, message, wParam, lParam);
211 }
212 break;
213 case WM_PAINT:
214 hdc = BeginPaint(hWnd, &ps);
215 // TODO: Add any drawing code here...
216 Update();
217 EndPaint(hWnd, &ps);
218 break;
219 case WM_DESTROY:
220 PostQuitMessage(0);
221 break;
222 case WM_LBUTTONDOWN:
223 //禁止显示移动矩形窗体框
224 ::SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,TRUE,NULL,0);
225 //非标题栏移动整个窗口
226 ::SendMessage(hWnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
227 break;
228 default:
229 return DefWindowProc(hWnd, message, wParam, lParam);
230 }
231 return 0;
232 }
233  
234 // Message handler for about box.
235 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
236 {
237 UNREFERENCED_PARAMETER(lParam);
238 switch (message)
239 {
240 case WM_INITDIALOG:
241 return (INT_PTR)TRUE;
242  
243 case WM_COMMAND:
244 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
245 {
246 EndDialog(hDlg, LOWORD(wParam));
247 return (INT_PTR)TRUE;
248 }
249 break;
250 }
251 return (INT_PTR)FALSE;
252 }
253  
254  
255 BOOL ImageFromIDResource(UINT nID, LPCTSTR sTR, Image * & pImg)
256 {
257 HINSTANCE hInst = hInst;//GetResourceInstance();
258 HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
259 if (!hRsrc)
260 return FALSE;
261 // load resource into memory
262 DWORD len = SizeofResource(hInst, hRsrc);
263 BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
264 if (!lpRsrc)
265 return FALSE;
266 // Allocate global memory on which to create stream
267 HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
268 BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
269 memcpy(pmem,lpRsrc,len);
270 IStream* pstm;
271 CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
272 // load from stream
273 pImg=Gdiplus::Image::FromStream(pstm);
274 // free/release stuff
275 GlobalUnlock(m_hMem);
276 pstm->Release();
277 FreeResource(lpRsrc);
278 return TRUE;
279 }
280  
281  
282 void Update()
283 {
284 HDC hdcTemp= ::GetDC(g_hWnd);
285 HDC hdcMemory=CreateCompatibleDC(hdcTemp);
286 HBITMAP hBitMap=CreateCompatibleBitmap(hdcTemp, g_BakWidth, g_BakHeight);
287 SelectObject(hdcMemory, hBitMap);
288  
289 HDC hdcScreen=::GetDC (g_hWnd);
290 RECT rct;
291 ::GetWindowRect(g_hWnd, &rct);
292 POINT ptWinPos={rct.left,rct.top};
293 Graphics graph(hdcMemory);
294  
295 Point points[] = { Point(0, 0),
296 Point(g_BakWidth, 0),
297 Point(0, g_BakHeight)
298 };
299  
300 graph.DrawImage(g_pImageBack, points, 3);
301  
302 POINT ptDst;
303 ptDst.x = rct.left;
304 ptDst.y = rct.top;
305 SIZE size={g_BakWidth, g_BakHeight};
306  
307 POINT pt;
308 pt.x = 0;
309 pt.y = 0;
310  
311 //设置成层次窗口
312 DWORD dwExStyle = GetWindowLong(g_hWnd,GWL_EXSTYLE);
313 if((dwExStyle & WS_EX_LAYERED) != WS_EX_LAYERED)
314 {//WS_EX_LAYERED是0x00080000
315 SetWindowLong(g_hWnd, GWL_EXSTYLE,dwExStyle^WS_EX_LAYERED);
316 }
317  
318 //更新窗口
319 if (!UpdateLayeredWindow( g_hWnd, hdcScreen, &ptWinPos,
320 &size, hdcMemory, &pt, 0, &g_Blend, 2))
321 {
322 DWORD dwError = ::GetLastError();
323 printf("failed");
324 }
325 //释放资源
326 graph.ReleaseHDC(hdcMemory);
327 ::DeleteObject(hBitMap);
328 ::DeleteDC(hdcMemory);;
329 ::ReleaseDC(g_hWnd, hdcTemp);
330 ::ReleaseDC(g_hWnd, hdcScreen);
331 }

以上介绍的就是Windows SDK 实现不规则窗口,希望对你有所帮助。

Windows SDK 实现不规则窗口介绍的更多相关文章

  1. Windows SDK笔记(经典--一定要看)

    Windows SDK笔记(一):Windows程序基本结构 一.概述 Windows程序具有相对固定的结构,对编写者而言,不需要书写整个过程,大部分过程由系统完成.程序中只要按一定的格式填写系统留给 ...

  2. Kinect for Windows SDK开发入门(二):基础知识 上

    原文来自:http://www.cnblogs.com/yangecnu/archive/2012/03/31/KinectSDK_Application_Fundamentals_Part1.htm ...

  3. Windows Server 2012 NIC Teaming介绍及注意事项

    Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...

  4. Kinect for Windows SDK开发学习相关资源

    Kinect for Windows SDK(K4W)将Kinect的体感操作带到了平常的应用学习中,提供了一种不同于传统的鼠标,键盘及触摸的无接触的交互方式,在某种程度上实现了自然交互界面的理想,即 ...

  5. 【Windows编程】系列第二篇:Windows SDK创建基本控件

    在Win32 SDK环境下,怎么来创建常用的那些基本控件呢?我们知道如果用MFC,简单的拖放即可完成大多数控件的创建,但是我们既然是用Windows SDK API编程,当然是从根上解决这个问题,实际 ...

  6. Kinect for Windows SDK 1.8的改进及新特性

    今年3月, 微软推出了Kinect for Windows SDK 1.7 更新,包括了手势识别 Kinect Interactions 和实时 3D 建模 Kinect Fusion 两项新技术. ...

  7. 背水一战 Windows 10 (3) - UI: 窗口全屏, 窗口尺寸

    [源码下载] 背水一战 Windows 10 (3) - UI: 窗口全屏, 窗口尺寸 作者:webabcd 介绍背水一战 Windows 10 之 UI 窗口全屏 窗口尺寸 示例1.窗口全屏UI/F ...

  8. Kinect for Windows SDK开发初体验(一)环境配置

    1.开发环境需求 (1).硬件需求 a.需要拥有双核,2.66GHz以上的CPU. b.显卡支持Microsoft DirectX 9.0c; c.2GB的内存 d.Kinect for Window ...

  9. Kinect for Windows SDK开发入门(一):开发环境配置

    [译]Kinect for Windows SDK开发入门(一):开发环境配置 前几天无意中看到微软发布了Kinect for windows sensor,进去看了一下Kinect应用的例子,发现K ...

随机推荐

  1. 公众号开发学习Day01

    登录https://mp.weixin.qq.com/后点击注册进入注册微信公众号界面,使用邮箱进行注册 注意点1:一个邮箱只能创建一个公众号,并且公众号的三种类型(订阅号,服务号,企业号)只能选一种 ...

  2. Asp.net 同时下载多个文件

    整理自网络 下载思路是首先把多个文件进行压缩,然后再下载压缩成的压缩包 引用文件dll:ICSharpCode.SharpZipLib.dll 1. 合成下载文件夹 Protected Sub btn ...

  3. eclipse 下找不到或无法加载主类的解决办法[转]

    转自:http://blog.sina.com.cn/s/blog_7ebc46500101gtff.html 有时候 Eclipse 会发神经,好端端的 project 就这么编译不了了,连 Hel ...

  4. 使用XFire+Spring构建Web Service(一)——helloWorld篇

    转自:http://www.blogjava.net/amigoxie/archive/2007/09/26/148207.html原文出处:http://tech.it168.com/j/2007- ...

  5. PhotoshopCS4轻松将PSD分层导出为Png分层

    大家在网上都下载过PSD分层素材,有时候想把素材分层导出,以往做法是一层一层导出,可是这样效率太低,其实利用PS自带功能可以轻松导出PNG分层.     我们先打开一个PSD文件. 文件有7个图层,分 ...

  6. 消除SDK更新时的“https://dl-ssl.google.com refused”异常

    原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“https://dl-ssl.google.com ref ...

  7. Zabbix 安装及微信短信提醒

    Zabbix简介 Zabbix 近几年得到了各大互联网公司的认可,当然第一点归功与它强大的监控功能,第二点免费开源也得到了广大用户的青睐.Zabbix 能将操作系统中的绝大部分指标进行监控,比如(CP ...

  8. 使用tornado让你的请求异步非阻塞

    http://www.dongwm.com/archives/shi-yong-tornadorang-ni-de-qing-qiu-yi-bu-fei-zu-sai/?utm_source=tuic ...

  9. mutable关键字

    mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词.在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量(muta ...

  10. Android 通过程序添加桌面快捷方式

    原理:通过代码向 Launcher 中的广播接收者发送广播来创建快捷图标. 首先要声明的权限是: <!--添加图标的权限--> <uses-permission android:na ...