سؤال

I'm trying to create window that contain richedit control and listbox control, the problem is that the second control which I create, doesn't show up. I mean:

case WM_CREATE: // In main window procedure
{
    /* Center the main window */
    This->CenterWindow(hwnd);

    /* Initialize the clients list */
    This->InitListClients(hwnd);

    /* Initialize the server log */
    This->InitEditLog(hwnd);

    return 0;
}

If InitListClients function will be first, only the listbox will show up, if InitEditLog will be first, only the richedit will show up.

Here are the functions:

void ApostleServer::InitEditLog(HWND &_hwnd)
{
    LoadLibrary(TEXT("Riched32.dll"));
    hEditLog = CreateWindowEx(WS_EX_STATICEDGE, "richedit", "bla", WS_CHILD | WS_VISIBLE | ES_MULTILINE, 10, 10, 390, 310, _hwnd, NULL, (HINSTANCE)GetWindowLong(_hwnd, GWL_HINSTANCE), NULL);
}

void ApostleServer::InitListClients(HWND &_hwnd)
{
    hListClients = CreateWindowEx(WS_EX_STATICEDGE, "listbox", "bla", WS_CHILD | WS_VISIBLE | LBS_NOTIFY, 550, 20, 150, 150, _hwnd, NULL, (HINSTANCE)GetWindowLong(_hwnd, GWL_HINSTANCE), NULL);
}

I'm kinda newbie with winapi and I couldn't find solution for this problem. Thanks.

EDIT: As I commented, the cause of the problem is the use of class members. Here is a whole code that I've wrote and has the same problem:

#include <Windows.h>
#include <stdlib.h>

class Server
{
public:

    /* Fields */
    MSG* msg;
    WNDCLASSW* wc;
    HWND hListClients;
    HWND hEditLog;

    /* Methods */
    void InitEditLog(HWND &_hwnd)
    {
        LoadLibrary(TEXT("Riched32.dll"));
        hEditLog = CreateWindowExW(WS_EX_STATICEDGE, L"richedit", L"Text", WS_CHILD | WS_VISIBLE | ES_MULTILINE, 10, 10, 390, 306, _hwnd, (HMENU)2, (HINSTANCE)GetWindowLong(_hwnd, GWL_HINSTANCE), NULL);
    }

    void InitListClients(HWND &_hwnd)
    {
        // Here I'm using hListClients class member, and that what cause the problem (I will see only the list on the window)
        hListClients = CreateWindowExW(WS_EX_STATICEDGE, L"listbox", L"asd", WS_CHILD | WS_VISIBLE | LBS_NOTIFY, 410, 10, 160, 306, _hwnd, (HMENU)1, (HINSTANCE)GetWindowLong(_hwnd, GWL_HINSTANCE), NULL);
        // If I was only creating the listbox (without returning handler), I will see the listbox and the richedit.
    }

    static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {

        Server* This = (Server*)GetWindowLongW(hwnd, GWL_USERDATA);

        switch(msg)
        {
            case WM_CREATE:
            {
                /* Initialize the clients list */
                This->InitListClients(hwnd); // Attention that I called this function first.

                /* Initialize the server log */
                This->InitEditLog(hwnd);
                // If I would call this function first, I will see only the richedit.

                return 0;
            }

            case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            }
        }

        return DefWindowProcW(hwnd, msg, wParam, lParam);
    }

    Server(HINSTANCE &_hInstance)
    {
        msg = new MSG;
        wc = new WNDCLASSW;
        wc->style = CS_HREDRAW | CS_VREDRAW;
        wc->cbClsExtra = 0;
        wc->cbWndExtra = 0;
        wc->lpszClassName = L"ApostleServer";
        wc->hInstance = _hInstance;
        wc->hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc->lpszMenuName = NULL;
        wc->lpfnWndProc = WndProc;
        wc->hCursor = LoadCursor(NULL, IDC_ARROW);
        wc->hIcon = LoadIcon(NULL, IDI_APPLICATION);

        RegisterClassW(&(*wc));
        CreateWindowW(wc->lpszClassName, L"Apostle Server", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 600, 400, 0, 0, _hInstance, 0);

        while(GetMessage(&(*msg), NULL, 0, 0))
        {
            TranslateMessage(&(*msg));
            DispatchMessage(&(*msg));
        }

    }
};

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    Server* srvr = new Server(hInstance);
    return 0;
}
هل كانت مفيدة؟

المحلول

Problem solved by creating the controls on WM_CREATE message (but not to set control handlers!), and set the control handlers after the creation of the main window.

WM_CREATE message:

    case WM_CREATE:
    {
        /* Center the main window */
        This->CenterWindow(hwnd);

        /* Initialize the clients list */
        This->InitListClients(hwnd);

        /* Initialize the server log */
        This->InitEditLog(hwnd);


        return 0;
    }

After main window creation:

RegisterClassW(&(*wc));
hMainWindow = CreateWindowW(wc->lpszClassName, L"Apostle Server", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 600, 400, 0, 0, _hInstance, 0);

/* Set controls handlers */
hListClients = GetDlgItem(hMainWindow, IDC_LISTCLIENTS);
hEditLog = GetDlgItem(hMainWindow, IDC_EDITLOG);

InitEditLog and InitListClients functions:

void ApostleServer::InitEditLog(HWND &_hwnd)
{
    LoadLibrary(TEXT("Riched32.dll"));
    CreateWindowExW(WS_EX_STATICEDGE, L"richedit", L"Text", WS_CHILD | WS_VISIBLE | ES_MULTILINE, 10, 10, 390, 306, _hwnd, (HMENU)IDC_EDITLOG, (HINSTANCE)GetWindowLong(_hwnd, GWL_HINSTANCE), NULL);
}

void ApostleServer::InitListClients(HWND &_hwnd)
{
    CreateWindowExW(WS_EX_STATICEDGE, L"listbox", L"asd", WS_CHILD | WS_VISIBLE | LBS_NOTIFY, 410, 10, 160, 306, _hwnd, (HMENU)IDC_LISTCLIENTS, (HINSTANCE)GetWindowLong(_hwnd, GWL_HINSTANCE), NULL);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top