窗口编辑控件不显示

时间:2014-07-11 21:11:01

标签: windows winapi

这是我的代码。我正在尝试创建一个编辑控件。然而,它没有显示出来。有些人可以看看我的代码,并指出错误。我无法弄清楚错误在哪里。我觉得这可能与父母的孩子关系有关。

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

1 个答案:

答案 0 :(得分:3)

您已将htextArea定义为WinProc中的局部变量,因此每次调用窗口过程时,其值都将为未初始化。您应该将其设为static或将其移动到函数外部的全局数据。

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