开发者

WinApi control doesn't show on the main window

hello I have the following win32 program, and I have an EDITTEXT control that doesn't show up on the screen. There is meant to be two EDITTEXT controls drawn on the main window, but only one shows, why is this?

full code

#include <windows.h>
#include <iostream>
#include "resource.h"
using namespace std;
const int BUFFERMAX = 512;
char server_ip[BUFFERMAX];
int port;


const char windclassname[] = "windowClass";

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPA开发者_如何学PythonRAM lParam);
INT_PTR CALLBACK AboutDialog(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
INT_PTR CALLBACK ConnectWin(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
    WNDCLASSEX parent_window;
    HWND hwnd;
    MSG msg;

    parent_window.cbSize = sizeof(WNDCLASSEX);
    parent_window.style = 0;
    parent_window.lpfnWndProc = WndProc;
    parent_window.cbClsExtra = 0;
    parent_window.cbWndExtra = 0;
    parent_window.hInstance = hInstance;
    parent_window.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    parent_window.hCursor = LoadCursor(NULL,IDC_ARROW);
    parent_window.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    parent_window.lpszMenuName = MAKEINTRESOURCE(ID_MENU);
    parent_window.lpszClassName = windclassname;
    parent_window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    //Registering the Class for use.
    if ( !RegisterClassEx(&parent_window)){
        MessageBox(NULL,"Could not Register WindowClass","Error",MB_OK);
        return 0;
    }

    //Creating the window
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,windclassname,"UI test",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,250,350,NULL,NULL,hInstance,NULL);
    if ( hwnd == NULL ){
        MessageBox(NULL,"Could not create window","Error",MB_OK);
        return 0;
    }

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

    while ( GetMessage(&msg,NULL,0,0) > 0 ) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
    switch (msg){
        case WM_CREATE:

            HWND chatbox_cntrl;
            HWND message_text;

            chatbox_cntrl = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
                WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
                0,0,240,260,hwnd,(HMENU)CHATBOX,GetModuleHandle(NULL),NULL);

            message_text = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
                WS_CHILD,0,270,240,10,hwnd,(HMENU)MESSAGETEXT,GetModuleHandle(NULL),NULL);

        break;
        case WM_COMMAND:
            switch(LOWORD(wParam)){
                case ID_MABOUT_INFO:
                    DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_MABOUT_INFO), hwnd,AboutDialog);
                break;

                case ID_MCONTROLS_CONNECT:
                    int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_MCONTROLS_CONNECT), hwnd,ConnectWin);
                break;
                case ID_MCONTROLS_EXIT:
                    DestroyWindow(hwnd);
                break;
            }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd,msg,wParam,lParam);

}

INT_PTR CALLBACK AboutDialog(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
    switch (msg){
        case WM_CLOSE:
            EndDialog(hwnd,WM_CLOSE);
        break;
        default:
            return false;
    }
    return true;

}

INT_PTR CALLBACK ConnectWin(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
    switch(msg){
        case WM_COMMAND:
            switch(wParam){
                case CONNECT_BUTTON:
                    int len = GetWindowTextLength(GetDlgItem(hwnd,EDITTEXT_CONNECT));
                    int recv = GetDlgItemText(hwnd,EDITTEXT_CONNECT,server_ip,len+1);
                    port = GetDlgItemInt(hwnd,EDITTEXT_PORT,NULL,false);
                    EndDialog(hwnd,CONNECT_BUTTON);
                break;
            }
        break;
        case WM_CLOSE:
            EndDialog(hwnd,WM_CLOSE);
        break;
        default:
            return false;
    }
    return true;
}

these are the two controls.

case WM_CREATE:

            HWND chatbox_cntrl;
            HWND message_text;

            chatbox_cntrl = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
                WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
                0,0,240,260,hwnd,(HMENU)CHATBOX,GetModuleHandle(NULL),NULL);

            message_text = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",
                WS_CHILD,0,270,240,10,hwnd,(HMENU)MESSAGETEXT,GetModuleHandle(NULL),NULL);

        break;


The message text window doesn't have the same style flags as the first. Specifically, it is missing WS_VISIBLE. This could well be your problem.


Did you call InitCommonControlsEx() before creating your edit windows? Does CreateWindow() return a valid HWND?

INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(icc);
// Load one of the intrinsic User32 control classes.
// The user controls include button, edit, static,
// listbox, combobox, and scrollbar. 
dwICC = ICC_STANDARD_CLASSES;
::InitCommonControlsEx(&icc);


Have you initialize the controls before load it with InitCommonControlsEx? Try this code one.

int InitControls() {
    INITCOMMONCONTROLSEX iccx;

    iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccx.dwICC = ICC_WIN95_CLASSES;

    InitCommonControlsEx(&iccx);
}

Remember to include header commctrl.h and library comctl32.dll

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