C # simulate a keyboard operation and input a value to another form

Source: Internet
Author: User

My company uses U8. Because BOM is not used for various reasons, the purchasing department has to manually enter the purchase orders. These purchase orders are in the Excel electronic format. How can I import these Excel files to U8 automatically? Considering that the development interface is not possible, we can find another way. Can we simulate manual input?

Fortunately, this approach is feasible.

1. First write the value to another program

A. Make sure that the current program form cannot get the focus


[Css]
Private const int WS_EX_TOOLWINDOW = 0x00000080;
Private const int WS_EX_NOACTIVATE = 0x08000000;
 
Protected override CreateParams
{
Get
{
CreateParams cp = base. CreateParams;
Cp. ExStyle | = (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW );
Cp. Parent = IntPtr. Zero; // Keep this line only if you used UserControl
Return cp;
}
}
B. Obtain the form handle of the active cell.

[Csharp]
[DllImport ("user32.dll")]
[Return: financialas (UnmanagedType. Bool)]
Public static extern bool GetGUIThreadInfo (uint idThread, ref GUITHREADINFO lpgui );
[DllImport ("user32.dll")]
Public static extern IntPtr GetForegroundWindow ();
[DllImport ("user32.dll", SetLastError = true)]
Static extern uint GetWindowThreadProcessId (IntPtr hWnd, out uint lpdwProcessId );
 
IntPtr hWnd = GetForegroundWindow ();
Uint processId;
Uint threadid = GetWindowThreadProcessId (hWnd, out processId );
GUITHREADINFO lpgui = new GUITHREADINFO ();
Lpgui. cbSize = Marshal. SizeOf (lpgui );

C. Analog keyboard input value

[Csharp] view plaincopy
[DllImport ("user32.dll")]
Static extern void keybd_event (byte bVk, byte bScan, uint dwFlags, int dwExtraInfo );
 
Keybd_event (keycode ["LEFT"], 0, 0, 0 );

2. Read the Excel file cyclically and write the value.
In this example, I write the key value to private Dictionary <string, byte> keycode; this facilitates conversion.


[Csharp]
Keycode = new Dictionary <string, byte> ();
Keycode. Add ("A", 65 );
Keycode. Add ("B", 66 );
Keycode. Add ("C", 67 );
Keycode. Add ("D", 68 );
Keycode. Add ("E", 69 );
Keycode. Add ("F", 70 );
Keycode. Add ("G", 71 );
Keycode. Add ("H", 72 );
Keycode. Add ("I", 73 );
Keycode. Add ("J", 74 );
Keycode. Add ("K", 75 );
Keycode. Add ("L", 76 );
Keycode. Add ("M", 77 );
Keycode. Add ("N", 78 );
Keycode. Add ("O", 79 );
Keycode. Add ("P", 80 );
Keycode. Add ("Q", 81 );
Keycode. Add ("R", 82 );
Keycode. Add ("S", 83 );
Keycode. Add ("T", 84 );
Keycode. Add ("U", 85 );
Keycode. Add ("V", 86 );
Keycode. Add ("W", 87 );
Keycode. Add ("X", 88 );
Keycode. Add ("Y", 89 );
Keycode. Add ("Z", 90 );
Keycode. Add ("0", 48 );
Keycode. Add ("1", 49 );
Keycode. Add ("2", 50 );
Keycode. Add ("3", 51 );
Keycode. Add ("4", 52 );
Keycode. Add ("5", 53 );
Keycode. Add ("6", 54 );
Keycode. Add ("7", 55 );
Keycode. Add ("8", 56 );
Keycode. Add ("9", 57 );
Keycode. Add (".", 0x6E );
Keycode. Add ("LEFT", 0x25 );
Keycode. Add ("UP", 0x26 );
Keycode. Add ("RIGHT", 0x27 );
Keycode. Add ("DOWN", 0x28 );
Keycode. Add ("-", 0x6D );


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.