2014-07-11 99 views
0

这是我的代码。我试图创建一个编辑控件。但是没有显示。有人可以看看我的代码,并指出错误。我找不出错误在哪里。我觉得这可能与父母的孩子关系有关。窗口编辑控件不显示

#include <cstdlib> 
#include <windows.h> 
#define MAINWINDOW_CLASS_ID 140; 
const char* MAINWINDOW_CLASS_NAME = "Main Window"; 
const char* TEXTAREA_CLASS_NAME = "EDIT"; 
using namespace std; 


LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, 
          WPARAM wParam, LPARAM lParam); 
/* 
* Initialize the window class and register it. 
*/ 
BOOL initInstance(HINSTANCE hInstance); 
/* 
* Create and show the window 
*/ 
HWND initWindow(HINSTANCE hInstance); 

HWND createTextArea(HWND hParent); 
/* 
* 
*/ 
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprevInstance, LPSTR lpCmdline, INT cmdlShow)  
{ 
    if (!initInstance(hInstance)) 
     return 0; 
    HWND hwndMain = initWindow(hInstance); 
    ShowWindow(hwndMain, cmdlShow); 
    MSG msg = {} ; 

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

    return 0; 
} 
BOOL initInstance(HINSTANCE hInstance) 
{ 
    WNDCLASS wc = {}; 
    wc.lpfnWndProc = WinProc; 
    wc.hInstance = hInstance, 
    wc.lpszClassName = MAINWINDOW_CLASS_NAME; 

    return RegisterClass(&wc); 
} 
HWND initWindow(HINSTANCE hInstance) 
{ 
    HWND hwndMain = CreateWindow(
      MAINWINDOW_CLASS_NAME, // The class name 
      "Text Editor", // The window name 
      WS_OVERLAPPEDWINDOW,// The window style 
      CW_USEDEFAULT, // The x pos 
      CW_USEDEFAULT, // The y pos 
      CW_USEDEFAULT, // The width 
      CW_USEDEFAULT, // The height 
      (HWND) NULL, // Handle to parent 
      (HMENU) NULL, // Handle to the menu 
      hInstance, // Handle to the instance 
      NULL  // Window creation data 
      ); 

    if (!hwndMain) { 
     return NULL; 
    } 
    else { 
     return hwndMain; 
    } 


} 
LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, 
          WPARAM wParam, LPARAM lParam) { 
     HWND htextArea; 
     char sztestText[] = "I should be able to see this text."; 

    switch (uMsg) { 
     case WM_CREATE: 
      htextArea = createTextArea(hwnd); 
      SendMessage(htextArea, WM_SETTEXT, 0, (LPARAM) sztestText); 
      break; 
     case WM_PAINT: 
      break; 
     case WM_CLOSE: 
      break; 
     case WM_SIZE: 
      RECT rectMainWindow; 
      GetWindowRect(hwnd, &rectMainWindow); 
      INT x = rectMainWindow.right - rectMainWindow.left + 50; 
      INT y = rectMainWindow.bottom - rectMainWindow.top + 50; 

      MoveWindow(htextArea, 0, 0, x, y, TRUE); 
    } 

    return DefWindowProc(hwnd, uMsg, wParam, lParam); 
} 

/*******************************************************************/ 
HWND createTextArea(HWND hParent) { 
    return CreateWindow(
      TEXTAREA_CLASS_NAME, // Class control name 
      NULL,   // Title 
      WS_CHILD | WS_VISIBLE | ES_MULTILINE, // Styles 
      0, 0, 0, 0, // Sizing and position 
      hParent, 
      (HMENU) MAKEINTRESOURCE(100), 
      (HINSTANCE) GetWindowLong(hParent, GWL_HINSTANCE), 
      NULL  
      ); 
} 

回答

3

您已经定义htextAreaWinProc局部变量,所以每次你的窗口过程被称为它的价值将是未初始化。您应该将其设置为static或将其移至该函数之外的全局数据。

(实际的问题是,当您收到WM_SIZE消息时,您已经失去了编辑控件的句柄,因此它的大小仍然为0,0)。

+0

谢谢。整个信息系统我还没有包裹我的头。我真的应该更多地研究。 – jax