簡體   English   中英

mfc slider 控件中是否有一種方法,當我移動拇指時,它會在默認背景顏色上填充 slider 的背景顏色

[英]Is there a way in mfc slider control that as i move the thumb, it fills the background color of the slider over the default background color

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));
    }
}

使用https://docs.microsoft.com/en-us/windows/win32/gdi/drawing-a-shaded-rectangle中的示例,您可以像這樣處理WM_CTLCOLORSTATIC消息:

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);
    }
}

您可以使用滑塊的 position 來計算所需的顏色值。

我無法提供答案,因為我自己沒有嘗試過您想要的,但您應該查看“所有者繪制”控制選項。 我用它來創建不同顏色的按鈕。 我沒有嘗試在 slider 移動時改變顏色。

(我假設您在上面發布的代碼不符合您的要求?)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM