Home > Blockchain >  Is there a way in mfc slider control that as i move the thumb, it fills the background color of the
Is there a way in mfc slider control that as i move the thumb, it fills the background color of the

Time:01-29

I am trying to make a customised slider in mfc.Is there a way so that as I move the slider, the background color changes according to the position of the thumb of the slider over the default background color, just like the default .net slider in .net.

void rogreen::OnPaint()
{
    CPaintDC dc(this); // device context for painting
                       // TODO: Add your message handler code here
                       // Do not call CStatic::OnPaint() for painting messages

    CRect rectWindow;
    GetClientRect(&rectWindow);


    CRect rect;
    GetClientRect(&rect);

    int r1 = { 0 }, g1 = { 255 }, b1 = { 0 }; //Any start color
    int r2 = { 0 }, g2 = { 0 }, b2 = { 0 }; //Any stop color

    for (int i = 0;i < rect.Height();i  )
    {
        int r, g, b;
        r = r1   (i * (r2 - r1) / rect.Height());
        g = g1   (i * (g2 - g1) / rect.Height());
        b = b1   (i * (b2 - b1) / rect.Height());
        dc.FillSolidRect(0, i, rect.Width(), 1, RGB(r, g, b));
    }
}

CodePudding user response:

Using the example from https://docs.microsoft.com/en-us/windows/win32/gdi/drawing-a-shaded-rectangle, you can handle a WM_CTLCOLORSTATIC message like that:

case WM_CTLCOLORSTATIC:
{
    if (::GetDlgItem(hDlg, IDC_SLIDER) == (HWND)lParam) {
        HDC hdc = (HDC)wParam;
        RECT r = {};
        ::GetClientRect((HWND)lParam, &r);
        
        // Create an array of TRIVERTEX structures that describe 
        // positional and color values for each vertex. For a rectangle, 
        // only two vertices need to be defined: upper-left and lower-right. 
        TRIVERTEX vertex[2];
        vertex[0].x = 0;
        vertex[0].y = 0;
        vertex[0].Red = 0x0000;
        vertex[0].Green = 0x8000;
        vertex[0].Blue = 0x8000;
        vertex[0].Alpha = 0x0000;

        vertex[1].x = r.right; // 400;
        vertex[1].y = r.bottom; // 40;
        vertex[1].Red = 0x0000;
        vertex[1].Green = 0xd000;
        vertex[1].Blue = 0xd000;
        vertex[1].Alpha = 0x0000;

        // Create a GRADIENT_RECT structure that 
        // references the TRIVERTEX vertices. 
        GRADIENT_RECT gRect;
        gRect.UpperLeft = 0;
        gRect.LowerRight = 1;
        // Draw a shaded rectangle. 
        GradientFill(hdc, vertex, 2, &gRect, 1, GRADIENT_FILL_RECT_V);
        return (INT_PTR)::GetStockObject(NULL_BRUSH);
    }
}

You can use your slider's position to calculate the color values you want.

CodePudding user response:

I can't provide an answer as I have not tried what you want myself, but you should look in the "owner draw" control option. I have used that to create different color buttons. I have not tried to vary the color as a slider moves.

(I assume the code you have posted above does not do what you want?)

  •  Tags:  
  • Related