Sending text from my program to an existing program textbox
Here is the situation. At my job, we use an existing program that allows us to scan barcodes into a record set. The issue is, to create this record set we have to scan each item separately. We click an add button which brings up a textbox and we scan the barcodes into it (or it can be ty开发者_开发技巧ped in)
To avoid having to do this, I've built a program in C# that creates a list of barcodes and I was looking for a way to just set up a simple copy and paste type setup that would copy a barcode, paste it into the existing programs textbox and maybe send a return char so it will be ready for the next barcode. It would simply send over each barcode one at a time till it had gone through the list.
I have control over my c# program, but the work program isn't built by me and I don't have access to the code or any apis it uses. Could someone suggest a solution that might work for this? I found something called SendMessage, but I'm not completely certain how it works since it says I need to search for the process first.
I solved a similar problem using http://inputsimulator.codeplex.com/. It simply allows you to automate key-presses so that you can:
- Read the content from your file with barcodes
- Press a key in you other software that opens up the input window
- Input the barcode
- Press return
I have done this for programs with no API, and it works great as a way of avoiding manual typing. Looks a bit ugly, though!
If you're using SendMessage, you can use spy++ to search for the class and name of the textbox, which will allow you to get a handle to the textbox. I'm sure I've seen an API call for settext, but I can't seem to find it at the moment. You can however use the messages WM_KEYDOWN, WM_KEYDOWN to simulate data entry.
http://msdn.microsoft.com/en-us/library/ff468861%28v=vs.85%29.aspx
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
Used as such:
SendMessage(textboxHandle, WM_KEYDOWN, KeyCode, null);
SendMessage(textboxHandle, WM_KEYUP, KeyCode, null);
Keycode list; http://msdn.microsoft.com/en-us/library/dd375731%28v=vs.85%29.aspx
You could make use of the UI Automation framework which is part of .NET 3+.
You would then use a tool such UISpy to extract the needed information relative to the text field in question which you can latch onto within your C# application and the UI Automation framework.
An example exists via a MSDN Magazine article.
精彩评论