How do I read MessageBox text using WinAPI
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 select开发者_高级运维ed by Spy++ Finder tool.
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);
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
精彩评论