How do I read MessageBox text using WinAPI - c#

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.

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

Related

How to get barcode data with barcode reader details

I am working on C#.
I am creating application which takes input from multiple barcode reader via USB.So,i want to know that from which reader i am getting value
Bar code scanners work by letting the system think their input comes from the keyboard.
So, one way of doing this:
bar code scanners can be programmed to send pre- and or suffixes with every scan.
You could prefix every scan with the e.g. 'F12' key for one scanner and with the 'F11' key for the other scanner. In the main form of your application, in the keypreview event, you scan for 'F12' and 'F11' keys, and if one comes in, you pop up a modal dialog with a textbox that has the focus. The rest of the scanned characters will end up in the textbox of that dialog. If on top op that, you also program an 'Enter' keysequence as a suffix to the scanned code, it would automatically close that dialog (if the dialog is well built). One of the advantages of this pattern is that, if a code is not readable, you can press F11 or F12, and the dialog will pop up and you can manually type the number.

Button with no hWnd

Good evening folks,
I'm building a simple application (A) that sends Strings to a textbox of another application (B). I was able to this step, but afterwards I'd like to automatically press a button placed just under the textbox. The problem is that I can't get the Handle of the Button; using "Window Detective"(similiar to Spy++), I see only the textboxes (called "TEdit", see the attachments) and no Buttons!. I'd like to add also that there's no only a Button but 3!! So, how could I press a specific Button? Is there another chance to get the Handle?
Program "target"
Window Detective screenshot
Based on the class name TEdit that's a VCL application probably coded in Delphi. The buttons are likely TSpeedButton and non-windowed. You won't be able to send them messages and they are not automatable.
Faced with this your best hope of success is to fake input. Fake the mouse click at the appropriate location on the form. It's not pretty but there's little option.

The header text in MessageBox.Show() is trimmed

I'm using MessageBox.Show() in a console application for a purpose. But, the header text displayed in the MessageBox is trimmed as it displays ending characters as "...".
Can I set the MessageBox window width to handle this behavior.
Please help me in this regard.
Thanks in advance.
No, in the default MessageBox its not possible to change the size of it. But if you want it, you can create your own message box. Just create a new form, put some buttons inside, in icon if you wanto and some text. And when you want it to show, just simply call ut, like you open the form
See
here
C# formatting a MessageBox
It can't be done. You have to create your own Form mimicking that behavior.
You can try to use the Win32 call SetWindowPos As suggested here.

Sending text into whatever window that is currently in focus

I'm trying to write a program with C# that sends text into other windows.
How do I write a command in C# that sends a text into the window that is currently under the users focus?
For example:
If the user clicks an open notepad window, or an open outlook letter, or an open excel sheet, and then clicks the button on my program, a text will be "pasted" directly into the last notepad window/outlook letter/excel cell that the user clicked on last.
I hope my question is clear enough. I'm not so experienced and am missing a lot of terminology.
Take your application out of focus by minimizing or hiding the main window, and then send your text with
SendKeys.SendWait("Hello World!");
Finally, restore your main window.
If the code is executed in the main form, you could do this
this.Visible = false;
SendKeys.SendWait("Hello World!");
this.Visible = true;
Olivier's response actually seems more accurate (and taught me something :)) than my original "does not seem achievable". If you need an example, then take a look at this:
http://www.codeproject.com/Articles/18366/Sending-Keystrokes-to-another-Application-in-C
However, on a more complex level, without an API to call into, there is not much more that you can do beyond this solution.

C# WinForms Wait on form to proceed

I'm tired and hungry, so I might of missed it, but from what I can see no existing post covers this...
I'm writing a plugin for an application. My plugin loads a form to get some data specifically, it uses the webcam to scan for a barcode. Once it's found a barcode, the form hides itself (incase it's needed again later). This is how I currently call the form that does the barcode work:
string readData = null;
if (eye == null)
{
System.Windows.Forms.Application.EnableVisualStyles();
eye = new CamView();
}
eye.Show();
if (eye.found)
{
readData = eye.readData;
}
return readData;
So, my problem is that eye.show() doesn't block. It makes the form appear and carries right on before there's a chance for the barcode to appear. I imagine I need to use some form of threading or locking, but my crude attempts to do so have just frozen the interface completely.
The "eye" form is basically just a viewfinder for the webcam, and relies on the camera_OnImageCapture event to make it do it's image checks for the barcode.
Is there an elegant way to make the application calling the plugin wait for the form to finish? Or do I just need to add an accept button to the "eye form?"
Cheers. And humble apologies if this is in anyway a repost.
.ShowDialog();
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
"You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed."
You are on the right track. You change the code to show CamView as a modal dialog but do no add an Accept button. Instead change camera_OnImageCapture to close the dialog.

Categories