2013-12-20 31 views
2

我想從win32 API使用SendMessage獲取記事本窗口的文本框上的文本。我首先找到窗口句柄,然後用SendMessage(hwndEdit,WM_GETTEXT,(WPARAM)bufferSize,(LPARAM)textBuffer)獲取文本。出於某種原因,即使它可以告訴我正確的文本長度,程序也只會返回記事本文本的1個字符,即使我有1024個緩衝區大小,它應該返回。我查看了我找到的例子,而且我的做法與示例相同。我不知道爲什麼會發生這種情況,有人可以幫我解決或指出我的錯誤嗎?從另一個窗口上的控件獲取文本的問題

#include <Windows.h> 
#include <iostream> 

int main() 
{ 
    printf("finding notepad window\n"); 
    HWND hwndNotepad = FindWindow(NULL, L"Untitled - Notepad"); 
    if(NULL != hwndNotepad) 
    { 
     printf("Find edit control window\n"); 
     HWND hwndEdit = FindWindowEx(hwndNotepad, NULL, L"EDIT", NULL); 

     if(NULL != hwndEdit) 
     { 
      printf("- get text length\n"); 
      int textLen = (int)SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0); 
      printf("textlength: %d\n", textLen); 
      if(0 < textLen) 
      { 
       const int bufferSize = 1024; 
       char textBuffer[bufferSize] = ""; 
       SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer); 
       printf("getting text:\n"); 
       printf("%s\n", textBuffer); 
      } 
      else 
      { 
       printf("its empty\n"); 
      } 
     } 
     else 
     { 
      printf("I cant find this control\n"); 
     } 
    } 
    else 
    { 
     printf("I cant find notepad window. \n"); 
    } 
    return 0; 
} 

截圖:http://i.imgur.com/bUhVqlq.png

回答

2

這可能是由於這樣的事實,記事本是使用UNICODE。試試這個

int copied = SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer); 
printf("Copied %d chars.\n", copied); 

找出您的被調用者認爲它複製了多少個字符。試試下面的打印Unicode文本:

const int bufferSize = 1024; 
wchar_t textBuffer[bufferSize] = ""; 
int copied = SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer); 
printf("Copied %d chars.\n", copied); 
printf("getting text:\n"); 
wprintf(L"%ls \n", textBuffer); 

如果您正在使用Visual Studio,它可能是值得一試,進入你的項目選項(右鍵項目 - >配置屬性 - >常規 - >字符集)並將其設置爲ANSI(「未設置」)。

+0

記事本是否被編譯爲Unicode或不?我在某處讀到win32 api的ansi版本將unicode數據轉換爲它的ansi等價物。 – user2699298

1

無需更改項目設置。您可以使用TCHAR而不是使用char

#include <Windows.h> 
#include <iostream> 

int main() 
{ 
    printf("finding notepad window\n"); 
    HWND hwndNotepad = FindWindow(NULL, L"Untitled - Notepad"); 
    if(NULL != hwndNotepad) 
    { 
     printf("Find edit control window\n"); 
     HWND hwndEdit = FindWindowEx(hwndNotepad, NULL, L"EDIT", NULL); 

     if(NULL != hwndEdit) 
     { 
      printf("- get text length\n"); 
      int textLen = (int)SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0); 
      printf("textlength: %d\n", textLen); 
      if(0 < textLen) 
      { 
       const int bufferSize = 1024; 
       TCHAR textBuffer[bufferSize]; 
       SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer); 
       printf("getting text:\n"); 
       printf("%s\n", textBuffer); 
      } 
      else 
      { 
       printf("its empty\n"); 
      } 
     } 
     else 
     { 
      printf("I cant find this control\n"); 
     } 
    } 
    else 
    { 
     printf("I cant find notepad window. \n"); 
    } 
    return 0; 
} 
+0

記事本是否被編譯爲Unicode還是不重要?我在某處讀到win32 api的ansi版本將unicode數據轉換爲它的ansi等價物。 – user2699298

相關問題