这个变量怎么没有定义?

时间:2014-02-18 22:50:23

标签: c++ winapi

这是我目前代码的代码段。这绝对没有意义。它表示CASE COMMAND下的hEditEW未定义在上面明确定义的位置。这可能是一个非常简单的修复,但我有一点时间解决它。

case WM_CREATE:
         {
                  HWND hWndButton = CreateWindowEx(NULL, "Button", "Click to Convert", WS_TABSTOP|WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                      200, 370, 150, 50, hWnd, (HMENU)IDC_BUTTON_ONE, NULL, NULL);

                  HWND hEditEW = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD|ES_MULTILINE| WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 25, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);

                  HWND hEditNS = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD | ES_MULTILINE | WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 300, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);
         }
    break;

case WM_COMMAND:
       {
                   switch (LOWORD(wParam))
                   {
                   case IDC_BUTTON_ONE:
                   {
                                          LPWSTR buffer[256];
                                          SendMessage(hEditEW,
                                              WM_GETTEXT,
                                              sizeof(buffer) / sizeof(buffer[0]),
                                              reinterpret_cast<LPARAM>(buffer));
                                          HANDLE hFile = CreateFile("C:\\test.txt", GENERIC_READ,
                                              0, NULL, CREATE_NEW, FILE_FLAG_OVERLAPPED, NULL); 

                   }

                       break;
                   }
                   break;
      }

3 个答案:

答案 0 :(得分:5)

您在case WM_CREATE之后的范围内定义它,并尝试在case WM_COMMAND之后的范围内使用它。这不起作用 - 不同的范围。

您甚至不需要切换来演示问题:

{
   HWND hEditEW = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD|ES_MULTILINE| WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 25, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);

}  //in scope up to here
{
   SendMessage(hEditEW,
      WM_GETTEXT,
      sizeof(buffer) / sizeof(buffer[0]),
      reinterpret_cast<LPARAM>(buffer));
}

答案 1 :(得分:0)

hEditEW是本地的 把HWND hEditNS;在顶部并替换

WND hEditNS = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD | ES_MULTILINE | WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 300, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);

hEditNS = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD | ES_MULTILINE | WS_VISIBLE |
                      ES_AUTOHSCROLL | ES_AUTOVSCROLL, 300, 50, 220, 300, hWnd, (HMENU)IDC_EDITOR_BOX_EAST_WEST, NULL, NULL);

如果你把HWND hEditNS放到了应该修复它的地方去除WND;在类/函数的顶部

答案 2 :(得分:0)

我不知道WinApi,但hEditEW超出了范围:

case WM_CREATE:
{
     HWND hEditEW = ....
} //end of scope

简单修复:将hEditEW放在较高范围内:

HWND hEditEW;
case WM_CREATE:
{ .....