zoukankan      html  css  js  c++  java
  • MFC 滑动条的重绘

    MFC自带的滑动条的样子是这样的。

    比较难看,所以需要重绘下,重绘后的样子是这样的。

    代码如下:

    CustomSliderCtr.h

    #pragma once
    
    
    // CCustomSliderCtr
    
    class CCustomSliderCtr : public CSliderCtrl
    {
        DECLARE_DYNAMIC(CCustomSliderCtr)
    
    public:
        CCustomSliderCtr();
        virtual ~CCustomSliderCtr();
    
    protected:
        afx_msg void OnPaint();
      afx_msg void OnLButtonDown(UINT nFlags, CPoint point) ;
    void CustDraw(CDC *pDc); DECLARE_MESSAGE_MAP() };

    CustomSliderCtr.cpp

    // CustomSliderCtr.cpp : 实现文件
    //
    
    #include "stdafx.h"
    #include "CustomSliderCtr.h"
    
    
    // CCustomSliderCtr
    
    IMPLEMENT_DYNAMIC(CCustomSliderCtr, CSliderCtrl)
    
    CCustomSliderCtr::CCustomSliderCtr()
    {
    
    }
    
    CCustomSliderCtr::~CCustomSliderCtr()
    {
    }
    
    
    BEGIN_MESSAGE_MAP(CCustomSliderCtr, CSliderCtrl)
        ON_WM_PAINT()
    	ON_WM_LBUTTONDOWN()
    END_MESSAGE_MAP()
    
    
    
    // CCustomSliderCtr 消息处理程序
    
    void CCustomSliderCtr::OnPaint()
    {
        CSliderCtrl::OnPaint();
        CDC *pDc = GetDoc();
        CustDraw(pDc);
    }
    
    void CCustomSliderCtr::CustDraw(CDC *pDc)
    {
    
        COLORREF colorLeft(SLIDER_LEFT_COLOR);
    
        COLORREF colorChannel(SlIDER_BK_COLOR);
        CPen penChannel(PS_DASHDOTDOT, 2, colorChannel);
        CBrush brushChannel;
        brushChannel.CreateSolidBrush(colorChannel);
    
        COLORREF colorThumb(SLIDER_THUMB_COLOR);
        CPen penThumb(PS_DASHDOTDOT, 2, colorThumb);
        CBrush brushThumb;
        brushThumb.CreateSolidBrush(colorThumb);
    
    
        CRect clientRect;
        GetClientRect(clientRect);
        
        CRect channelRect;
        GetChannelRect(&channelRect);
         
        //clientRect.bottom /= 2; 
    
        pDc->SetBkMode(TRANSPARENT);
    
        pDc->FillSolidRect(clientRect, DIALOG_BKCOLOR);
        
        clientRect.left = channelRect.left;
        clientRect.right = channelRect.right;
        
        pDc->FillSolidRect(clientRect, colorChannel);
    
        pDc->Draw3dRect(clientRect, colorChannel, colorChannel);
    
        CRect thumbRect;
        GetThumbRect(thumbRect);
        
        thumbRect.bottom *= 2;
    
        CRect leftRect;
        leftRect.left = clientRect.left;
        leftRect.top = clientRect.top;
        leftRect.bottom = clientRect.bottom;
        leftRect.right = thumbRect.left;
    
        
        pDc->SelectObject(&brushThumb);
        pDc->SelectObject(&penThumb);
    
        pDc->FillSolidRect(leftRect, colorLeft);
    
        pDc->Draw3dRect(leftRect, colorLeft, colorLeft);
    
        pDc->Ellipse(thumbRect);
    }
    
    void CCustomSliderCtr::OnLButtonDown(UINT nFlags, CPoint point)   
    {  
        CSliderCtrl::OnLButtonDown(nFlags, point);  
        CRect   rectClient,rectChannel;   
        GetClientRect(rectClient);   
        GetChannelRect(rectChannel);   
        int nMax = 0;  
        int nMin = 0;  
        GetRange(nMin,nMax);  
        int nPos = (nMax - nMin)*(point.x - rectClient.left - rectChannel.left)/(rectChannel.right - rectChannel.left);   
        SetPos(nPos);  
    }  

     

     其中颜色定义

    #define SlIDER_BK_COLOR RGB(192, 192, 192)
    #define SLIDER_LEFT_COLOR RGB(148, 40, 255)
    #define SLIDER_THUMB_COLOR RGB(240, 240, 240)
    #define DIALOG_BKCOLOR RGB(201,227,253)
    高山流水,海纳百川!
  • 相关阅读:
    angular-ui-bootstrap的弹出框定义成一个服务的实践(二)
    分享百度文件上传组件webUploader的使用demo
    display的flex属性使用详解
    数组去重(初识ES6)
    在ng中的select的使用方法的讲解
    安装xamp之后,appach、mysql等问题的总结
    python中string.casefold和string.lower区别
    python3数据类型
    MySQL bin-log 日志清理方式
    python终端颜色设置
  • 原文地址:https://www.cnblogs.com/ahcc08/p/6253819.html
Copyright © 2011-2022 走看看