Hallo Coder,
da man mich noch mals nach dem Code fragte, schrieb ich kurz ein Beispiel für die API-Funktion "UpdateLayeredWindow"
Spoiler anzeigen
Code
#pragma comment(lib, "Msimg32.lib")
#pragma comment(lib, "GDIplus.lib")
#include <Windows.h>
#include <gdiplus.h>
#include <Windowsx.h>
using namespace Gdiplus;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
#pragma region Create Window
// WndClass erstellen!
WNDCLASSEX WndClass;
char sClassName[] = "MyClass";
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
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); // Klasse registrieren
// Create Window
HWND hWnd = CreateWindowEx( WS_EX_LAYERED, // Ex-Styles
WndClass.lpszClassName, // Class
"UpdateLayeredWindow", // Title
WS_OVERLAPPEDWINDOW, // Style
0, // x-Position
0, // y-Position
400, // Width
400, // Height
NULL, // Chield
NULL, // No menu
hInstance, // Program Instance handler
NULL ); // No Window Creation data
// Show Window
ShowWindow(hWnd, nCmdShow);
#pragma endregion
#pragma region GDI+
// Start GDI+
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Create Graphics and Bitmaps
HDC hScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen,400,400);
SelectObject(hDC,hBitmap );
Graphics* hGraphics = Graphics::FromHDC(hDC);
Pen hPen(Color(255,255,255,255));
#pragma endregion
// Create Structurs
BLENDFUNCTION blend = {0};
blend.SourceConstantAlpha = 255; // Transparens des Fensters
blend.AlphaFormat = AC_SRC_ALPHA; // Semi-Transparens aktivieren
POINT ptPos = {0, 0}; // Position des Fensters
SIZE hSize = {400,400}; // Fenstergröße
POINT ptSrc = {0, 0}; // Position an der das "Bild" auf das Fenster gezeichnet werden soll
// Main Loop
MSG hMsg; // Messages of Window
int y = 0;
while(true)
{
// GetMessages
while(PeekMessage(&hMsg, hWnd, 0, 0, PM_NOREMOVE))
{
GetMessage(&hMsg, hWnd, 0, 0 );
TranslateMessage( &hMsg );
DispatchMessage( &hMsg );
}
// Draw
hGraphics->Clear(Color(0,0,0,0));
y++;
hGraphics->DrawLine(&hPen,0,0,400,y);
// Display
UpdateLayeredWindow(hWnd, hScreen, NULL, &hSize, hDC, &ptSrc, 0, &blend, ULW_ALPHA);
// Sleep
Sleep(20);
}
// Exit/ Ressourcen frei geben!
GdiplusShutdown(gdiplusToken);
return true;
}
// 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;
}
Alles anzeigen
Das ganze findet ihr auch in der Zip im Anhang:
- .cpp
- .exe
- .vcproj (Vc++ 2008 Express)
Grüße Faweyr