<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    純win32實現PNG圖片透明窗體

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

     

    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

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全