WinAPI 32 и GDI+ перемещают окно при перетаскивании изображения PNG с некоторой областью с прозрачностью.C++

Программы на C++. Форум разработчиков
Ответить
Гость
 WinAPI 32 и GDI+ перемещают окно при перетаскивании изображения PNG с некоторой областью с прозрачностью.

Сообщение Гость »


Я написал программу, которая создает окно с атрибутом WS_POPUP, чтобы загрузить PNG-изображение для фона с прозрачной частью,
прежде всего в коде, содержащемся в случае WM_MOUSEMOVE : Я использую функцию SetWindowPos для перемещения окна, но эта функция перемещает окно в левый верхний угол, это меня немного беспокоит, и я бы хотел, чтобы изображение правильно следовало за курсором мыши.
затем изображение PNG имеет прозрачную часть, которая отображается правильно, но это как если бы оно взяло то, что находится под окном, и скопировало его в само окно.
В любом случае, это код, который я написал .

Код: Выделить всё

#include 
#include 

#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

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

Image* image; // Declare the image pointer globally
int imageWidth = 0;
int imageHeight = 0;
bool isDragging = false;
POINT dragStartPos;
POINT prevMousePos;
POINT windowStartPos;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Initialize GDI+
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

// Load the PNG image
image = Image::FromFile(L"C:\\Users\\hacktooth\\Downloads\\bg.png"); // Replace with your image path
if (image) {
imageWidth = image->GetWidth();
imageHeight = image->GetHeight();
}

// Create the window class
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 = NULL; //(HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"WindowClass";
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);

// Register the window class
RegisterClassEx(&wcex);

// Create the application window
HWND hWnd = CreateWindow(
L"WindowClass",
L"PNG Image",
WS_POPUP,
NULL,
NULL,
imageWidth, // Set the window width to image width
imageHeight, // Set the window height to image height
NULL,
NULL,
hInstance,
NULL
);

// Display the window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

// Main message loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Clean up GDI+
if (image) {
delete image;
}
GdiplusShutdown(gdiplusToken);

return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
Graphics* graphics; // Declare the Graphics pointer
POINT offset{}; //mouse coord offsets

switch (message) {
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

// Draw the image
graphics = new Graphics(hdc);
graphics -> DrawImage(image, 0, 0, imageWidth, imageHeight); // Scale image to fit window

delete graphics; // Cleanup the Graphics object

EndPaint(hWnd, &ps);
break;

case WM_LBUTTONDOWN:
isDragging = true;

offset.x = (int)(short)LOWORD(lParam);
offset.y = (int)(short)HIWORD(lParam);

SetCapture(hWnd);
break;

case WM_LBUTTONUP:
ReleaseCapture();
isDragging = false;
break;

case WM_MOUSEMOVE:
if (isDragging)
{
RECT mainWindowRect;
POINT pos;
short windowWidth, windowHeight;

pos.x = (int)(short)LOWORD(lParam);
pos.y = (int)(short)HIWORD(lParam);

GetWindowRect(hWnd, &mainWindowRect);
windowHeight = mainWindowRect.bottom - mainWindowRect.top;
windowWidth = mainWindowRect.right - mainWindowRect.left;

ClientToScreen(hWnd, &pos);
//MoveWindow(hWnd, pos.x, pos.y, windowWidth, windowHeight, TRUE);
SetWindowPos(hWnd, NULL, pos.x - offset.x, pos.y - offset.y, windowWidth, windowHeight, 0);
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}

return 0;
}
VIDEO :
text
I try with MOVEWINDOW function :
//MoveWindow(hWnd, pos.x, pos.y, windowWidth, windowHeight, TRUE);
same result...
I try with a second mouse coord setted in WM_LBUTTONDOWN same result


Источник: https://stackoverflow.com/questions/781 ... on-with-tr
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»