0

我正在编写一个具有自定义框架的窗口的应用程序。我访问了这些网站并使用它们来创建我自己的应用程序:

Winprog

并且:

Microsoft Docs:使用 dwm 的自定义框架窗口

我使用 TDM_GCC 和 c 语言编写这个应用程序。这个应用程序使用一些链接器编译成功;但我的自定义框架窗口不可拖动且不可调整大小。请帮我解决我的问题。我的代码如下:

#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <dwmapi.h>
#include <stdbool.h>

const char g_szClassName[] = "myWindowClass";

LRESULT HitTestNCA(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    POINT ptMouse = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};

    RECT rcWindow;
    GetWindowRect(hWnd, &rcWindow); 

    RECT rcFrame = { 0 };
    AdjustWindowRectEx(&rcFrame, WS_OVERLAPPEDWINDOW & ~WS_CAPTION, false, WS_EX_CLIENTEDGE);

    USHORT uRow = 1;
    USHORT uCol = 1;
    bool fOnResizeBorder = false;

    if (ptMouse.y >= rcWindow.top && ptMouse.y < rcWindow.top + 50)
    {
        fOnResizeBorder = (ptMouse.y < (rcWindow.top - rcFrame.top));
        uRow = 0;
    }
    else if (ptMouse.y < rcWindow.bottom && ptMouse.y >= rcWindow.bottom - 50)
    {
        uRow = 2;
    }

    if (ptMouse.x >= rcWindow.left && ptMouse.x < rcWindow.left + 50)
    {
        uCol = 0; // left side
    }
    else if (ptMouse.x < rcWindow.right && ptMouse.x >= rcWindow.right - 50)
    {
        uCol = 2; // right side
    }

    LRESULT hitTests[3][3] = 
    {
        { HTTOPLEFT,    fOnResizeBorder ? HTTOP : HTCAPTION,    HTTOPRIGHT },
        { HTLEFT,       HTNOWHERE,     HTRIGHT },
        { HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT },
    };

    return hitTests[uRow][uCol];
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{        
    MARGINS m = {50,50,50,50};
    bool f = false;
    LRESULT l = 0;
    HRESULT hr = S_OK;
    RECT rcClient;

    if(msg == WM_ACTIVATE){
        hr = DwmExtendFrameIntoClientArea(hwnd, &m);
        f = true;
        l = 0;
    }

    if(msg == WM_NCCALCSIZE){
        wParam = true;
        return 0;
    }

    if(msg == WM_CREATE){
        GetWindowRect(hwnd, &rcClient);
        SetWindowPos(hwnd, 
            NULL, 
            rcClient.left, rcClient.top,
            800, 600,
            SWP_FRAMECHANGED);
        f = true;
        l = 0;
    }

    if ((msg == WM_NCHITTEST) && (l == 0)){
        l = HitTestNCA(hwnd, wParam, lParam);
        if (l != HTNOWHERE)
        f = false;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(NULL_PEN);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE ,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

我的项目在一个 zip 文件中:

一个.zip

4

0 回答 0