zoukankan      html  css  js  c++  java
  • 纯win32实现PNG图片透明窗体

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

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

    http://www.cnblogs.com/lidabo/p/3701249.html

  • 相关阅读:
    PHP基础学习笔记(一)
    安装wampserver之后,浏览器中输入localhost页面显示IIS7解决办法
    HTML5常识总结(一)
    AngularJs中的服务
    AngularJs中的directives(指令part1)
    Happy Number——LeetCode
    Binary Tree Zigzag Level Order Traversal——LeetCode
    Construct Binary Tree from Preorder and Inorder Traversal——LeetCode
    Construct Binary Tree from Inorder and Postorder Traversal——LeetCode
    Convert Sorted Array to Binary Search Tree——LeetCode
  • 原文地址:https://www.cnblogs.com/findumars/p/5403007.html
Copyright © 2011-2022 走看看