窗口从顶部拖动后丢失了

问题描述 投票:0回答:1

我正在编写一个 GDI 应用程序(黑色背景和星空动画)。我需要窗口拖动代码方面的帮助。它可以完美地拖动,但是通过选择窗口的上部(标题)及其附近的区域来拖动它,窗口会丢失。我不知道我做错了什么。我尝试做其他事情,但动画停止了一秒钟然后继续,我不希望这样。

#include <Windows.h>
#include <iostream>
#include <windowsx.h>

#define IDB_EXIT 102

PAINTSTRUCT ps;
HDC hdc;
RECT starfield_rc;

int WIDTH = 350;
int HEIGHT = 400;

const int numStars = 50;
const int starSpeed = 1;

struct Star
{
    int x, y;
    int speed;
};

const int NUM_STARS = 100;
Star stars[NUM_STARS];

HDC hdcMem;
HBITMAP hbmMem;
HBITMAP hbmOld;

void starfield(HDC hdc, int x, int y, RECT *rc)
{
    int width = 335;
    rc->left = x;
    rc->top = y;
    rc->right = width;
    rc->bottom = 249;

    HDC hdcMem = CreateCompatibleDC(hdc);
    HBITMAP hbmMem = CreateCompatibleBitmap(hdc, width, 215);
    HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);

    HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0));
    FillRect(hdcMem, rc, brush);
    DeleteObject(brush);

    for (int i = 0; i < NUM_STARS; i++)
    {
        SetPixel(hdcMem, stars[i].x, stars[i].y, RGB(255, 255, 255));
        stars[i].x = (stars[i].x + stars[i].speed) % width;
    }
    BitBlt(hdc, x, y, width, 215, hdcMem, 0, 0, SRCCOPY);
    SelectObject(hdcMem, hbmOld);
    DeleteObject(hbmMem);
    DeleteDC(hdcMem);
}
// main function
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lparam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow)
{
    for (int i = 0; i < NUM_STARS; i++)
    {
        stars[i].x = rand() % 340;
        stars[i].y = rand() % 249;
        stars[i].speed = rand() % 3 + 1;
    }
    TCHAR appname[] = TEXT("Template");
    WNDCLASS wndclass;
    MSG msg;
    HWND hwnd;

    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hInstance = hInstance;
    wndclass.lpfnWndProc = WndProc;
    wndclass.lpszClassName = appname;
    wndclass.lpszMenuName = NULL;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;

    // check this window class is registered or not
    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("Window class is not registered"), TEXT("Error...."), MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_COMPOSITED,
                          appname,                            // window name
                          appname,                            // window text
                          WS_VISIBLE | WS_SYSMENU | WS_POPUP, // set POPUP window style for no border & controls
                          100,                                // window position x
                          100,                                // window position y
                          WIDTH,                              // width
                          HEIGHT,                             // height
                          NULL,
                          NULL,
                          hInstance,
                          NULL);
    // show & update created window
    SetTimer(hwnd, 1, 10, NULL); // Set a timer to trigger every 100ms
    ShowWindow(hwnd, nCmdShow);

    // get message from queue
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}
bool isSizingOrMoving = false;

// WndProc function
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    HINSTANCE hIns;
    HWND exit_button, gen_button, title, license;
    LRESULT move = NULL;
    static int xClick;
    static int yClick;

    switch (message)
    {
    case WM_CREATE:

    {
        exit_button = CreateWindow(TEXT("BUTTON"), NULL, WS_CHILD | WS_VISIBLE | BS_BITMAP,
                                   250, 360, 70, 30, hwnd, (HMENU)100, hIns, 0);
        HBITMAP exitImg = (HBITMAP)LoadImageA(GetModuleHandleA(nullptr), (LPCSTR)MAKEINTRESOURCE(IDB_EXIT), IMAGE_BITMAP, 0, 0, NULL);

        title = CreateWindow(TEXT("STATIC"), TEXT("Hello"), WS_CHILD | WS_VISIBLE, 150, 5, 120, 20, hwnd, 0, hIns, 0);

        SendMessageW(exit_button, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)exitImg);
    }

    break;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        hdc = BeginPaint(hwnd, &ps);
        starfield(hdc, 6, 31, &starfield_rc);
        EndPaint(hwnd, &ps);
        break;
    }

    case WM_TIMER:
        InvalidateRect(hwnd, &starfield_rc, FALSE);
        break;

    case WM_COMMAND:

        switch (wParam)
        {
        case 100:
            PostQuitMessage(EXIT_SUCCESS);
            return 0;
        }

        break;
    case WM_CTLCOLORSTATIC:

    {
        hdc = (HDC)wParam;

        return (LRESULT)CreateSolidBrush(RGB(0, 0, 0)); // black and white

        break;
    }

    case WM_CTLCOLOREDIT:

    {
        hdc = (HDC)wParam;

        return (LRESULT)CreateSolidBrush(RGB(0, 0, 0)); // black and white

        break;
    }

    case WM_LBUTTONDOWN:
        // Get the click position
        xClick = LOWORD(lParam);
        yClick = HIWORD(lParam);
        if (xClick < 0 && yClick < 0)
        {

            return 1;
        }
        // Restrict mouse input to current window
        SetCapture(hwnd);
        return 1;
    case WM_LBUTTONUP:
    {
        // Window no longer requires all mouse input
        ReleaseCapture();
        return 1;
    }

    case WM_MOUSEMOVE:
    {
        if (GetCapture() == hwnd) // Check if this window has mouse input
        {
            // Get the window's screen coordinates
            RECT rcWindow;
            GetWindowRect(hwnd, &rcWindow);

            // Get the current mouse coordinates
            int xMouse = LOWORD(lParam);
            int yMouse = HIWORD(lParam);

            // Calculate the new window coordinates
            int xWindow = rcWindow.left + xMouse - xClick;
            int yWindow = rcWindow.top + yMouse - yClick;

            // Set the window's new screen position (don't resize or change z-order)
            SetWindowPos(hwnd, NULL, xWindow, yWindow, 0, 0,
                         SWP_NOSIZE | SWP_NOZORDER);
        }
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(EXIT_SUCCESS);
        return 0;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}
c++ windows winapi gdi
1个回答
0
投票

这里的许多问题(使用统一变量、边缘情况等)是,在

WM_MOUSEMOVE
成功时不会返回 0。 Alos
SetWindowPos
有时可能会失败,特别是如果您不对其输入进行清理。有些(例如,您将其移动到窗口和客户区域之间有任务栏)可能会导致窗口崩溃,因为您跟踪鼠标位置的方式以及随后
DefaultWndProc
进行了控制。

        // Get the current mouse coordinates
        int xMouse = GET_X_LPARAM(lParam);
        int yMouse = GET_Y_LPARAM(lParam);

请勿使用 LOWORD 或 HIWORD 宏来提取 x- 和 y- 光标位置的坐标,因为这些宏返回 在具有多个显示器的系统上结果不正确。系统具有 多个显示器可以具有负 x 和 y 坐标,并且 LOWORD HIWORD 将坐标视为无符号量。

具有虚拟桌面的现代 Windows 也是多屏幕的,因此如果您尝试在任务栏附近“跨”屏幕边框,可能会遇到窗口“跳”出屏幕的问题。

另外,至少检查一下是否成功(您不需要将其传递给):

        if (SetWindowPos(hwnd, NULL, xWindow, yWindow, 0, 0,
            SWP_NOSIZE | SWP_NOZORDER)) {
            return 0; // MUST return zero of WM_MOUSEMOVE is processed
        }
© www.soinside.com 2019 - 2024. All rights reserved.