user1754835
user1754835

Reputation: 23

WinApi, Push button are not displayed

I'm trying to create simple windows app uisng WinAPi. Here is the code:

       #include "stdafx.h"
       #include "APIup.h"

       #define MAX_LOADSTRING 100
       HWND hWnd,cw13;
       // Global Variables:
       HINSTANCE hInst;                             // current                      instance
       TCHAR szTitle[MAX_LOADSTRING];                   // The title bar text
       TCHAR szWindowClass[MAX_LOADSTRING];         // the main window class name

       // Forward declarations of functions included in this code module:
       ATOM             MyRegisterClass(HINSTANCE hInstance);
       ATOM Register(HINSTANCE hInstance);
       BOOL             InitInstance(HINSTANCE, int);
       LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
       LRESULT CALLBACK    WndProc_dla13(HWND, UINT, WPARAM, LPARAM);
       INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

       int APIENTRY _tWinMain(HINSTANCE hInstance,
                            HINSTANCE hPrevInstance,
                            LPTSTR    lpCmdLine,
                            int       nCmdShow)
       {
        UNREFERENCED_PARAMETER(hPrevInstance);
        UNREFERENCED_PARAMETER(lpCmdLine);

            // TODO: Place code here.
        MSG msg;
                HACCEL hAccelTable;

        // Initialize global strings
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
        LoadString(hInstance, IDC_APIUP, szWindowClass, MAX_LOADSTRING);
        MyRegisterClass(hInstance);
        Register(hInstance);

        // Perform application initialization:
        if (!InitInstance (hInstance, nCmdShow))
        {
            return FALSE;
        }

        hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_APIUP));

        // Main message loop:
        while (GetMessage(&msg, NULL, 0, 0))
        {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

        return (int) msg.wParam;
       }





        ATOM MyRegisterClass(HINSTANCE hInstance)
        {
            WNDCLASSEX wcex;

            wcex.cbSize = sizeof(WNDCLASSEX);

            wcex.style          = CS_HREDRAW | CS_VREDRAW;
            wcex.lpfnWndProc    = WndProc;
            wcex.cbClsExtra     = 0;
            wcex.cbWndExtra     = 0;
            wcex.hInstance      = hInstance;
            wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APIUP));
            wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
            wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
            wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_APIUP);
            wcex.lpszClassName  = szWindowClass;
            wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));


            return RegisterClassEx(&wcex);
               }

               ATOM Register(HINSTANCE hInstance)
               {
            WNDCLASSEX cw13class;

            cw13class.cbSize = sizeof(WNDCLASSEX);

            cw13class.style         = CS_HREDRAW | CS_VREDRAW;
            cw13class.lpfnWndProc   = WndProc_dla13;
            cw13class.cbClsExtra        = 0;
            cw13class.cbWndExtra        = 0;
            cw13class.hInstance     = hInstance;
            cw13class.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APIUP));
            cw13class.hCursor       = LoadCursor(NULL, IDC_ARROW);
            cw13class.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
            cw13class.lpszMenuName  = NULL;
            cw13class.lpszClassName = L"13class";
            cw13class.hIconSm       = LoadIcon(cw13class.hInstance,                               MAKEINTRESOURCE(IDI_SMALL));
            return RegisterClassEx(&cw13class);

        }

        BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
        {




           hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
              CW_USEDEFAULT, 0, 100, 100, NULL, NULL, hInstance, NULL);
           cw13=CreateWindow(L"13class",L"Ćwiczenie                         13",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,300,300,NULL,NULL,hInst,NULL);


           if (!hWnd)
           {
              return FALSE;
           }

          ShowWindow(hWnd, nCmdShow);
           UpdateWindow(hWnd);

           return TRUE;
        }


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:

        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        case ID_32771:
            ShowWindow(cw13, 1);
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

LRESULT CALLBACK WndProc_dla13 (HWND hWnd, UINT msg/*message*/, WPARAM  wParam,LPARAM lParam)
        {
 switch(msg)  
  {
    case WM_CREATE:

      CreateWindowW(L"button", L"Beep",
          WS_VISIBLE | WS_CHILD ,
          20, 50, 80, 25,
          cw13, (HMENU) 1, hInst, NULL);

      CreateWindowW(L"button", L"Quit",
          WS_VISIBLE | WS_CHILD ,
          120, 50, 80, 25,
          cw13, (HMENU) 2, NULL, NULL);
      break;

    case WM_COMMAND:

      if (LOWORD(wParam) == 1) {
        Beep(40, 50);
      }

      if (LOWORD(wParam) == 2) {
        PostQuitMessage(0);
      }

      break;

    case WM_DESTROY:

      PostQuitMessage(0);
      break;
      default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
  }
 return 0;
}




// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

And the problem is that: when i create window 'cw13' it is created and displayed, but push buttons are not.I created two separate WndProc functions. Other messages are executed correctly( like WM_QUIT), only CreateWindowW under WM_CREATE,witch should create and show Push buttons aren't. Where is the problem?

Upvotes: 2

Views: 425

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595402

When processing the WM_CREATE message, your call to CreateWindow(L"13class") is still running. Your cw13 variable has not been assigned yet, so you are assigning an invalid HWND as the parent of your buttons. That is why they do not appear. Use the hwnd parameter of WndProc_dla13() instead as the parent. That will be the same HWND that gets assigned to the cw13 variable once CreateWindow(L"13class") exits.

Upvotes: 1

Related Questions