How to lock the mouse in C #

Source: Internet
Author: User

I wrote a article about the udisc lock a few days ago. I was addicted to writing this kind of software myself.
alas ~ This software is easy to analyze for the first time, but it is still a little difficult for my "advanced" Program personnel. After reading programming windows (this book is not about core programming, it is Win32 programming, but it is a pity that a good book is always out of print early), I have clarified my thoughts.
the difficulty of the program is to lock the mouse. The online dashboard can either hook the mouse and keyboard to block all keyboard and mouse messages sent out of its own program, or rewrite the system's Gina. DLL to Block keyboard operations such as Ctrl, ALT, delete, and Esc. Use the clipcursor function to lock the mouse.
I am not a good guy. I cannot download a hook class from codeproject.-_-|. After my own analysis, I finally thought of a simple method (thanks to programming windows, I was brave enough to use this method ).
In fact, a timer control is used to check the handle of the current active form (getforegroundwindow function) every few seconds. If it is different from its own form handle, then use the sendmessage API function to send the minimal command (originally intended to close the command, but this will lose unsaved data, which is really bad ), use the setforegroundwindow API function to set itself as the main form and use the clipcursor function to lock the mouse.
Note: Why does programming windows give me courage, at first, I was worried that timer could not execute so many codes within one second. Later I saw that timer was a queue message in Windows message mechanism, therefore, after the previous timer completes the check, my if statement will never go wrong.

You may wonder why it is so complicated. I used clipcursor and the mouse was not always inside. Actually ...... Press win or Ctrl + ESC, or lock the computer (WIN + l) and log on to clipcursor again. Therefore, you must call several other functions for collaboration.
Here is a part of the source code:

// How to move the mouse within a certain range
Import DLL # Region Import DLL
[System. runtime. interopservices. dllimport ( " User32.dll " , Setlasterror =   True )]
Private   Static   Extern   Int Sendmessage (intptr hwnd, Uint MSG, Int Wparam, Int Lparam );
[System. runtime. interopservices. dllimport ( " User32.dll " , Charset = System. runtime. interopservices. charset. Auto, exactspelling =   True )]
Public   Static   Extern Intptr getforegroundwindow ();
[System. runtime. interopservices. dllimport ( " User32.dll " , Entrypoint =   " Setforegroundwindow " )]
Public   Static   Extern   Bool Setforegroundwindow (intptr hwnd );
# Endregion
Intptr global_active1_wintptr;
Private   Void Timer_delaylock_tick ( Object Sender, eventargs E)
{
Const   Uint Wm_syscommand =   Zero X 0112 ;
Const   Int SC _minimize =   0xf020 ;
Global_active1_wintptr = Getforegroundwindow ();
If ( This . Handle ! = Global_active1_wintptr && Global_active1_wintptr ! = Intptr. Zero)
{
Sendmessage (global_active1_wintptr, wm_syscommand, SC _minimize, 0 ); // Send minimal messages
Form_main.encrtptionclassmain.lockit ( This . Handle. toint32 ());
Setforegroundwindow ( This . Handle ); // Activate yourself as an active form
}
}

In addition, you only need to change SC _minimize to another message, such as SC _closed, to close the current active form. The specific SC _closed value can be viewed under Win32. It seems that it exists in a header file. If windows. H is included, you can right-click it to see the definition. I wanted to sort it out for you, but I have been busy recently. I am not very familiar with msdn when I have time to find it. Otherwise, I should just give it a hand. If the cool knows, you can tell me what to do ~

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.