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

以下是代码,注意需要修改一些简单的位置,如资源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. 【HDOJ】【4089】Activation

    概率DP kuangbin总结中的第5题 题解copy: HDU 4098 题意:有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有一下情况: 1.激活失败,留在队列 ...

  2. Directx3D SimpleSample Sample

    在d3d 2010 june这个版本里的samples 不知道为什么SimpleSample Sample 这个 它的documents基本等于没有 Starting point for new Di ...

  3. uva 10887

    是个 hash  用的容器类水过 #include <iostream> #include <cstdio> #include <string> #include ...

  4. Appboy 基于 MongoDB 的数据密集型实践

    摘要:Appboy 正在过手机等新兴渠道尝试一种新的方法,让机构可以与顾客建立更好的关系,可以说是市场自动化产业的一个前沿探索者.在移动端探索上,该公司已经取得了一定的成功,知名产品有 iHeartM ...

  5. StringBuffer用法

    public class StringBufferTest { public static void main(String[] args) { StringBuffer sb=new StringB ...

  6. LINUX下如何查看tomcat运行状态,判断其是否启动

    1,查看Tomcat启动日志. ${catalina_home}\logs [root@iZ25b4ffkfaZ logs]# tail -f catalina.outSep 10, 2015 11: ...

  7. **json_encode:让Json更懂中文(JSON_UNESCAPED_UNICODE)

    我们知道, 用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式, 还会在一定程度上增加传输的数据量. 代码如下: <?php echo ...

  8. hdu 1063 Exponentiation

    求实数的幂,这个用C++写的话有点长,但是用Java写就非常方便了…… );            System.out.println(an);        }    }}

  9. codeforces D

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  10. P1082 找朋友

    描述 童年的我们,对各种事物充满了好奇与向往.这天,小朋友们对数字产生了兴趣,并且想和数字交朋友.可是,怎么分配这些数字才能使得每个小朋友都唯一地找到一个数字朋友呢?C小朋友说:咱们按自己名字的字典序 ...