1. #include <windows.h>
  2. #include <gdiplus.h>
  3. /* GDI+ startup token */
  4. ULONG_PTR gdiplusStartupToken;
  5. /* Declare Windows procedure */
  6. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  7. // UpdateLayeredWindow Defination
  8. typedef BOOL(*UPDATELAYEREDWINDOWFUNCTION)(HWND,HDC,POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);
  9. /* Make the class name into a global variable */
  10. char szClassName[ ] = "PNGDialog";
  11. int WINAPI WinMain (HINSTANCE hThisInstance,
  12. HINSTANCE hPrevInstance,
  13. LPSTR lpszArgument,
  14. int nCmdShow)
  15. {
  16. /**/
  17. Gdiplus::GdiplusStartupInput gdiInput;
  18. Gdiplus::GdiplusStartup(&gdiplusStartupToken,&gdiInput,NULL);
  19. /**/
  20. HWND hwnd; /* This is the handle for our window */
  21. MSG messages; /* Here messages to the application are saved */
  22. WNDCLASSEX wincl; /* Data structure for the windowclass */
  23. /* The Window structure */
  24. wincl.hInstance = hThisInstance;
  25. wincl.lpszClassName = szClassName;//+-69+
  26. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  27. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  28. wincl.cbSize = sizeof (WNDCLASSEX);
  29. /* Use default icon and mouse-pointer */
  30. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  31. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  32. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  33. wincl.lpszMenuName = NULL; /* No menu */
  34. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  35. wincl.cbWndExtra = 0; /* structure or the window instance */
  36. /* Use Windows's default colour as the background of the window */
  37. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  38. /* Register the window class, and if it fails quit the program */
  39. if (!RegisterClassEx (&wincl))
  40. return 0;
  41. /* The class is registered, let's create the program*/
  42. hwnd = CreateWindowEx (
  43. WS_EX_LAYERED|WS_EX_TOPMOST|WS_EX_TOOLWINDOW, /* Extended possibilites for variation */
  44. szClassName, /* Classname */
  45. "PNGDialog Example Application", /* Title Text */
  46. WS_OVERLAPPEDWINDOW, /* default window */
  47. CW_USEDEFAULT, /* Windows decides the position */
  48. CW_USEDEFAULT, /* where the window ends up on the screen */
  49. 500, /* The programs width */
  50. 500, /* and height in pixels */
  51. HWND_DESKTOP, /* The window is a child-window to desktop */
  52. NULL, /* No menu */
  53. hThisInstance, /* Program Instance handler */
  54. NULL /* No Window Creation data */
  55. );
  56. /* Make the window visible on the screen */
  57. ShowWindow (hwnd, nCmdShow);
  58. LONG style = ::GetWindowLong(hwnd,GWL_STYLE);
  59. if(style&WS_CAPTION)
  60. style^=WS_CAPTION;
  61. if(style&WS_THICKFRAME)
  62. style^=WS_THICKFRAME;
  63. if(style&WS_SYSMENU)
  64. style^=WS_SYSMENU;
  65. ::SetWindowLong(hwnd,GWL_STYLE,style);
  66. style = ::GetWindowLong(hwnd,GWL_EXSTYLE);
  67. if(style&WS_EX_APPWINDOW)
  68. style^=WS_EX_APPWINDOW;
  69. ::SetWindowLong(hwnd,GWL_EXSTYLE,style);
  70. /********************************************
  71. * step 1.
  72. * Using Gdiplus to load the image
  73. ********************************************/
  74. RECT wndRect;
  75. ::GetWindowRect(hwnd,&wndRect);
  76. SIZE wndSize = {wndRect.right-wndRect.left,wndRect.bottom-wndRect.top};
  77. HDC hdc = ::GetDC(hwnd);
  78. HDC memDC = ::CreateCompatibleDC(hdc);
  79. HBITMAP memBitmap = ::CreateCompatibleBitmap(hdc,wndSize.cx,wndSize.cy);
  80. ::SelectObject(memDC,memBitmap);
  81. Gdiplus::Image image(L"pic.png");
  82. Gdiplus::Graphics graphics(memDC);
  83. graphics.DrawImage(&image,0,0,wndSize.cx,wndSize.cy);
  84. /********************************************
  85. * step 2.
  86. * Get "UpdateLayeredWindow" function's
  87. * proc address.
  88. ********************************************/
  89. HMODULE hUser32 = ::LoadLibrary("User32.dll");
  90. if(!hUser32)
  91. {
  92. return FALSE;
  93. }
  94. UPDATELAYEREDWINDOWFUNCTION UpdateLayeredWindow = (UPDATELAYEREDWINDOWFUNCTION)::GetProcAddress(hUser32,"UpdateLayeredWindow");
  95. if(!UpdateLayeredWindow)
  96. {
  97. return FALSE;
  98. }
  99. // get screen dc
  100. HDC screenDC = GetDC(NULL);
  101. POINT ptSrc = {0,0};
  102. /*********************************************
  103. * step 3.
  104. * Use UpdateLayeredWindow to Draw the Window
  105. *
  106. *********************************************/
  107. BLENDFUNCTION blendFunction;
  108. blendFunction.AlphaFormat = AC_SRC_ALPHA;
  109. blendFunction.BlendFlags = 0;
  110. blendFunction.BlendOp = AC_SRC_OVER;
  111. blendFunction.SourceConstantAlpha = 255;
  112. UpdateLayeredWindow(hwnd,screenDC,&ptSrc,&wndSize,memDC,&ptSrc,0,&blendFunction,2);
  113. ::DeleteDC(memDC);
  114. ::DeleteObject(memBitmap);
  115. /* Run the message loop. It will run until GetMessage() returns 0 */
  116. while (GetMessage (&messages, NULL, 0, 0))
  117. {
  118. /* Translate virtual-key messages into character messages */
  119. TranslateMessage(&messages);
  120. /* Send message to WindowProcedure */
  121. DispatchMessage(&messages);
  122. }
  123. Gdiplus::GdiplusShutdown(gdiplusStartupToken);
  124. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  125. return messages.wParam;
  126. }
  127. /* This function is called by the Windows function DispatchMessage() */
  128. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  129. {
  130. switch (message) /* handle the messages */
  131. {
  132. case WM_DESTROY:
  133. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
  134. break;
  135. case WM_LBUTTONDOWN:
  136. //::SendMessage(hwnd,WM_HIT)
  137. break;
  138. default: /* for messages that we don't deal with */
  139. return DefWindowProc (hwnd, message, wParam, lParam);
  140. }
  141. return 0;
  142. }

