Home > Software design >  How to reopen the winapi window?
How to reopen the winapi window?

Time:09-15

I have WinAPI application with menu. I click "Graphics" and choose open or draw. It doesn't matter what exactly. Then I close the child window. When I try to open it again, it doesn't work. Maybe I should put somewhere ShowWindow(hWnd, SW_HIDE). But I don't understand, where it should be. Maybe there is another solution

main window

Here I'll put my code:

Callbacks, which I use

LRESULT CALLBACK DrawProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
        case WM_PAINT:
        {
            ...
        }
        case WM_LBUTTONDOWN:
        {
            ...
        }
    }
    return DefWindowProc(hWnd, msg, wp, lp);
}


LRESULT CALLBACK GraphProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
        case WM_PAINT:
        {
            ...
        }
        break;
        case WM_CREATE:
            ...
        case WM_SIZE:
            ...
    }
    return DefWindowProc(hWnd, msg, wp, lp);
}
LRESULT CALLBACK SoftwareMainProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_COMMAND:
        switch (wp)
        {
        case draw_plot:
            GraphClass.style          = CS_HREDRAW | CS_VREDRAW;
            GraphClass.lpfnWndProc    = DrawProcedure;
            GraphClass.hInstance      = hInst;
            GraphClass.lpszMenuName  = NULL;
            GraphClass.lpszClassName = L"graphics";

            if (!RegisterClassW(&GraphClass))
            {
                return -1;
            }

            gr_draw = CreateWindow(L"graphics", L"DRAW", WS_VISIBLE | WS_BORDER | WS_MAXIMIZE | WS_HSCROLL | WS_VSCROLL | WS_OVERLAPPEDWINDOW, 0, 0, 800, 700, NULL, NULL, hInst, NULL);
            break;

        case open_plot:
            GraphClass.style          = CS_HREDRAW | CS_VREDRAW;
            GraphClass.lpfnWndProc    = GraphProcedure;
            GraphClass.hInstance      = hInst;
            GraphClass.lpszMenuName  = NULL;
            GraphClass.lpszClassName = L"graphics";

            if (!RegisterClassW(&GraphClass))
            {
                return -1;
            }

            gr_open = CreateWindow(L"graphics", L"OPEN", WS_VISIBLE | WS_BORDER | WS_MAXIMIZE | WS_OVERLAPPEDWINDOW, 0, 0, 800, 700, NULL, NULL, hInst, NULL);
            break;
     
        case OnExitSoftware:
            PostQuitMessage(0);
            break;
        default:
            break;
        }
        break;

    case WM_SIZE:
    {
        ...
    }
    break;
    case WM_CREATE:
        MainWndAddMenus(hWnd);
        MainWndAddWidgets(hWnd);
        break;
    case WM_DESTROY: // close mainwindow
        ExitSoftware();
        break;
    default:
        return DefWindowProc(hWnd, msg, wp, lp);
    }
}

CodePudding user response:

RegisterClassW(&GraphClass) doesn't work the second time, because the window class is already registered, because you already registered it the first time.

It returns false to tell you that it didn't work, then your code doesn't open the window. To reiterate: You told the computer, that if RegisterClassW(&GraphClass) doesn't work, then it shouldn't open the window.

Solution: Either register the window class the first time you use it (not every time), or register all the window classes when the program starts.

Side question to think about: Why did you tell the computer to do nothing if RegisterClassW(&GraphClass) doesn't work? If you told it to pop up a message box saying "RegisterClassW(&GraphClass) didn't work", you'd know where the problem was, because the message box would tell you.

  • Related