After the two interfaces defined in the previous article, the menu and toolbar basically matchCommandMode, you can load it in the system. But theoretically, it is one thing to follow the pattern, actually writingCodeIt is another thing.
We are sorry to find that Toolbarbutton This class does not even have a click event UI None of the related events, because they are not Control Inherited, but from Component Inherited. Click the toolbar button from the toolbar Toolbar Of Buttonclick Event, that is, if you want to hang up a code that clicks a single button on the toolbar Toolbar In terms of events, there is no such thing as a strange design. This Toolbar It can only be used in static places (in fact, even static places, this Buttonclick The event is also very bad, and the abnormal event of the static Toolbar will be seen later), for the dynamic Toolbar . NET Framework This Toolbar It is powerless.
No, let's change it. Click events on our own. Below isToolbarbuttonComplete code:
Toolbarbutton
Public Class Commandtoolbarbutton: toolbarbutton, icommandholder
{
Private String _ Id;
Private Icommand command;
Public Event Eventhandler onclick;
Public String ID
{
Get {Return_ Id ;}
Set {_ Id=Value ;}
}
Public Commandtoolbarbutton ( String Name ): Base (Name) {}
Icommandholder Member # Region Icommandholder Member
Public Icommand getcommand ()
{
ReturnCommand;
}
Public Void Setcommand (icommand cmd)
{
Command=CMD;
}
# Endregion
Internal Virtual Void Doonclick () {
If(Onclick! = Null)
Onclick (This, System. eventargs. Empty );
}
}
to enable toolbar Get To Know This Ton perform minor changes, toolbarbutton .
Protected Override VoidWndproc (RefMessage m ){
If(M. MSG = 0x2111 ){
IntIndex = ((Int) M. wparam) & 0 xFFFF;
Toolbarbutton button =This. Buttons [Index];
If(Button! =Null){
Commandtoolbarbutton commandbtn = buttonAsCommandtoolbarbutton;
If(Commandbtn! =Null){
Commandbtn. doonclick ();
}Else{
Toolbarbuttonclickeventargs args1 =NewToolbarbuttonclickeventargs (button );
This. Onbuttonclick (args1 );
}
}
}Else
Base. Wndproc (RefM );
Base. Resetmouseeventargs ();
}
the above Code contains a magic number , in Win32 API indicates that the cyclic message ( wm_entermenuloop , note that it is not written in the click message ), because the message constants are rarely used, I am just a little lazy and write it directly.