Home > Back-end >  Resizing Window Glitch in C
Resizing Window Glitch in C

Time:12-26

I need to make a WinAPI window in C. Not C . In C, when I make the window, it has a problem with resizing. When I resize it to be bigger, it makes a black background with odd white patches in it. The only way to solve this is by making it the original size. It doesn't happen with C . How can I fix this? It compiles without errors.

At normal size: It displays correctly

Maximized: It makes a strange effect.

Code:
wmain.h

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

const wchar_t* szWndClassName = L"WindowClass"; const wchar_t* szWndName = L"Notepad";
int width = 600, height = 400;
HINSTANCE hInst; HWND hWnd;
WNDCLASS wc;

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

RECT rect;

int CenterWindow(HWND parent_window, int width, int height)
{
    GetClientRect(parent_window, &rect);
    rect.left = (rect.right / 2) - (width / 2);
    rect.top = (rect.bottom / 2) - (height / 2);
    return 0;
}

wmain.c

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include "wmain.h"

#pragma warning (disable: 28251)
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszCMDArgs, int nCMDShow)
{
    hInst = hThisInst;

    wc.lpszClassName = szWndClassName;
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInst;
    wc.hCursor = LoadCursor(wc.hInstance, L"IDC_ARROW");
    wc.hIcon = LoadIcon(wc.hInstance, L"Resource Files/Images/Notepad.ico");

    if (!RegisterClass(&wc))
    {
        MessageBox(NULL, L"RegisterClassW failed!", L"Error", MB_ICONERROR);

        return 1;
    }

    CenterWindow(GetDesktopWindow(), width, height);

    hWnd = CreateWindow(szWndClassName, szWndName, WS_OVERLAPPEDWINDOW, rect.left, rect.top, width, height, NULL, NULL, hInst, NULL);

    if (!hWnd)
    {
        MessageBox(NULL, L"CreateWindowW failed!", L"Error", MB_ICONERROR);

        return 2;
    }

    ShowWindow(hWnd, nCMDShow);
    UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }


    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
        case WM_CREATE:
            break;
        case WM_COMMAND:
            switch (wp)
            {

            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, msg, wp, lp);
    }
}

EDIT

To fix this, add this to your Window Procedure:

case WM_ERASEBKGND:
    wc.hbrBacground = (HBRUSH)(COLOR_WINDOW   1);
    break;

CodePudding user response:

Either set a window background in the WNDCLASS, or implement the WM_PAINT message to redraw the window.

  • Related