win32用GDI+加载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
win32用GDI+加载png图片作为背景图的更多相关文章
- VB6之GDI+加载PNG图片
原生的VB6不支持PNG个图片,因为刚有VB的时候还没有PNG的概念呢.不过,利用GDI+加载解析个PNG简直是砍瓜切菜般简单. GDI+的模块是我在网上下载的,地址应该是:http://vistas ...
- GDI+ 加载PNG图片
#include <GdiPlus.h>#pragma comment(lib, "GdiPlus.lib")using namespace Gdiplus; clas ...
- VC中使用GDI+实现为按钮加载Png图片
http://blog.csdn.net/flyfish1986/article/details/5381605 VC中使用GDI+实现为按钮加载Png图片 http://www.codeprojec ...
- WebView加载HTML图片大小自适应与文章自动换行
http://www.brighttj.com/ios/ios-webview-load-html-image-adaptive.html 在很多App中都会使用到webview,尤其是在加载新闻内容 ...
- Android ListView 图片异步加载和图片内存缓存
开发Android应用经常需要处理图片的加载问题.因为图片一般都是存放在服务器端,需要联网去加载,而这又是一个比较耗时的过程,所以Android中都是通过开启一个异步线程去加载.为了增加用户体验,给用 ...
- WPF中加载高分辨率图片性能优化
在最近的项目中,遇到一个关于WPF中同时加载多张图片时,内存占用非常高的问题. 问题背景: 在一个ListView中同时加载多张图片,注意:我们需要加载的图片分辨率非常高. 代码: XAML: < ...
- Fresco 使用笔记(一):加载gif图片并播放
项目总结 --------------------------------------------------------------------- 前言: 项目中图文混合使用的太多太多了,但是绝大部 ...
- 图片_ _Android有效解决加载大图片时内存溢出的问题 2
Android有效解决加载大图片时内存溢出的问题 博客分类: Android Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或 setImageResource或 Bit ...
- Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子
Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0505选数
题目
- 查看电脑已安装的Jdk的位数
查看自己电脑已安装的Jdk的位数的方法: public class ShowJdkBit { public static void main(String[] args) { String arch ...
- LVM的一般操作过程
1. 在磁盘分区上建立物理卷 #fdisk /dev/hdb #pvdisplay /dev/hdb1 //在已经建立好的分区或硬盘上建立物理卷 #pvcreate /dev/hdb1 2 ...
- Java中的异常详解
一.异常定义 阻止当前方法或作用域继续执行的问题,称为异常 二.异常分析 所有不正常类都继承Throwable类,这个类主要有两个子类Error类和Exception类.Error指系统错误 ...
- java--线程状态
1.新建状态 Thread t1 = new Thread(); 创建之后,就已经有了相应的内存和其他资源,但是还是处于不可运行状态. 2.就绪状态 当一个线程使用.start()启动之后就处于就绪状 ...
- 盘点:#AzureChat - 虚拟机和自动伸缩
感谢大家跟 Corey Sanders 和 Stephen Siciliano 一起参加本次 #AzureChat.我们很高兴能借这次在线讨论的机会,倾听各位社区成员对我们最受欢迎的两个主题的意见 - ...
- poj3070 Fibonacci 矩阵快速幂
学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...
- APEC计划指引我们前进:云计算服务将上升
APEC纲领指引我们前进:云计算服务业必将兴起 这是APEC领导人的合影: 这次APEC会议通过了<北京纲领>和<亚太伙伴关系声明>,进一步明白了亚太地区经济合作的发展方向.目 ...
- jQuery 查询 xml
加载xml:(注:这个方法在Chrome是用不了的,Chrome是禁止访问本地的xml文件,在ie11里面用不了,ie11的xmlDom文档没有load方法) function loadXML(fil ...
- 在Qt中如何使用QtDesigner创建的UI文件
使用Qt有一些时间了,一直在IDE环境(qtcreator和VS2003+集成器)中使用,自然少了很多麻烦的步骤.但是在享受这种便利的同 时,我们也失去了理解更多知识背后的点滴.在IDE中,如果我们要 ...