纯win32实现PNG图片透明窗体
- #include <windows.h>
- #include <gdiplus.h>
- /* GDI+ startup token */
- ULONG_PTR gdiplusStartupToken;
- /* Declare Windows procedure */
- LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
- // UpdateLayeredWindow Defination
- typedef BOOL(*UPDATELAYEREDWINDOWFUNCTION)(HWND,HDC,POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);
- /* Make the class name into a global variable */
- char szClassName[ ] = "PNGDialog";
- int WINAPI WinMain (HINSTANCE hThisInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpszArgument,
- int nCmdShow)
- {
- /**/
- Gdiplus::GdiplusStartupInput gdiInput;
- Gdiplus::GdiplusStartup(&gdiplusStartupToken,&gdiInput,NULL);
- /**/
- HWND hwnd; /* This is the handle for our window */
- MSG messages; /* Here messages to the application are saved */
- WNDCLASSEX wincl; /* Data structure for the windowclass */
- /* The Window structure */
- wincl.hInstance = hThisInstance;
- wincl.lpszClassName = szClassName;//+-69+
- wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
- wincl.style = CS_DBLCLKS; /* Catch double-clicks */
- wincl.cbSize = sizeof (WNDCLASSEX);
- /* Use default icon and mouse-pointer */
- wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
- wincl.lpszMenuName = NULL; /* No menu */
- wincl.cbClsExtra = 0; /* No extra bytes after the window class */
- wincl.cbWndExtra = 0; /* structure or the window instance */
- /* Use Windows's default colour as the background of the window */
- wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
- /* Register the window class, and if it fails quit the program */
- if (!RegisterClassEx (&wincl))
- return 0;
- /* The class is registered, let's create the program*/
- hwnd = CreateWindowEx (
- WS_EX_LAYERED|WS_EX_TOPMOST|WS_EX_TOOLWINDOW, /* Extended possibilites for variation */
- szClassName, /* Classname */
- "PNGDialog Example Application", /* Title Text */
- WS_OVERLAPPEDWINDOW, /* default window */
- CW_USEDEFAULT, /* Windows decides the position */
- CW_USEDEFAULT, /* where the window ends up on the screen */
- 500, /* The programs width */
- 500, /* and height in pixels */
- HWND_DESKTOP, /* The window is a child-window to desktop */
- NULL, /* No menu */
- hThisInstance, /* Program Instance handler */
- NULL /* No Window Creation data */
- );
- /* Make the window visible on the screen */
- ShowWindow (hwnd, nCmdShow);
- LONG style = ::GetWindowLong(hwnd,GWL_STYLE);
- if(style&WS_CAPTION)
- style^=WS_CAPTION;
- if(style&WS_THICKFRAME)
- style^=WS_THICKFRAME;
- if(style&WS_SYSMENU)
- style^=WS_SYSMENU;
- ::SetWindowLong(hwnd,GWL_STYLE,style);
- style = ::GetWindowLong(hwnd,GWL_EXSTYLE);
- if(style&WS_EX_APPWINDOW)
- style^=WS_EX_APPWINDOW;
- ::SetWindowLong(hwnd,GWL_EXSTYLE,style);
- /********************************************
- * step 1.
- * Using Gdiplus to load the image
- ********************************************/
- RECT wndRect;
- ::GetWindowRect(hwnd,&wndRect);
- SIZE wndSize = {wndRect.right-wndRect.left,wndRect.bottom-wndRect.top};
- HDC hdc = ::GetDC(hwnd);
- HDC memDC = ::CreateCompatibleDC(hdc);
- HBITMAP memBitmap = ::CreateCompatibleBitmap(hdc,wndSize.cx,wndSize.cy);
- ::SelectObject(memDC,memBitmap);
- Gdiplus::Image image(L"pic.png");
- Gdiplus::Graphics graphics(memDC);
- graphics.DrawImage(&image,0,0,wndSize.cx,wndSize.cy);
- /********************************************
- * step 2.
- * Get "UpdateLayeredWindow" function's
- * proc address.
- ********************************************/
- HMODULE hUser32 = ::LoadLibrary("User32.dll");
- if(!hUser32)
- {
- return FALSE;
- }
- UPDATELAYEREDWINDOWFUNCTION UpdateLayeredWindow = (UPDATELAYEREDWINDOWFUNCTION)::GetProcAddress(hUser32,"UpdateLayeredWindow");
- if(!UpdateLayeredWindow)
- {
- return FALSE;
- }
- // get screen dc
- HDC screenDC = GetDC(NULL);
- POINT ptSrc = {0,0};
- /*********************************************
- * step 3.
- * Use UpdateLayeredWindow to Draw the Window
- *
- *********************************************/
- BLENDFUNCTION blendFunction;
- blendFunction.AlphaFormat = AC_SRC_ALPHA;
- blendFunction.BlendFlags = 0;
- blendFunction.BlendOp = AC_SRC_OVER;
- blendFunction.SourceConstantAlpha = 255;
- UpdateLayeredWindow(hwnd,screenDC,&ptSrc,&wndSize,memDC,&ptSrc,0,&blendFunction,2);
- ::DeleteDC(memDC);
- ::DeleteObject(memBitmap);
- /* Run the message loop. It will run until GetMessage() returns 0 */
- while (GetMessage (&messages, NULL, 0, 0))
- {
- /* Translate virtual-key messages into character messages */
- TranslateMessage(&messages);
- /* Send message to WindowProcedure */
- DispatchMessage(&messages);
- }
- Gdiplus::GdiplusShutdown(gdiplusStartupToken);
- /* The program return-value is 0 - The value that PostQuitMessage() gave */
- return messages.wParam;
- }
- /* This function is called by the Windows function DispatchMessage() */
- LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message) /* handle the messages */
- {
- case WM_DESTROY:
- PostQuitMessage (0); /* send a WM_QUIT to the message queue */
- break;
- case WM_LBUTTONDOWN:
- //::SendMessage(hwnd,WM_HIT)
- break;
- default: /* for messages that we don't deal with */
- return DefWindowProc (hwnd, message, wParam, lParam);
- }
- return 0;
- }
本文出自 “冰狐浪子的博客” 博客,请务必保留此出处http://bhlzlx.blog.51cto.com/3389283/949818
http://www.cnblogs.com/lidabo/p/3701249.html
纯win32实现PNG图片透明窗体的更多相关文章
- win32下实现透明窗体
最開始写透明窗体的代码,在百度了之后,找到了SetLayeredWindowAttributes()这一个函数,可是因为网上案列的缺少,使得非常多人无法非常好的使用这一个方法,我花了几天的时间写了一个 ...
- 重温 Win32 API ----- 截屏指定窗体并打印
朋友说在一个VC++6.0开发的项目中要增加打印窗体的功能,让帮忙写个代码供其调用. 这么老的IDE当然不想碰了,并且也不喜欢MFC笨拙不清晰的封装.所以决定採用纯Win32 API,然后用C++类简 ...
- gdi+ 高速绘制透明窗体
gdi+ 高速绘制透明窗体: 方法一: 1.用Iamge对象载入png资源, 2.调用drawimage函数讲图片绘制出了 3.UpdateLayeredWindow对窗体进行布局 方法二: 1.用B ...
- qt 获取windows 的消息(通过MFC的DLL的透明窗体转发消息)good
qt 给win32 发送消息很简单,但是要获取windows 消息却十分复杂,最后想了一个不是很完美 但是也是以现在本人能力所能实现的唯一途径了,基本原理是 利用vc编写一个mfc 的dll ,这个d ...
- 【转载】Layered Window(分层窗体,透明窗体)
本文转载自花间醉卧<Layered Window(分层窗体,透明窗体)> //为窗体添加WS_EX_LAYERED属性,该属性使窗体支持透明 ModifyStyleEx(0, WS_EX_ ...
- 解决IE6中 PNG图片透明的终极方案-八种方案!
“珍惜生命,远离IE6”,IE6中的bug令很多Web前端开发人员实为头疼,因此不知道烧了多少脑细胞,在众多的Bug中最令人抓狂的就是IE对png图片的不支持,导致设计师和重构师放弃了很多很炫的效果, ...
- WPF透明窗体不支持缩放解决方案
方案一 WPF中的无边框透明窗体,由于没有边并且透明,窗体无法进行缩放操作,今天来讲解如何解决这个问题. 先说一下思路,我们先手为该窗体添加4个边,4个角用于缩放操作,然后再为他们写事件,完成拖放操作 ...
- Qt Widget 利用 Qt4.5 实现酷炫透明窗体
本文讲述的是Qt Widget 利用 Qt4.5 实现酷炫透明窗体,QWidget类中的每一个窗口部件都是矩形,并且它们按Z轴顺序排列的.一个窗口部件可以被它的父窗口部件或者它前面的窗口部件盖住一部分 ...
- 分享11个纯css完成的图片浏览器
图片画廊用于在网站上显示系列图片,它已成为网站重要的组成部分.实现图片画廊有很多种方法,今天要与大家分享的是11个使用纯 CSS 实现的图片画廊,它们代码少,效果炫,加载速度快,希望能对大家有所帮助. ...
随机推荐
- Ant学习实例
ant 目录(?)[+] Ant学习实例 安装Ant 基础元素 project元素 target元素 property元素 完整示例 Ant学习实例 1.安装Ant 先从http://ant. ...
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)(摘抄)
IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转, Spring 框架的核心基于控制反转原理. 什么是控制反转?控制反转是一种将组件依赖关系的创建和管理置于程序外部的技 ...
- Fiddler 教程(转)
阅读目录 Fiddler的基本介绍 Fiddler的工作原理 同类的其它工具 Fiddler如何捕获Firefox的会话 Fiddler如何捕获HTTPS会话 Fiddler的基本界面 Fiddler ...
- 完整的yuicompressor单个压缩和批量压缩以及gzip再次压缩,拦截器的配置等
下载地址:http://yuilibrary.com/download/yuicompressor/ 个人认为现在yuicompressor是最安全,最值得信赖的压缩工具,至少到现在没出现过问题 1. ...
- hdu4704之费马小定理+整数快速幂
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
- django目录下的各文件
本文部分转载. 使用Python setup.py install命令从源代码安装完Django后,这些都会被拷贝到Python安装目录下的Lib/site-packages/django子目录中.之 ...
- 使用Spring标签库
A.Spring标签库 Web项目若使用Spring Web MVC并使用JSP作为表现的话.从Spring2.0版本开始提供一套标签库可供使用. 使用标签库无非是易于开发,维护之类云云.这里就不阐述 ...
- activity横屏竖屏的切换
原理: 其实总结起来,我们可以得到以下的一些结论 1.当内存不足(不容易模拟).切屏时会调用onSaveInstanceState().onRestoreInstanceState()方法 对于onS ...
- asp.net 通过 Handler 导出数据至excel (让用户下载)
效果图: 代码: Export2Excel.ashx <%@ WebHandler Language="C#" CodeBehind="Export2Excel.a ...
- Tri_integral Summer Training 9 总结
比赛链接 A B C D H I J K 多灾多难的 Summer Training 9,前一天挂了一场比赛,结果题一半不能做,于是打了一个小时就放弃了.之后的两场Summer Training 9一 ...