C # Right-click the taskbar and use the mouse hook to shield the menu

Source: Internet
Author: User

Recently, I encountered a problem that I needed to block the right-click menu on the taskbar. I had a tip, and I wanted to help people who needed it.

After checking the information for half a day on the Internet, you can modify the Registry. However, it is quite uncomfortable to restart the machine. You can also use the mouse hook to capture the right-click message for the purpose. Note that the menu will pop up only when the right-click button is released (because only the message pressed by the mouse is intercepted, the program has been failing, and I was skeptical about the possibility of using a hook. Oh, be careful ).

I will not elaborate on the use of C # To call windows APIs and hooks. Refer

Http://blog.csdn.net/LeoMaya/archive/2007/05/18/1615052.aspx

Http://blog.csdn.net/wztgq/archive/2006/08/02/1012132.aspx

There are also two API browsers, which are pretty good

Foxapi Chinese function browser v1.5(Detailed descriptions of API functions are provided)

API explorer v2.82(API functions can be converted to C # and VB. NET code)

After debugging in Visual Studio 2005, the project can be downloaded at http://download.csdn.net/source/536490.

 

/// Author: danseshi // Email: danseshi@yahoo.com.cn // Bolg: http://blog.csdn.net/danseshi////Date:2008.7.12using system; using system. windows. forms; using system. drawing; using system. runtime. interopservices; using system. diagnostics; namespace mousehook {public partial class form1: FORM {# region fields private int hmousehook = 0; // global hook constant private const int wh_mouse_ll = 14; // declare the constant of the message, press and release private cons T int wm_rbuttondown = 0x204; private const int wm_rbuttonup = 0x205; // Save the private rectangle taskbarrect In the rectangular area of the taskbar; private rectangle newtaskbarrect; // define the delegate Public Delegate int hookproc (INT ncode, int wparam, intptr lparam); Private hookproc mousehookprocedure; # endregion # region declares the API function and requires the introduction of space (system. runtime. interopservices) // find the qualified window [dllimport ("user32.dll", entrypoint = "findwindow")] public St Atic extern int findwindow (string lpclassname, string lpwindowname); // obtain the rectangular area of the window [dllimport ("user32.dll", entrypoint = "getwindowrect")] public static extern int getwindowrect (INT hwnd, ref rectangle lprect); // install hook [dllimport ("user32.dll")] public static extern int setwindowshookex (INT idhook, hookproc lpfn, intptr hinstance, int threadid); // uninstall the hook [dllimport ("user32.dll", entrypoint = "unho Okwindowshookex ")] public static extern bool unhookwindowshookex (INT hhook); // call the next hook [dllimport (" user32.dll ")] public static extern int callnexthookex (INT idhook, int ncode, int wparam, intptr lparam); // obtain the identifier of the current thread [dllimport ("kernel32.dll")] public static extern int getcurrentthreadid (); // obtain the module handle of an application or dynamic link library [dllimport ("kernel32.dll")] public static extern intptr getmodulehandle (string Nam E); // The mouse structure, saving the mouse information [structlayout (layoutkind. sequential)] public struct mousehookstruct {public point pt; Public int hwnd; Public int whittestcode; Public int dwextrainfo;} # endregion public form1 () {initializecomponent ();} /// <summary> /// installation hook /// </Summary> private void starthook () {If (hmousehook = 0) {hmousehook = setwindowshookex (wh_mouse_ll, mousehookprocedure, getmodulehandle (proce SS. getcurrentprocess (). mainmodule. modulename), 0); If (hmousehook = 0) {// If the hook fails to be set. this. stophook (); MessageBox. show ("set Windows Hook failed! ") ;}}/// <Summary> // uninstall hook // </Summary> private void stophook () {bool stop = true; If (hmousehook! = 0) {stop = unhookwindowshookex (hmousehook); hmousehook = 0; If (! Stop) {// MessageBox. Show ("unhook failed! ") ;}} Private int mousehookproc (INT ncode, int wparam, intptr lparam) {If (ncode> = 0) {// convert the data indicated by the lparam parameter in the memory to the mousehookstruct structure mousehookstruct mouse = (mousehookstruct) Marshal. ptrtostructure (lparam, typeof (mousehookstruct); // mouse // This sentence is used to view the mouse position this. TEXT = "mouseposition:" + mouse.pt. tostring (); If (wparam = wm_rbuttondown | wparam = wm_rbuttonup) {// intercept if (newtaskbarrect. contain S (mouse.pt) {// when the mouse is in the range of the taskbar // if 1 is returned, the message ends. This message is no longer transmitted until now. // If the return value is 0 or the callnexthookex function is called, the message returns this hook and continues to be transmitted, that is, to the real receiver of the message: return 1; }}return callnexthookex (hmousehook, ncode, wparam, lparam) ;}# region events private void formateload (Object sender, eventargs e) {mousehookprocedure = new hookproc (mousehookproc ); // taskbarhandle is the handle of the returned taskbar // shell_traywnd is the class name of the taskbar int taskbarhandle = findwindow ("shell_traywnd", null); // obtain the area of the taskbar // note that, when the function returns, taskbarrect contains the screen coordinates in the upper left corner and lower right corner of the window. // that is, taskbarrect. width and taskbarrect. height is the value (0, 0) relative to the upper left corner of the screen. // This is different from the rectangle structure of C #. getwindowrect (taskbarhandle, ref taskbarrect); this. richtextbox1.text = "taskbarrect. location: "+ taskbarrect. location. tostring () + "/N"; this. richtextbox1.text + = "taskbarrect. size: "+ taskbarrect. size. tostring () + "/n"; // construct a rectangle structure in C # newtaskbarrect = new rectangle (taskbarrect. x, taskbarrect. y, taskbarrect. width-taskbarrect. x, taskbarrect. height-taskbarrect. y); this. richtextbox1.text + = "newtaskbarrect. location: "+ newtaskbarrect. location. tostring () + "/N"; this. richtextbox1.text + = "newtaskbarrect. size: "+ newtaskbarrect. size. tostring ();} private void form=formclosing (Object sender, formclosingeventargs e) {This. stophook ();} private void button#click (Object sender, eventargs e) {This. starthook (); this. button1.enabled = false; this. button2.enabled = true;} private void button2_click (Object sender, eventargs e) {This. stophook (); this. button1.enabled = true; this. button2.enabled = false;} # endregion }}

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.