Hallo liebe Community,
ich sitz mal wieder vor einem woll zu einfachen Problem, bei dem nicht mal mehr Bloodhound Gang hilft...
Ich möchte wie in diesem Threadversucht auch in C++ die UpdateLayeredWindow-Function nutzen. Nun weiß ich jeddoch nicht wie ich mit GDI+ ein PNG-Image lade und dieses korrekt übergebe (siehe SourceCode).
C++ Code
#pragma comment(lib, "Msimg32.lib")
#pragma comment(lib, "GDIplus.lib")
#include <Windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static char sClassName[] = "MyClass";
static HINSTANCE zhInstance = NULL;
// Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX WndClass;
HWND hWnd;
MSG Msg;
zhInstance = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = zhInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = sClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&WndClass);
// Set Size of Image;
int iWidth = 200;
int iHeight = 200;
// Create Window
hWnd = CreateWindowEx( WS_EX_LAYERED, // Ex-Styles
sClassName, // Class
"Faweyr´s Problem", // Title
WS_OVERLAPPEDWINDOW, // Style
0, // x-Position
0, // y-Position
iWidth, // Width
iHeight, // Height
NULL, // Chield
NULL, // No menu
zhInstance, // Program Instance handler
NULL ); // No Window Creation data
// Show Window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//---------------------------------------------------------------------------------------------------------------------
// Start GDI+
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Image laden
//?????????????????????????????????????????????
Image img = ImageLoadFromFile("C:\Button.png"); // wie lade ich verdammt noch mal ein Bild?
//?????????????????????????????????????????????
// Make mem DC + mem bitmap
HDC hdcScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hdcScreen);
HBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, iWidth, iHeight);
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, /*??? Bild */ ); // und übergebe es hier korrekt!
// Call UpdateLayeredWindow
BLENDFUNCTION blend = {0};
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptPos = {0, 0};
SIZE sizeWnd = {iWidth, iHeight};
POINT ptSrc = {0, 0};
UpdateLayeredWindow(hWnd, hdcScreen, &ptPos, &sizeWnd, hDC, &ptSrc, 0, &blend, ULW_ALPHA);
SelectObject(hDC, hBmpOld);
//---------------------------------------------------------------------------------------------------------------------
// Main Loop
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
// Exit/ Ressourcen frei geben!
DeleteObject(hBmp);
DeleteDC(hDC);
ReleaseDC(NULL, hdcScreen);
GdiplusShutdown(gdiplusToken);
return Msg.wParam;
}
// Callback Function
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
Wäre voll geil wenn mir jemand weiter helfen könnte !
Source und PNG hängen mit an!