childwc.lpfnWndProc in Eclipse

Below the code to create a child window, but how to really call ChildWndProc to make a child window? Now it is creating a child window using WndProc. Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{

	WNDCLASSEX wc, childwc;
    HWND hwnd, hwnd2;
    MSG Msg;

    //Step 1: Registering the Window Class
    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)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
    wc.hIcon  = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    wc.hIconSm  = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);


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

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Riffmaker Pro",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 2400, 1200,
        NULL, NULL, hInstance, NULL);

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

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

    /*if(!RegisterClassEx(&childwc))
        {
            MessageBox(NULL, "Window Registration2 Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }*/

    childwc.cbSize        = sizeof(WNDCLASSEX);
    childwc.style         = 0;
    childwc.lpfnWndProc   = ChildWndProc;
    childwc.cbClsExtra    = 0;
    childwc.cbWndExtra    = 0;
    childwc.hInstance     = hInstance;
    childwc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    childwc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    childwc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    childwc.lpszMenuName  = NULL;
    childwc.lpszClassName = g_szClassName2;
    childwc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    childwc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
    childwc.hIcon  = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    childwc.hIconSm  = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);

    hwnd2 = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName2,
            "The title of my childwindow",
			WS_CHILD | WS_VISIBLE | WS_CAPTION
            | WS_SYSMENU | WS_THICKFRAME
            | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
            CW_USEDEFAULT, CW_USEDEFAULT, 850, 600,
            hwnd, NULL, hInstance, NULL);

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

        ShowWindow(hwnd2, nCmdShow);
        UpdateWindow(hwnd2);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
Your question makes no sense to me.

However, you need to register the child class before you create a window from it.
You have the RegisterClassEx call commented out.
Also, it's in the wrong place since it needs to go after the code that fills in the childwc structure.

And I believe that the ShowWindow/UpdateWindow calls just need to be done for the parent window and should be done after you've created the child (just before the message loop).
Last edited on
When I do what you propose, I get the following error in a message box:
Window Registration2 Failed!
So can RegisterClassEx be called twice? What to do now?
Obviously you would need to post the exact code that you ran so I can see what you did wrong.
Last edited on
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPSTR lpCmdLine, int nCmdShow)
{

	WNDCLASSEX wc, childwc;
  HWND hwnd, hwnd2;
  MSG Msg;

  //Step 1: Registering the Window Class
  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)(COLOR_WINDOW+1);
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = g_szClassName;
  wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

  wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
  wc.hIcon  = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  wc.hIconSm  = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);


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

  // Step 2: Creating the Window
  hwnd = CreateWindowEx(
      WS_EX_CLIENTEDGE,
      g_szClassName,
      "Riffmaker Pro",
      WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT, 2400, 1200,
      NULL, NULL, hInstance, NULL);

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

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



  childwc.cbSize        = sizeof(WNDCLASSEX);
  childwc.style         = 0;
  childwc.lpfnWndProc   = ChildWndProc;
  childwc.cbClsExtra    = 0;
  childwc.cbWndExtra    = 0;
  childwc.hInstance     = hInstance;
  childwc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  childwc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  childwc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  childwc.lpszMenuName  = NULL;
  childwc.lpszClassName = g_szClassName2;
  childwc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

  childwc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
  childwc.hIcon  = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  childwc.hIconSm  = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);

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

  hwnd2 = CreateWindowEx(
          WS_EX_CLIENTEDGE,
          g_szClassName2,
          "The title of my childwindow",
			WS_CHILD | WS_VISIBLE | WS_CAPTION
          | WS_SYSMENU | WS_THICKFRAME
          | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
          CW_USEDEFAULT, CW_USEDEFAULT, 850, 600,
          hwnd, NULL, hInstance, NULL);

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

      ShowWindow(hwnd2, nCmdShow);
      UpdateWindow(hwnd2);

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


So RegisterClassEx is called twice, which causes the problem. How to continue?
I really can't help you since I don't use windows currently. However, there's nothing wrong with calling RegisterClassEx more than once. There's probably something wrong with the way you've filled in the window class structure. (E.g., can a child window have a menu?)

This isn't your current problem, but you also didn't fix the ShowWindow/UpdateWindow error. They should only be called once, only on the parent window, and just before the message loop.
I solved it now.

const char g_szClassName[] = "myWindowClass";
const char g_szClassName2[] = "myWindowClass";


must be

const char g_szClassName[] = "myWindowClass";
const char g_szClassName2[] = "myWindowClass2";
Topic archived. No new replies allowed.