Continued: analyze how to use C #. Net for screen software and register global shortcuts (on)
The third method is to simulate the printscreen button, access the clipboard to obtain the screen, and then perform the screenshot operation.
1. Simulating the printscreen button saves us a lot of code, but it also gave me a lot of confusion at the beginning. If you want to call the API through the button_click event to simulate the press of the printscreen key, then use clipboard. getimage () gets the image in the clipboard. There is only one result: failed! Maybe you think that when we call clipboard. getimage (), the simulated buttons are not completed, and then we add thread. Sleep (0) among them. Unfortunately, the results are the same. I suspect that the disorder between the mouse clicking button and the simulated printscreen button is caused, so I will not talk about it here. The solution I think is to use backgroundworker to execute the button processing, in this way, the disorder between the form thread and the simulated button is separated.
C # simulate keyboard buttons:
The keybd_event in user32.dll is used to simulate the keyboard.
The prototype is as follows:
[Dllimport ("user32.dll")] Static extern void keybd_event (byte bvk, // virtual key value byte bscan, // hardware scan code uint dwflags, // uintptr dwextrainfo // The supplementary information associated with the keyboard action );
Bvk indicates the virtual key value. In fact, it is a macro of the byte type value. Its value range is 1-254. For a virtual key-value table, use the keyword "virtual-key codes" on msdn to find relevant information. Bscan indicates that when a key on the keyboard is pressed or opened, the scan code generated by the hardware of the keyboard system can be converted by the mapvirtualkey () function between the virtual key value and the scan code, generally, it can be set to 0. Dwflags indicates a variety of keyboard actions. The application can use the combination of the following predefined constants to set the flag. keyeventf_exetendedkey = 1: if this value is specified, the first value of the scan code is the prefix byte of oxeo (224. Deyeventf_keyup = 2: if this value is specified, the key is released. If this value is not specified, the key is handed over. Dwextralnfo: defines the 32-bit value associated with the strike key. C # example of simulating the printscreen button,
public static void PrintScreen() { IceApi.keybd_event( (byte)0x2c, 0, (uint)0, IntPtr.Zero );//down IceApi.keybd_event( (byte)0x2c, 0, (uint)2, IntPtr.Zero );//up }
Paste the button for processing, see ~ :
We handed over all the tasks after clicking to backgroundworker, saving a lot of trouble.
The next step is to enter the real stage. First, create a new form snapform and set the following attributes:
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;this.Opacity = 0.99;this.ShowIcon = false;this.ShowInTaskbar = false;this.TopMost = true;this.WindowState =System.Windows.Forms.FormWindowState.Maximized;
Modify the constructor to (SEE ):
When the form is displayed, it is already on the full screen.
Then, we can place a picturebox on the form and draw a line and a rectangle in picturebox_paint.
Example:
Picbox_paint
private void picBox_Paint( object sender, PaintEventArgs e ) {
Graphics g = e.Graphics;
if (isDrawing) {
//g.DrawRectangle( penRect, captureRect);
DrawScableRect( captureRect, g );
}
else if (!isDrawned) {
g.DrawLine( penLine, 0, currentPoint.Y, fullScreen.Width, currentPoint.Y );
g.DrawLine( penLine, currentPoint.X, 0, currentPoint.X, fullScreen.Height );
}
}
First, we need to record the starting point of the rectangle when the mouse is pressed,
if (!isDrawing) { startPoint = e.Location; captureRect.Location = startPoint; isDrawing = true; return; }
Then, refresh the rectangle size in the mousemove event, call this. Refresh (), force the screen weight, and call the paint method.
The last step is to check whether the painting is completed in the mouseup event. This will stop calling this. Refresh.
As for drawing graphics, graphics. drawrectangle () and graphics. drawline () are required. You can go to msdn to query their usage methods.
In the mousemove event, we can detect the current position of the mouse, set the Mouse shape, and then implement the drag, expansion, and reduction of the rectangle.
I will not go into detail here.
For saving, we can add a mousedoubleclick event. Below, I directly intercept the rectangular area from the original image,
Use orgbmp. Clone (capturerect, screensnap. pixelformat) to copy the region we need. This ensures that
Image Quality also avoids the rectangular border issue.
Mousedoubleclick
private void SnapForm_MouseDoubleClick( object sender, MouseEventArgs e ) {
if (e.Button==MouseButtons.Left) {
Bitmap orgbmp = new Bitmap( screenSnap );
try {
Bitmap ab = orgbmp.Clone( captureRect, screenSnap.PixelFormat );
if (saveDlg.ShowDialog() == DialogResult.OK) {
ab.Save( saveDlg.FileName, imgFormat[Path.GetExtension( saveDlg.FileName )] );
MessageBox.Show( "Completed!" );
}
}
catch { }
finally { orgbmp.Dispose(); }
}
}
In this way, C # is basically finished. to beautify the system, we can place a translucent layer on the form to make QQ-like effects.
Finally, let's talk about how to customize mouse styles and define global shortcuts in C.
1. Custom mouse style: If you only use the managed code in C #, the result of the custom mouse will become a monochrome.
We need several APIs in user32.dll to customize the color mouse style.
C# Signature:[DllImport("user32.dll")]static extern IntPtr LoadCursorFromFile(string lpFileName);[DllImport( "user32.dll" )]public static extern uint DestroyCursor( IntPtr cursorHandle );
Both of them are relatively simple. We can load the mouse handle during form_load. Remember to release the handle resource at form_closing ~
This. cursor = new cursor (intptr handle) is used to instantiate the mouse.
2. For Global shortcuts or hotkeys, you need to call the other two APIs in USER32.
[DllImport( "user32.dll", SetLastError = true )] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options System.Windows.Forms.Keys vk // virtual-key code ); [DllImport( "user32.dll", SetLastError = true )] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier ); [Flags] public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 }
For example, register the F3 key as the shortcut key: registerhotkey (this. Handle, 7890, iceapi. keymodifiers. None, keys. F3 );
These two APIs should be put in the form_load and form_formclosed events respectively.
Finally, we need to intercept system messages to perform corresponding operations for the shortcut keys we define. In this case, we need to rewrite wndproc.
Wndproc
protected override void WndProc( ref Message m ) {
switch (m.Msg) {
//hotkey pressed
case 0x0312:
if (m.WParam.ToString() == "7890") {
GlobalKeyProc( this.WindowState == FormWindowState.Minimized );
}
break;
}
base.WndProc( ref m );
}
Process the work we need in globalkeyproc.
Finally. Thank you for watching.