How do I read MessageBox text using WinAPI

爱⌒轻易说出口 提交于 2019-12-23 12:21:39

问题


How do I read a message of standard Win message box (Info)?

Using

SendMessage(this.HandleControl, WM_GETTEXT, builder.Capacity, builder);

I can only read the header of the message box or the text of the button, but not the message itself.

thanks.

Notes (from Q&A):

this.HandleControl is a handler to the message box window

Spy++ shows no child controls bar the button. That's what it made me thinking that Message Boxes have their own way of keeping text w/out using labels

It's a legacy app written with delphi, the button's class is TButton as per Spy++, but still there's no controls except of button inside the dialog window.

After checking a notepad window, both Image & Text are 'selectable', I guess my app doesn't use a std MessageBox. still, how do I go about extracting the text out of the thing? I can see that no labels in my delphi app can be selected by Spy++ Finder tool.


回答1:


The message text is in a label control on the modal MessageBox dialog window. You have to get the window handle to the MessageBox dialog (win32 API FindWindow) then retrieve the window handle to the control (win32 API GetDlgItem) and then retrieve the text from that window win32 API GetWindowText).

EDIT --

TCHAR text[51] = {0};
HWND msgBox = ::FindWindow(NULL, TEXT("MessageBoxCaption"));
HWND label = ::GetDlgItem(msgBox, 0xFFFF);
::GetWindowText(label, text, sizeof(text)-1);



回答2:


Try simulating a copy operation (Ctrl-C), then fetch the text from the clipboard: messageboxes allow copying the whole content that way (if they're properly done).

The OP commented that: that worked, thanks. I might end up with doing it that way. Ideally we wanted to keep our implementation focus independant, but choosing between a dedicated PC and OCR I'd probably go the first route.

Personally I've tested this in Delphi 6 and it comes out looking like this:

---------------------------
Confirm
---------------------------
You are about to close the program
WARNING: Are you sure?
---------------------------
Yes   No   
---------------------------

Note: This is based on an answer that was proposed by "Stefan" in the comments to the original Question



来源:https://stackoverflow.com/questions/5978879/how-do-i-read-messagebox-text-using-winapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!