2013-08-24 5 views
0

을 만들 문제는 내가 만들 번째 컨트롤이 표시되지 않는다는 것입니다. 는 말은 : 첫째 될 것C++ WINAPI 불가능 내가 리치 에디트 컨트롤과 목록 상자 컨트롤을 포함하는 창을 만들려고 해요 2 개 제어

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; 
} 

기능 InitListClients 경우에만 목록 상자가 먼저 될 것입니다 InitEditLog 경우에만 리치 에디트가 표시됩니다, 표시됩니다.

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); 
} 

내가 좀 WINAPI와 안돼서 내가이 문제에 대한 해결책을 찾을 수 없습니다 :

다음은 함수입니다. 감사합니다. .

편집 : 주석으로 이 문제의 원인 반원의 사용이다. 여기 은 내가 쓴 한 전체 코드를과 같은 문제가 있습니다 (!하지만 제어 처리기를 설정하지 않음) WM_CREATE 메시지 컨트롤을 생성하여 해결

#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; 
} 
+0

코드의 첫 번째 블록이 아닌 이상하다. 우선,'this' 포인터는 C++에서 대문자로 쓰이지 않습니다. 그래서 저는이 변수가 윈도우를 나타내는 C++ 객체를 가리키는 이름이라고 가정합니다. 그런데 왜 멤버 변수로 액세스하지 않고 윈도우 핸들을 매개 변수로 전달할 것입니까? 어쨌든, 당신의 코드를 깨뜨리는 것이 아니라, 퍼즐을 푸는 것입니다. –

+0

또 다른 문제는 2013 년 임에도 불구하고 8 바이트 ANSI 문자열을 사용하고 있다는 것입니다. 모든 Windows 응용 프로그램은 이제 유니 코드 여야합니다. 'UNICODE' 심볼이 정의되어 있고 문자열 리터럴의 접두어가'L'인지 확인하십시오. –

+0

코드는 나를 위해 잘 작동하지만. 문제는 여기서 우리에게 보여준 것과 다른 것입니다. 새 프로젝트를 만들고 한 번에 하나씩 코드 조각을 추가하여 직접 디버깅 할 수 있습니다. –

답변

0

문제를, 그리고 이후의 제어 처리기를 설정 메인 윈도우의 생성.

WM_CREATE 메시지 : 메인 윈도우 생성 후

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; 
    } 

:

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); 

InitEditLogInitListClients 함수 :

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); 
}