2011-04-16 107 views
5

獲取文字我嘗試這樣做:編輯控件

int editlength; 
int buttonid = 3324; // id to button, the numbers dont mean anything 
int editid = 5652; // id to edit 

LPTSTR edittxt; 

HWND button; // created in wWinmain as a button 
HWND edit; // created in wWinMain as an edit control 

// LRESULT CALLBACK WindowProc 

switch(uMsg) 
{ 
    case WM_COMMAND: 
     if(wParam == buttonid) 
     { 
      filedit = GetDlgItem(hwnd, editid); // I tried with and without this 
      editlength = GetWindowTextLength(filedit); 
      GetWindowText(filedit, edittxt, editlength); 

      MessageBox(hwnd, edittxt, L"edit text", 0); 
     } 
     break; 
} 

,但我得到沒有看到在消息框中的任何文本。

回答

14

GetWindowText()的最後一個參數是緩衝區的大小。由於您將其設置爲字符串的長度,因此您正在告訴函數緩衝區太小,因爲空終止符沒有空間。沒有任何東西被複制。

此外,您必須已經分配緩衝區來保存文本的副本。 edittxt指向什麼?我什至不知道你在哪裏初始化它。

正確使用會是這個樣子:

TCHAR buff[1024]; 
GetWindowText(hWndCtrl, buff, 1024); 
4

edittxt需要有一個指針獲取文本的緩衝區的..所以試試這個...

char txt[1024]; 
.... 
GetWindowText(filedit, txt, sizeof(txt)); 

你可能有調整unicode ..對不起,因爲我做了原始的win32。

+0

謝謝,但我認爲接受的答案應該去@jonathan,因爲他首先得到:) – 2011-04-16 23:47:30