码迷,mamicode.com
首页 > Windows程序 > 详细

纯win32实现PNG图片透明窗体

时间:2014-04-30 23:36:06      阅读:742      评论:0      收藏:0      [点我收藏+]

标签:des   com   http   style   blog   class   c   log   size   t   tar   

 

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

 

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

纯win32实现PNG图片透明窗体,布布扣,bubuko.com

纯win32实现PNG图片透明窗体

标签:des   com   http   style   blog   class   c   log   size   t   tar   

原文地址:http://www.cnblogs.com/lidabo/p/3701249.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!