Code
I. Custom button rfmsstepbutton:
//// // Rfmsstepbutton. h
# Pragma once
Class rfmsstepbutton: Public cbutton
{
Declare_dynamic (rfmsstepbutton)
Public:
Rfmsstepbutton ();
Virtual ~ Rfmsstepbutton ();
Protected:
Declare_message_map ()
Public:
Afx_msg void onlbuttondown (uint nflags, cpoint point );
Public:
Afx_msg void onlbuttonup (uint nflags, cpoint point );
Public:
Afx_msg void onmousemove (uint nflags, cpoint point );
Public:
Bool m_bstartmove;
Public:
Afx_msg void onlbuttondblclk (uint nflags, cpoint point );
Public:
Cpoint m_oldpoint;
};
/// // Rfmsstepbutton. cpp
# Include "stdafx. H"
# Include "buttontest. H"
# Include "rfmsstepbutton. H"
# Include "mydialog. H"
// Rfmsstepbutton
Implement_dynamic (rfmsstepbutton, cbutton)
Rfmsstepbutton: rfmsstepbutton ()
: M_bstartmove (false)
, M_oldpoint (0)
{
}
Rfmsstepbutton ::~ Rfmsstepbutton ()
{
}
Begin_message_map (rfmsstepbutton, cbutton)
On_wm_lbuttondown ()
On_wm_lbuttonup ()
On_wm_mousemove ()
On_wm_lbuttondblclk ()
End_message_map ()
// Rfmsstepbutton Message Processing Program
Void rfmsstepbutton: onlbuttondown (uint nflags, cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
M_bstartmove = true;
M_oldpoint = point;
Clienttoscreen (& m_oldpoint );
Cbutton: onlbuttondown (nflags, point );
}
Void rfmsstepbutton: onlbuttonup (uint nflags, cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
M_bstartmove = false;
Cbutton: onlbuttonup (nflags, point );;
}
Void rfmsstepbutton: onmousemove (uint nflags, cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
If (m_bstartmove)
{
/*
This method has the advantages of simple code, but the problem persists, that is, when you press the left button somewhere else and drag it to the button, the button will move again.
Releasecapture ();
Sendmessage (wm_nclbuttondown, (wparam) htcaption, 0 );
*/
Crect rect;
Getclientrect (& rect );
Clienttoscreen (& rect );
Getparent ()-> screentoclient (& rect );
Clienttoscreen (& Point );
Setwindowpos (null, rect. Left + point. X-m_oldpoint.x,
Rect. Top + point. Y-m_oldpoint.y, 0, 0, swp_nozorder | swp_nosize );
M_oldpoint = point;
Invalidate ();
}
Cbutton: onmousemove (nflags, point );
}
Void rfmsstepbutton: onlbuttondblclk (uint nflags, cpoint point)
{
// Double-click the event.
Mydialog dialog;
Dialog. domodal ();
Cbutton: onlbuttondblclk (nflags, point );
}
/*
Note: The response function must use wm_click, rather than bn_clicked.
*/
2. Call and group response:
// Buttontestdlg. cpp: implementation file
# Define id1 120
# Define Id2 140
Begin_message_map (cbuttontestdlg, cdialog)
On_command_range (id1, Id2, onmybutton) // all controls from id1 to Id2 respond to the onmybutton Function
End_message_map ()
Bool cbuttontestdlg: oninitdialog ()
{
Cdialog: oninitdialog ();
Rfmsstepbutton * button11 = new rfmsstepbutton;
Crect rect1 (200,0, 300,100 );
Button11-> Create (L "124", ws_child | ws_visible | ws_tabstop, rect1, this, 124 );
Rfmsstepbutton * button12 = new rfmsstepbutton;
Crect rect3 (200,200,300,300 );
Button12-> Create (L "125", ws_child | ws_visible | ws_tabstop, rect3, this, 125 );
Return true;
}
Void cbuttontestdlg: onmybutton (uint NID)
{
// MessageBox (L "this ");
}