user4419205
user4419205

Reputation:

WinAPI multiple controls issue

I'm trying to create a window holding a button control and an edit control. I create the controls using two functions, one for the button control and one for the edit control.

The problem I get is that I can't have both controls on the window at the same time. It's only the control which is initialized in the first function that is displayed on the window.

Here is the code: main.c

LRESULT CALLBACK WindowProc(HWND hWindow, unsigned int message, WPARAM wParam, LPARAM lParam)
{
    HWND* hButtonConvert = NULL;
    HWND* hEditNumber = NULL;

    switch (message)
    {
        case WM_CREATE:
        {
            CreateEditControls(hWindow, hEditNumber); // Only this functions works
            CreateButtonControls(hWindow, hButtonConvert); // hButtonConvert is not displayed
        }
        break;

        ...
    }

    return 0;
}

gui.c

#include <windows.h>
#include "gui.h"

void CreateButtonControls(HWND hWindow, HWND* hButtonConvert)
{
    const char* buttonConvertCaption = "Convert";
    const unsigned int buttonConvertXPosition = 100;
    const unsigned int buttonConvertYPosition = 10;
    const unsigned int buttonConvertWidth = 125;
    const unsigned int buttonConvertHeight = 50;
    const DWORD buttonConvertStyle = WS_CHILD | WS_VISIBLE;

    *hButtonConvert = CreateWindowEx(WS_EX_CLIENTEDGE, "Button", buttonConvertCaption, buttonConvertStyle, buttonConvertXPosition, buttonConvertYPosition, buttonConvertWidth, buttonConvertHeight, hWindow, (HMENU)IDB_CONVERT, NULL, NULL);;
}

void CreateEditControls(HWND hWindow, HWND* hEditNumber)
{
    const char* editNumberCaption = NULL;
    const unsigned int editNumberXPosition = 10;
    const unsigned int editNumberYPosition = 10;
    const unsigned int editNumberWidth = 50;
    const unsigned int editNumberHeight = 30;
    const DWORD editNumberStyle = WS_CHILD | WS_VISIBLE | WS_BORDER;

    *hEditNumber = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", editNumberCaption, editNumberStyle, editNumberXPosition, editNumberYPosition, editNumberWidth, editNumberHeight, hWindow, (HMENU)IDE_NUMBER, NULL, NULL);
}

Upvotes: 1

Views: 42

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

hButton and hEdit should not be declared as pointers, and maybe you need to put them in global scope if you intend to use them elsewhere.

HWND hButtonConvert = NULL;
HWND hEditNumber = NULL;

LRESULT CALLBACK WindowProc(HWND hWindow...)
{
    switch (message)
    {
        case WM_CREATE:
        {
        CreateEditControls(hWindow, &hEdit);
        CreateButtonControls(hWindow, &hButtonConvert);
        ...
        break;
        }
}

Alternative:

If child window has its own ID then you don't need to save its HWND

void CreateEditControls(HWND hWindow)
{
    const int x = 10;
    const int y = 10;
    const int w = 50;
    const int h = 30;
    const DWORD style = WS_CHILD | WS_VISIBLE | WS_BORDER;
    CreateWindowEx(0, "Edit", 0, style, x, y, w, h, hWindow, (HMENU)IDE_NUMBER, NULL, NULL);
}

In another scope:

HWND temporary = GetDlgItem(hWindow, IDE_NUMBER);
SetWindowText(temporary, "hey there");

Upvotes: 1

Related Questions