GDI +在8.1窗口不起作用?

时间:2015-09-02 10:03:31

标签: visual-studio-2010 winapi windows-8.1 gdi+

我想在win32中为所有Windows平台制作​​一个alpha混合窗口。我在visual_studio_2010_sp1_x86中加载了一个带有GDI +和Image and Graphics类的png文件。它适用于Windows XP和7_sp1 x86& x64非常好。但是当我在Windows 8.1上运行时,它没有显示任何错误消息。 Windows 8.1是否停止支持GDI +或我的代码出错?感谢名单。 这是我的所有代码:

#define _UNICODE
#define UNICODE

#ifndef WINVER              
#define WINVER 0x0501       
#endif

#ifndef _WIN32_WINNT        
#define _WIN32_WINNT 0x0501
#endif

#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0410
#endif

#define WIN32_LEAN_AND_MEAN 

#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

#include <commctrl.h>
#include <atlimage.h>
#include <objbase.h> 
#include <gdiplus.h> 
using namespace Gdiplus; 
#pragma comment (lib, "Gdiplus.lib")
#pragma comment (lib, "comctl32.lib")

GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;

LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    MSG msg;
    HACCEL hAccelTable;

    GdiplusStartupInput Startup;
    GdiplusStartup( &gdiplusToken, &Startup, NULL );

    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, IDI_APPLICATION);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = L"CTest";
    wcex.hIconSm        = LoadIcon(wcex.hInstance, IDI_APPLICATION);
    RegisterClassEx(&wcex);

    HWND hWnd;
    hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_LEFT | WS_EX_TOOLWINDOW, L"CTest", L"Test", WS_VISIBLE,
                            CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   if (!hWnd)
      return FALSE;

    HANDLE hFile = CreateFile(L"Shape.png" ,
        GENERIC_READ | GENERIC_WRITE , 
        FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE , 
        NULL , OPEN_EXISTING , NULL , NULL);
    if (!hFile)
        return(0);
    DWORD dwSize = GetFileSize( hFile, NULL );
    HGLOBAL hMem = GlobalAlloc(GMEM_FIXED, dwSize);
    LPBYTE pMem = (LPBYTE)GlobalLock(hMem);
    DWORD dwRead = 0;
    ReadFile( hFile, pMem, dwSize, &dwRead, NULL);
    CloseHandle(hFile);

    IStream * pStream = NULL;
    CreateStreamOnHGlobal( hMem, FALSE, &pStream);
    Image * pImage = pImage->FromStream(pStream, FALSE);
    GlobalUnlock(hMem);
    pStream->Release();
    if (!pImage)
        return(0);
    SetWindowPos(hWnd, NULL, 0, 0, 150, 150, SWP_NOZORDER | SWP_NOMOVE);
    ShowWindow(hWnd, SW_NORMAL);
    //UpdateWindow(hWnd);

    HDC hDC = GetDC(hWnd);
    HDC hdcMemory = CreateCompatibleDC(hDC);
    BITMAPINFOHEADER bmih = { 0 };   
    int nBytesPerLine = ((150 * 32 + 31) & (~31)) >> 3;
    bmih.biSize = sizeof(BITMAPINFOHEADER);   
    bmih.biWidth = 150;   
    bmih.biHeight = 150;   
    bmih.biPlanes = 1;   
    bmih.biBitCount = 32;   
    bmih.biCompression = BI_RGB;   
    bmih.biClrUsed = 0;   
    bmih.biSizeImage = nBytesPerLine * 150;
    PVOID pvBits = NULL;   
    HBITMAP hbmpMem = CreateDIBSection(NULL, (PBITMAPINFO)&bmih, DIB_RGB_COLORS, &pvBits, NULL, 0);
    memset( pvBits, 0, 150 * 4 * 150);
    if(hbmpMem)   
    {   
        HGDIOBJ hbmpOld = SelectObject( hdcMemory, hbmpMem); 
        Graphics graph(hdcMemory);
        graph.SetPageScale(1.0);
        graph.SetPageUnit(UnitPixel);
        graph.SetSmoothingMode(SmoothingModeNone);
        graph.DrawImage(pImage, 0, 0, 150, 150);
        RECT rc;
        GetWindowRect(hWnd, &rc);
        POINT ptSrc = { 0, 0};
        POINT ptWinPos = { rc.left, rc.top};
        SIZE szWin = { 150 , 150 };
        BLENDFUNCTION stBlend = { AC_SRC_OVER, 0, 0xFF, AC_SRC_ALPHA };
        UpdateLayeredWindow(hWnd, hDC, &ptWinPos, &szWin, hdcMemory, &ptSrc, 0, &stBlend, ULW_ALPHA);
        graph.ReleaseHDC(hdcMemory);
        SelectObject( hdcMemory, hbmpOld);   
        DeleteObject(hbmpMem); 
    }
    DeleteDC(hdcMemory);
    DeleteDC(hDC);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE)
            break;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    delete pImage;

    GdiplusShutdown(gdiplusToken);

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_NCHITTEST:
        {
            UINT uHitTest = DefWindowProc(hWnd, WM_NCHITTEST, wParam, lParam);
            if(uHitTest == HTCLIENT)
                return(HTCAPTION);
            else
                return(uHitTest);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 1;
}

在处理完之后,我发现我在Windows 8.1“程序文件(x86)”或“程序文件”文件夹中运行程序,但它没有用!但当我将程序文件夹更改为其他任何地方时,它工作!有人能告诉我这是什么问题吗?!

0 个答案:

没有答案