Recently, I saw a friend studying how to implement double-click button in my blog.
Web: http://www.cnblogs.com/adaiye/archive/2008/10/16/button_doubleclick2.html
He implements it with code. In fact, his spirit is worth learning, but adding double-click events on the button itself is rather embarrassing. I have read this article and I feel very grateful, it seems that many skills related to window development (the so-called skill is the smallest unit of a technology, and the technology refers to how to use the skills) maybe many of my friends in the garden are not very familiar with it (I don't mean it). I have written some code here for a few minutes, for your reference, Windows developers can ignore this article;
In fact, this code segment mainly uses a plug-in method to intercept control events (Windows messages) in the window. The premise is that the control itself is not implemented, for example, the wm_lbuttondblclk message button in the Code does not provide the corresponding event encapsulation (double-click the button does not make sense, please do not imitate), mainly to solve some less code operations, it is best to encapsulate a large amount of code into controls for reuse;
I have not developed Windows desktops for many years and may write some problems in many places. Please forgive me. The code is very simple and I will not comment on it too much;
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace windowsapplication1
{
Public partial class form1: Form
{
// The button form1.designer. CS will not be pasted on the window.
// This class is described after the code
Subclassmanager SCM;
Public form1 ()
{
Initializecomponent ();
// 1) subclass controls and map processing functions
SCM = new subclassmanager (this. button1.handle );
SCM. subclasswindowproc ++ = button?wndproc; // event ing
}
Protected override void onhandledestroyed (eventargs E)
{
// 3) Release
SCM. Dispose ();
Base. onhandledestroyed (E );
}
Protected bool button1_wndproc (ref message m)
{
// 2) any message of button1 can be received here
// Double-click the button.. Net does not encapsulate this event. Here, we just want to demonstrate the subclass.
// Double-clicking a button does not make sense. Do not imitate it.
Const int wm_lbuttondblclk = 0x0203;
If (M. MSG = wm_lbuttondblclk)
{
Console. writeline ("wm_lbuttondblclk"); // enter your processing code
}
Return true;
}
}
/// <Summary>
/// This class is used to subscribe controls or windows and intercept messages that are not encapsulated by. net. (do not use this class if the window is overwritten by wndproc)
/// </Summary>
Public class subclassmanager: system. Windows. Forms. nativewindow, idisposable
{
Public Delegate bool subclasswndproc (ref message m); // delegate the Message Processing Function
Private intptr handle;
Private bool disposed = false;
Public event subclasswndproc subclasswindowproc; // Message Processing Event
Public subclassmanager (intptr hwnd)
{
Handle = hwnd;
This. assignhandle (handle );
}
Protected override void wndproc (ref message m)
{
If (subclasswindowproc! = NULL)
{
If (subclasswindowproc (ref m ))
{// Call the base class if true is returned
Base. wndproc (ref m );
}
}
}
# Region idisposable Member
~ Subclassmanager ()
{
Console. writeline ("Release subclassmanager ");
Dispose (false );
}
Private void dispose (bool disposing)
{
If (! This. disposed)
{
If (disposing)
{
This. releasehandle ();
}
}
Disposed = true;
}
Public void dispose ()
{
Dispose (true );
}
# Endregion
}
}