G
G
German2019-04-08 19:27:47
C++ / C#
German, 2019-04-08 19:27:47

WinAPI child window not created or CreateWindows returns null?

There is such a code
#include <Windows.h>
#include <wchar.h>

using namespace std;

#define ID_FIRSTCHILD 100

HINSTANCE hInst;

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

wchar_t WinName[] = L"MainFrame";
HWND child;

int WINAPI WinMain(
  HINSTANCE This,
  HINSTANCE Prev,
  LPSTR cmd,
  int mode
)
{
  HWND hWnd;
  MSG msg;
  WNDCLASS wc;
  wc.hInstance = This;
  hInst = This;
  wc.lpszClassName = WinName;
  wc.lpfnWndProc = WndProc;
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.lpszMenuName = NULL;
  wc.cbClsExtra = NULL;
  wc.cbWndExtra = NULL;
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  if (!RegisterClass(&wc)) return NULL;
  int WindowWidth = 800;
  int WindowHeight = 800;
  int ScreenWidth = GetSystemMetrics(SM_CXSCREEN) - WindowWidth;
  int ScreenHeight = GetSystemMetrics(SM_CYSCREEN) - WindowHeight;
  hWnd = CreateWindow(
    WinName,
    L"Title",
    WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX,
    ScreenWidth / 2,
    ScreenHeight / 2,
    WindowWidth,
    WindowHeight,
    HWND_DESKTOP,
    NULL,
    This,
    NULL
  );
  ShowWindow(hWnd, mode);
  while (GetMessage(&msg, NULL, NULL, NULL))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return NULL;
}

LRESULT CALLBACK WndProc(
  HWND hWnd,
  UINT message,
  WPARAM wParam,
  LPARAM lParam
)
{
  switch (message)
  {
  case WM_CREATE:
    WNDCLASS w;
    memset(&w, 0, sizeof(WNDCLASS));
    w.lpszClassName = L"ChildMain";
    w.lpfnWndProc = ChildProc;
    w.hInstance = hInst;
    w.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    w.style = CS_HREDRAW | CS_VREDRAW;
    w.hCursor = LoadCursor(NULL, IDC_CROSS);
    w.lpszMenuName = NULL;
    w.cbClsExtra = NULL;
    w.cbWndExtra = NULL;
    if (!RegisterClass(&w))
    {
      MessageBox(NULL, L"Class not register", L"", MB_OK);
      return NULL;
    }
    child = CreateWindow(
      L"ChildWnd",
      L"ChildWindow",
      WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX,
      10,
      10,
      500,
      300,
      hWnd,
      NULL,
      hInst,
      NULL
    );
    if (!child)
    {
      MessageBox(NULL, L"А child нулевой", L"", MB_OK);
      return NULL;
    }
    ShowWindow(child, SW_NORMAL);
    UpdateWindow(child);
    break;
  case WM_DESTROY:
    PostQuitMessage(NULL);
    break;
  default: return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return NULL;
}

LRESULT CALLBACK ChildProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
  if (Message == WM_CREATE)
  {
    MessageBox(NULL, L"Created", L"", MB_OK);
  }
  if (Message == WM_DESTROY)
  {
    MessageBox(NULL, L"Destroy", L"", MB_OK);
    return NULL;
  }
  return DefWindowProc(hwnd, Message, wparam, lparam);
}


The error "A child is null" is falling, why is this happening?
Sorry for the bad code.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SerJook, 2019-04-08
@SerJook

Maybe because you are registering a class named "ChildMain" and trying to create a window of class "ChildWnd"??

N
NewDevLab, 2019-04-08
@NewDevLab

WinName - because no such class is registered?

S
Sumor, 2019-04-08
@Sumor

To understand what kind of error, call GetLastError additionally.
At the time of processing WM_CREATE, the window has not yet been fully created, there may be a problem with this.
Child windows most often mean panels or controls. Try to create a child window not with your own class, but, for example, a button or a text box.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question