1. 程式人生 > >win32用GDI+載入png圖片作為背景圖

win32用GDI+載入png圖片作為背景圖

  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. }

相關推薦

win32GDI+載入png圖片作為背景

#include <windows.h> #include <gdiplus.h> /* GDI+ startup token */ ULONG_PTR gdiplusStartupToken; /* Declare Windows procedure */ LRESULT CA

CButton 重繪(支援PNG圖片作為背景

按鈕重繪支援PNG圖片作為背景很使用。可以實現按鈕一種背景圖片,然後滑鼠點選按鈕,增加一個透明的png邊框,也可以修改以下實現滑鼠停留在某個按鈕上面,改變按鈕的狀態。 #if !defined(AFX_PICBUTTON_H__4A8AEB90_4455_

windows下PHP獲取視訊第一幀圖片作為背景

我在網上找了很多這方面的資料,總結如下:一般用ffmpeg獲取視訊第一幀圖片作為背景圖; ffmpeg的下載連結  http://ffmpeg.org/download.html ; 下載好包,包裡面在

Win32從資源中載入PNG圖片,然後建立GDI+的Image物件

void LoadPNGFromStaticRes(HMODULE hModule, UINT nResId, Image** ppImg) { HRSRC hRes = FindResource(hModule, MAKEINTRESOURCE(nResId), TEXT("PNG"))

使用GDI+和CImage類載入png圖片

    本文的方法可以載入bmp、jpg、png等多種格式的圖片,但由於大多軟體都使用可帶透明色的png圖片,所以以載入png圖片為研究切入點,找到對應的載入辦法。本文結合TrueLink程式碼的實際使用情況,分別講述使用GDI+和CImage來載入png圖片的方法,並對

移動端設置, mobile , 一張圖片作為背景 ,平鋪 ,自動拉伸 , 圖片 鋪滿視界 ,窗口. background-image , background-size, background-repeat

效果 背景 dev 技術 oct pla div osi eight 1. 效果: 瀏覽器: 手機模擬: 2.代碼: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head&g

圖片作為背景並且是鏈接的寫法(背景圖片加鏈接)

span 方法 http img spl ack 文本 生活 指向 圖片作為背景,並且是鏈接的寫法。例如網站的logo圖片。例如:土豆的logo圖片 <a title="土豆網 tudou.com 每個人都是生活的導演" href="http://www.jb51.

android全平臺編譯libpng並基於ANativeWindow載入PNG圖片

圖形影象實踐 android全平臺編譯libjpeg-turbo並基於ANativeWindow載入JPEG圖片 android全平臺編譯libpng並基於ANativeWindow載入PNG圖片 環境配置 作業系統:ubuntu 16.05 ndk版本:

Python3 tkinter 使用圖片作為背景,並在該背景上加輸入框,按鈕

  最近用到了tkinter,想用自己喜歡的圖片作為背景,看了不少部落格,可能是我少敲了什麼,很少有能成功執行的,最後終於發現了一個可行方案,於是在這裡記錄一下(程式碼為原創) 圖片使用的p站喜愛畫師的作品(因為是以前儲存的,抱歉沒找到畫師連結),侵刪。 import tk

vc資源中載入png圖片以及顯示

因要做一個小的登陸客戶端,所用圖片格式為png.但較少,所以想直接放到資源中.碰到的第一個問題是.net2003中載入png圖片到資源中總是不成功.試了下發現jpg也不行,jpg圖片會提示"不是正確的JPEG格式",後來用vc6.0開啟資原始檔載入成功的.vs2003中原因還未找到,有知道

mfc中圖片控制元件上載入PNG圖片

就一個載入png圖片的程式弄了一下午,想哭。 ***在stadfx.h檔案中新增 #include <comdef.h>//初始化一下com口   #include "gdiplus.h"  using namespace Gdiplus;  #pragma

MFC載入PNG圖片

    MFC中如果要在自繪控制元件中加入圖片,需要BMP格式的圖片,直接在資原始檔中加入,然後通過CBitmap類的LoadBitmap方法載入資源即可。但是對於JPG或者PNG格式的圖片,就無法採用這種方法載入了,然而PNG格式圖片的大小比BMP格式小很多,所以很多時候需

java程式設計過程中中如何在JFrame面板新增圖片作為背景

/**  * 作者:李鵬飛* 功能:新增背景演示 * 時間:2013/9/9 */ package www.csdn.java.lipf; import java.awt.FlowLayout;

android studio中新建assets,webview載入本地圖片

xml檔案中建立webview <WebView android:id="@+id/wv_explain" android:layout_width="match_parent" android:layout_hei

載入png圖片按鈕GdipButton按鈕類

介紹 這個網站上有幾個自繪按鈕,但是我找不到一個容易支援PNG檔案透明度的按鈕,所以我建立了這個類。由於這個類使用GDI +,它實際上支援許多影象格式,但是現在更好的質量按鈕現在是PNG而不是ICO,所以這裡就是一個。 更新:我的樣式工具包(譯者:這篇文章有翻譯,請點選)中有這個類的擴充套件版本。

GDI操作PNG圖片

這兩天在做UI,以前做過一點,但是不太熟悉,於是乎就遇到很多的問題。 以前用BITMAP的時候,做的就是畫素COPY,再多一點就是畫素運算,這一切的東西都是有自己控制的。也就是說不參雜alpha通道。 話說今天我用PNG圖片,但是用到一個小圖片的時候,圖片總是不能正常顯示。

UIWebView載入SVG圖片

svg圖片其實一般用瀏覽器可以直接開啟的,所以在ios中用UIWebView開啟的話就如下:     _web = [[UIWebViewalloc] initWithFrame:CGRectMake

iOS 如何設定一整張圖片作為背景圖片(穿透導航欄)

#pragma mark -推出另外一個控制器的邏輯控制 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { //設定所有介面的背景圖片 UIImageView * b

使用GDI載入PNG實現自定義不規則按鈕

不能直接截圖!@!!!!氣死了。,。改 1---先把按鈕的圖片載入資源裡面,這樣可以在後面直接獲得它的ID號。 例如載入後:PNG圖片ID號為  :IDB_PNG1 2---然後我們把一堆GDI的程式碼加入工程中,為了整潔新建立了資料夾,等會載入標頭檔案的時候要考慮到下

自己GDI+封裝的BMP轉PNG格式圖片的dll(一)

該DLL提供兩個函式介面1. 轉換處理主函式int BMP2PNG(char *file); 引數file 需要轉換的檔案,包括全路徑名:如C:\ABC.bmp 返回值成功返回0,不成功返回非零值2. 資訊返回函式unsigned long GetInfoLog(); 引數