Windows SDK 实现不规则窗口介绍
不规则窗口在程序界面设计中能提供非常好的用户体验,以下是我程序运行时的效果图:
以下是代码,注意需要修改一些简单的位置,如资源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 实现不规则窗口介绍的更多相关文章
- Windows SDK笔记(经典--一定要看)
Windows SDK笔记(一):Windows程序基本结构 一.概述 Windows程序具有相对固定的结构,对编写者而言,不需要书写整个过程,大部分过程由系统完成.程序中只要按一定的格式填写系统留给 ...
- Kinect for Windows SDK开发入门(二):基础知识 上
原文来自:http://www.cnblogs.com/yangecnu/archive/2012/03/31/KinectSDK_Application_Fundamentals_Part1.htm ...
- Windows Server 2012 NIC Teaming介绍及注意事项
Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...
- Kinect for Windows SDK开发学习相关资源
Kinect for Windows SDK(K4W)将Kinect的体感操作带到了平常的应用学习中,提供了一种不同于传统的鼠标,键盘及触摸的无接触的交互方式,在某种程度上实现了自然交互界面的理想,即 ...
- 【Windows编程】系列第二篇:Windows SDK创建基本控件
在Win32 SDK环境下,怎么来创建常用的那些基本控件呢?我们知道如果用MFC,简单的拖放即可完成大多数控件的创建,但是我们既然是用Windows SDK API编程,当然是从根上解决这个问题,实际 ...
- Kinect for Windows SDK 1.8的改进及新特性
今年3月, 微软推出了Kinect for Windows SDK 1.7 更新,包括了手势识别 Kinect Interactions 和实时 3D 建模 Kinect Fusion 两项新技术. ...
- 背水一战 Windows 10 (3) - UI: 窗口全屏, 窗口尺寸
[源码下载] 背水一战 Windows 10 (3) - UI: 窗口全屏, 窗口尺寸 作者:webabcd 介绍背水一战 Windows 10 之 UI 窗口全屏 窗口尺寸 示例1.窗口全屏UI/F ...
- Kinect for Windows SDK开发初体验(一)环境配置
1.开发环境需求 (1).硬件需求 a.需要拥有双核,2.66GHz以上的CPU. b.显卡支持Microsoft DirectX 9.0c; c.2GB的内存 d.Kinect for Window ...
- Kinect for Windows SDK开发入门(一):开发环境配置
[译]Kinect for Windows SDK开发入门(一):开发环境配置 前几天无意中看到微软发布了Kinect for windows sensor,进去看了一下Kinect应用的例子,发现K ...
随机推荐
- android 中设置HttpURLConnection 超时并判断是否超时
设置超时: URL url1 = new URL(url); HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); c ...
- javacript中的mvc设计模式
以下内容为原创翻译,翻译不对的地方还请原谅,凑合着看吧. 原文网址是: 来源:http://www.alexatnet.com/articles/model-view-controller-mvc-j ...
- NYOJ-79 拦截导弹 AC 分类: NYOJ 2014-01-01 23:25 167人阅读 评论(0) 收藏
#include<stdio.h> int main(){ int num[1000]={0}; int n,m,x,y; scanf("%d",&n); wh ...
- 【设计模式六大原则3】依赖倒置原则(Dependence Inversion Principle)
定义:高层模块不应该依赖低层模块,二者都应该依赖其抽象:抽象不应该依赖细节:细节应该依赖抽象. 问题由来:类A直接依赖类B,假如要将类A改为依赖类C,则必须通过修改类A的代码来达成.这种场景下,类 ...
- Codeforces 337D Book of evil
一道树形dp,写出来是因为最近也做了道类似的.这题是看了分析的思路才做出来的,但感觉很多这样的dp都是利用类似的性质.像这题的话distDown很好想,但distUp的时候就很难想了,其实只要抓住di ...
- SDUT1061Binomial Showdown(组合数)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1061 题意 : 表示这个题的英文没看懂,就看懂 ...
- tomcat源码导入eclipse
1. 获取源代码 方式一:从官网http://tomcat.apache.org/download-70.cgi 直接下载,官网提供了Binary 和 Source Code两种下载方式,要研究tom ...
- C语言中时间调用处理的相关函数介绍
asctime(将时间和日期以字符串格式表示) 相关函数:time,ctime,gmtime,localtime 表头文件:#include<time.h> 定义函数:char * asc ...
- Xamarin.Android 入门之:Xamarin快速入门
一. 准备工作 1.新建一个项目取名为phoneword 2.在项目创建好之后,让我们展开“Resources”文件夹然后找到并打开该文件夹下的“layout”文件夹,双击main.axml在Andr ...
- 用 React 编写2048游戏
1.代码 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="U ...