Initialize m_hwnd

kaveh rahimi 61 Reputation points
2021-10-18T08:39:39.69+00:00

Hi , I've written a windows desktop application code to create a window I want create a button on it and I've used this statement:
HWND hwndButton = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"OK", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
20, // x position
20, // y position
100, // Button width
100, // Button height
m_hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLongPtr(m_hwnd, GWLP_HINSTANCE),
NULL); // Pointer not needed.
how can I intialize m_hwnd?
Please help
Thanks

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,546 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 40,856 Reputation points
    2021-10-18T08:46:52.01+00:00

    Presumably you are creating your button control when the parent window is handling the WM_CREATE message. If that is the case, then the HWND for the parent is available since it was passed to the parent window procedure as the very first parameter. Use that HWND in the call to CreateWindow for the child button control parent.

    If you are doing something different then provide more information about how you are creating the parent window and when and where the child button control is to be created.


  2. Castorix31 81,831 Reputation points
    2021-10-18T08:59:05.497+00:00

    I use this template when I want to test Win32 APIs , with a Button and a Click event where I add code :

    #include <windows.h>
    #include <tchar.h>
    
    //#include <commctrl.h>
    //#pragma comment (lib, "comctl32")
    
    //#include "gdiplus.h"
    //using namespace Gdiplus;
    //#pragma comment(lib, "gdiplus.lib")
    
    #pragma comment(linker,"\"/manifestdependency:type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
    
    HINSTANCE hInst;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    int nWidth = 600, nHeight = 400;
    #define IDC_STATIC 10
    #define IDC_BUTTON 11
    
    int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
    {
        //GdiplusStartupInput gdiplusStartupInput;
        //ULONG_PTR gdiplusToken;
        //GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
        hInst = hInstance;
        WNDCLASSEX wcex =
        {
            sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInst, LoadIcon(NULL, IDI_APPLICATION),
            LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1), NULL, TEXT("WindowClass"), NULL,
        };
        if (!RegisterClassEx(&wcex))
            return MessageBox(NULL, TEXT("Cannot register class !"), TEXT("Error"), MB_ICONERROR | MB_OK);
        int nX = (GetSystemMetrics(SM_CXSCREEN) - nWidth) / 2, nY = (GetSystemMetrics(SM_CYSCREEN) - nHeight) / 2;
        HWND hWnd = CreateWindowEx(0, wcex.lpszClassName, TEXT("Test"), WS_OVERLAPPEDWINDOW, nX, nY, nWidth, nHeight, NULL, NULL, hInst, NULL);
        if (!hWnd)
            return MessageBox(NULL, TEXT("Cannot create window !"), TEXT("Error"), MB_ICONERROR | MB_OK);
        ShowWindow(hWnd, SW_SHOWNORMAL);
        UpdateWindow(hWnd);
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static HWND hWndButton = NULL, hWndStatic = NULL;
        int wmId, wmEvent;
        switch (message)
        {
        case WM_CREATE:
        {
            // hWndStatic = CreateWindowEx(0, TEXT("Static"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_BITMAP, 10, 10, 200, 200, hWnd, (HMENU)IDC_STATIC, hInst, NULL);
            hWndButton = CreateWindowEx(0, L"Button", L"Click", WS_CHILD | WS_VISIBLE | BS_PUSHLIKE, 100, 60, 60, 32, hWnd, (HMENU)IDC_BUTTON, hInst, NULL);
    
            return 0;
        }
        break;
        case WM_COMMAND:
        {
            wmId = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            switch (wmId)
            {
            case IDC_BUTTON:
            {
                if (wmEvent == BN_CLICKED)
                {
                    Beep(1000, 10);
                }
            }
            break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hDC = BeginPaint(hWnd, &ps);
    
            EndPaint(hWnd, &ps);
        }
        break;
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
        break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }
    
    0 comments No comments