This button slider is not written by me. Although it is not very useful, it is relatively creative. So I sorted it out and kept it as a souvenir.
/*************************************** ****************************************
File Name: csliderbutton. h header file
Author: Yang zhizhong
Creation Time: 16:52:07
File Description: button Slider
Copyright: Copyright (c) 2010-2012 cname
Modification history: Yang zhizhong 1.00 initial version
**************************************** ***************************************/
# Pragma once
// Csliderbutton
Class csliderbutton: Public cbutton
{
Declare_dynamic (csliderbutton)
Public:
Csliderbutton ();
Virtual ~ Csliderbutton ();
Protected:
Declare_message_map ()
Public:
Virtual void drawitem (lpdrawitemstruct );
Virtual bool pretranslatemessage (MSG * PMSG );
Protected:
Virtual void presubclasswindow ();
Public:
Void getrange (uint & Nmax, uint & Nmin );
Uint getslidepos ();
Void Init (uint nbkgndid, uint nslideid, cstring strtiptext );
Void Init (uint nbkgndid, uint nslideid, uint Nmax, uint Nmin, cstring strtiptext );
Void settooltiptext (cstring strtext );
Void setbuttoncursor (hcursor );
Void setslidepos (uint NPOs );
Void setrange (uint Nmin, uint Nmax );
Void setbitmap (uint nbkgnd, uint nslide );
Protected:
Afx_msg bool onsetcursor (cwnd * pwnd, uint nhittest, uint message );
Afx_msg void onlbuttondown (uint nflags, cpoint point );
PRIVATE:
Uint m_nwidth;
Void adjustposition ();
Void drawslide (CDC * PDC );
Void drawbackground (CDC * PDC );
Uint m_ncurrentpos;
Uint m_nmax;
Uint m_nmin;
Void setdefaultcursor ();
Hcursor m_hcursor;
Ctooltipctrl m_tooltip;
Cwnd * pwndparent;
Uint m_nslide;
Uint m_nbkgnd;
};
// Sliderbutton. cpp: implementation file
/*************************************** ****************************************
File Name: sliderbutton. cpp implementation file
Author: Yang zhizhong
Creation Time: 17:49:21
File Description: button Slider
Copyright: Copyright (c) 2010-2012 cname
Modification history: Yang zhizhong 1.00 initial version
**************************************** ***************************************/
# Include "stdafx. H"
# Include "sliderbutton. H"
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif
# Define id_tip 1
// Csliderbutton
Implement_dynamic (csliderbutton, cbutton)
Csliderbutton: csliderbutton ()
{
}
Csliderbutton ::~ Csliderbutton ()
{
}
Begin_message_map (csliderbutton, cbutton)
On_wm_setcursor ()
On_wm_lbuttondown ()
End_message_map ()
// Csliderbutton Message Processing Program
/*************************************** *****************************
Function Name: setbitmap (uint nbkgnd, uint nslide)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: setbitmap (uint nbkgnd, uint nslide)
{
M_nbkgnd = nbkgnd;
Nslide = m_nslide;
Invalidate ();
}
/*************************************** *****************************
Function Name: setdefacurcursor ()
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: setdefaultcursor ()
{
M_hcursor = loadcursor (null, makeintresource (32649 ));
}
/*************************************** *****************************
Function Name: setrange (uint Nmin, uint Nmax)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: setrange (uint Nmin, uint Nmax)
{
M_nmin = Nmin;
M_nmax = Nmax;
Adjustposition ();
Invalidate ();
}
/*************************************** *****************************
Function Name: setslidepos (uint NPOs)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: setslidepos (uint NPOs)
{
M_ncurrentpos = NPOs;
Invalidate ();
}
/*************************************** *****************************
Function Name: drawitem (lpdrawitemstruct)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: drawitem (lpdrawitemstruct)
{
// Todo: add your code to draw the specified item
CDC * PDC;
PDC = CDC: fromhandle (lpdrawitemstruct-> HDC );
Drawbackground (PDC );
Drawslide (PDC );
}
/*************************************** *****************************
Function Name: presubclasswindow ()
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: presubclasswindow ()
{
// Todo: add your specialized code here and/or call the base class
Setdefacurcursor ();
Crect rect;
Getclientrect (& rect );
M_tooltip.create (this );
M_tooltip.setdelaytime (100 );
M_tooltip.setmaxtipwidth (200 );
M_tooltip.addtool (this, "", rect, id_tip );
Cbutton: presubclasswindow ();
}
/*************************************** *****************************
Function Name: pretranslatemessage (MSG * PMSG)
Note: QQ 940446982
**************************************** *****************************/
Bool csliderbutton: pretranslatemessage (MSG * PMSG)
{
// Todo: add your specialized code here and/or call the base class
M_tooltip.relayevent (PMSG );
Return cbutton: pretranslatemessage (PMSG );
}
/*************************************** *****************************
Function Name: onsetcursor (cwnd * pwnd, uint nhittest, uint message)
Note: QQ 940446982
**************************************** *****************************/
Bool csliderbutton: onsetcursor (cwnd * pwnd, uint nhittest, uint message)
{
// Todo: add your message handler code here and/or call default
If (m_hcursor! = NULL ){
: Setcursor (m_hcursor );
Return true;
}
Return false;
}
/*************************************** *****************************
Function Name: setbuttoncursor (hcursor)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: setbuttoncursor (hcursor)
{
M_hcursor = hcursor;
}
/*************************************** *****************************
Function Name: drawbackground (CDC * PDC)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: drawbackground (CDC * PDC)
{
CDC memdc;
Cbitmap bitmap;
Bitmap m_bitmap;
Bitmap. loadbitmap (m_nbkgnd );
Bitmap. getbitmap (& m_bitmap );
Memdc. createcompatibledc (PDC );
Memdc. SelectObject (& Bitmap );
PDC-> bitblt (0, 0, m_bitmap.bmwidth, m_bitmap.bmheight, & memdc,
0, 0, srccopy );
Bitmap. deleteobject ();
Memdc. deletedc ();
}
/*************************************** *****************************
Function Name: drawslide (CDC * PDC)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: drawslide (CDC * PDC)
{
Crect rect;
Getclientrect (& rect );
Int nwidth = rect. Width ();
Cbitmap bitmap;
Bitmap m_bitmap;
CDC memdc;
Bitmap. loadbitmap (m_nslide );
Bitmap. getbitmap (& m_bitmap );
Nwidth-= m_bitmap.bmwidth;
Int x = (INT) (float) nwidth/(float) (m_nMax-m_nMin) * (m_nCurrentPos-m_nMin ));
If (x <0)
X = 0;
Memdc. createcompatibledc (PDC );
Memdc. SelectObject (& Bitmap );
PDC-> bitblt (x, 0, m_bitmap.bmwidth, m_bitmap.bmheight,
& Memdc, 0, 0, srccopy );
Bitmap. deleteobject ();
Memdc. deletedc ();
M_nwidth = nwidth;
}
/*************************************** *****************************
Function Name: settooltiptext (cstring strtext)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: settooltiptext (cstring strtext)
{
M_tooltip.updatetiptext (strtext, this, id_tip );
}
/*************************************** *****************************
Function Name: Init (uint nbkgndid, uint nslideid, uint Nmax, uint Nmin, cstring strtiptext)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: Init (uint nbkgndid, uint nslideid, uint Nmax, uint Nmin, cstring strtiptext)
{
M_nbkgnd = nbkgndid;
M_nslide = nslideid;
M_nmax = Nmax;
M_nmin = Nmin;
M_tooltip.updatetiptext (strtiptext, this, id_tip );
M_ncurrentpos = m_nmin;
Pwndparent = getparent ();
Adjustposition ();
}
/*************************************** *****************************
Function Name: Init (uint nbkgndid, uint nslideid, cstring strtiptext)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: Init (uint nbkgndid, uint nslideid, cstring strtiptext)
{
M_nbkgnd = nbkgndid;
M_nslide = nslideid;
M_nmax = 100;
M_nmin = 1;
M_tooltip.updatetiptext (strtiptext, this, id_tip );
M_ncurrentpos = m_nmin;
Pwndparent = getparent ();
Adjustposition ();
}
/*************************************** *****************************
Function Name: getslidepos ()
Note: QQ 940446982
**************************************** *****************************/
Uint csliderbutton: getslidepos ()
{
Return m_ncurrentpos;
}
/*************************************** *****************************
Function Name: adjustposition ()
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: adjustposition ()
{
Crect rect;
Cbitmap bitmap;
Bitmap m_bitmap;
Bitmap. loadbitmap (m_nbkgnd );
Bitmap. getbitmap (& m_bitmap );
Getwindowrect (& rect );
Pwndparent-> screentoclient (& rect );
Rect. Right = rect. Left + m_bitmap.bmwidth;
Rect. Bottom = rect. Top + m_bitmap.bmheight;
Movewindow (& rect, true );
Bitmap. deleteobject ();
}
/*************************************** *****************************
Function Name: onlbuttondown (uint nflags, cpoint point)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: onlbuttondown (uint nflags, cpoint point)
{
// Todo: add your message handler code here and/or call default
Crect rect;
Getclientrect (& rect );
If (rect. ptinrect (point )){
M_ncurrentpos = (uint) (float) point. X/(float) m_nwidth * (m_nMax-m_nMin) + m_nmin;
}
Cbutton: onlbuttondown (nflags, point );
}
/*************************************** *****************************
Function Name: getrange (uint & Nmax, uint & Nmin)
Note: QQ 940446982
**************************************** *****************************/
Void csliderbutton: getrange (uint & Nmax, uint & Nmin)
{
Nmax = m_nmax;
Nmin = m_nmin;
}