本文出自 “冰狐浪子的博客” 博客,请务必保留此出处http://bhlzlx.blog.51cto.com/3389283/949818

win32用GDI+加载png图片作为背景图的更多相关文章

  1. VB6之GDI+加载PNG图片

    原生的VB6不支持PNG个图片,因为刚有VB的时候还没有PNG的概念呢.不过,利用GDI+加载解析个PNG简直是砍瓜切菜般简单. GDI+的模块是我在网上下载的,地址应该是:http://vistas ...

  2. GDI+ 加载PNG图片

    #include <GdiPlus.h>#pragma comment(lib, "GdiPlus.lib")using namespace Gdiplus; clas ...

  3. VC中使用GDI+实现为按钮加载Png图片

    http://blog.csdn.net/flyfish1986/article/details/5381605 VC中使用GDI+实现为按钮加载Png图片 http://www.codeprojec ...

  4. WebView加载HTML图片大小自适应与文章自动换行

    http://www.brighttj.com/ios/ios-webview-load-html-image-adaptive.html 在很多App中都会使用到webview,尤其是在加载新闻内容 ...

  5. Android ListView 图片异步加载和图片内存缓存

    开发Android应用经常需要处理图片的加载问题.因为图片一般都是存放在服务器端,需要联网去加载,而这又是一个比较耗时的过程,所以Android中都是通过开启一个异步线程去加载.为了增加用户体验,给用 ...

  6. WPF中加载高分辨率图片性能优化

    在最近的项目中,遇到一个关于WPF中同时加载多张图片时,内存占用非常高的问题. 问题背景: 在一个ListView中同时加载多张图片,注意:我们需要加载的图片分辨率非常高. 代码: XAML: < ...

  7. Fresco 使用笔记(一):加载gif图片并播放

    项目总结 --------------------------------------------------------------------- 前言: 项目中图文混合使用的太多太多了,但是绝大部 ...

  8. 图片_ _Android有效解决加载大图片时内存溢出的问题 2

    Android有效解决加载大图片时内存溢出的问题 博客分类: Android Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或 setImageResource或 Bit ...

  9. Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子

    Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...

随机推荐

  1. MediaPlayer视频播放器

    android视频播放 根据apidemo重写.代码如下: package com.jamdeo.tv.livetv.player; import android.media.AudioManager ...

  2. Linux下tcpdump用法

    根据使用者的定义对网络上的数据包进行截获的包分析工具.tcpdump将网络中传送的数据包的“头”完全截获下来提供分析.它支持针对网络层.协议.主机.网络或端口的过滤,并提供了and. or.not等逻 ...

  3. BZOJ 3375: [Usaco2004 Mar]Paranoid Cows 发疯的奶牛( set )

    果然写得短就跑得慢... 直接用set就行了(你要写棵平衡树也可以).没有包含的话, 假如L(i) <= L(j), 那么R[i] <= R[j]. 所以从小到大扫, 每次查找左端点小于当 ...

  4. pyfits 读取bintable

    import pyfits as pf import numpy as np import math import pandas as pd import matplotlib.pyplot as p ...

  5. Jquery学习笔记:删除节点的操作

    假设如下的html代码 <div id="mydiv" style="width:100px;height:100px;border:1px solid red&q ...

  6. 存几个html画图的网站

    http://jvectormap.com/ http://julying.com/lab/raphael-js/docs/#Paper.path http://www.highcharts.com/ ...

  7. 130825组队赛-Regionals 2012, North America - East Central NA

    A.Babs' Box Boutique 一道简单的dfs搜索题,需要两两比较,然后搜到底,得到最大值就行了.比赛时队友写的,我只负责debug..赛后自己写的.. #include<iostr ...

  8. 动态字符串 Stringbuilder类

    StringBuilder动态字符串 string 类型的修改会浪费资源,如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类, stringbuild ...

  9. No http handler was found for request type 'GET'

    asp.net 运用统计图表的时候,在本地运行,没有问题,发布到服务器上的IIS后,就报这个错误,具体错误 看图 这个图片真恶心,网上找了下,资料不多,但是还是有个能真正解决问题的, 解决方法是,在w ...

  10. Android SurfaceView实现静态于动态画图效果

    本文是基于Android的SurfaceView的动态画图效果,实现静态和动态下的正弦波画图,可作为自己做图的简单参考,废话不多说,先上图, 静态效果: 动态效果: 比较简单,代码注释的也比较详细,易 ...