对照的背景颜色(winapi)

时间:2014-03-21 19:30:26

标签: winapi controls background-color flicker

以下是我所说的:program window

我知道,这个话题已多次发布,但我找不到任何好的解决方案。 我的问题是 1: - 闪烁控件显示时间和直方图(静态,不是双缓冲) 2: - 我无法将控件的背景(标签,轨迹伙伴,直方图bkgnd)从白色更改为LTGRAY_BRUSH。
这里有一些代码:

case WM_PAINT:
    PaintProcedure( hdcClock );
    SetBkColor(hdcWindow, RGB(255,220,180));
    TextOut(hdcWindow,260,10,TEXT(" -- CAMERA WINDOW -- "),21); 
    break;

... ...

void PaintProcedure( HDC hdc )
{
    img = cam->getFrame();
    cam->ShowImage(img, hdcCam, 1, 1, img->width, img->height, 0, 0);

    InvalidateRect( hClock, &rClock, 1 );               
    RedrawWindow(hClock,&rClock,0,RDW_UPDATENOW);

    char sTime[256];
    SYSTEMTIME time;
    HFONT hFont;
    SIZE size;
    ...
        ...
    BeginPath (hdc) ;
    SetBkMode( hdc, TRANSPARENT/*OPAQUE*/ );
    TextOut( hdc,1,1,sTime, strlen( sTime ) );
    EndPath (hdc) ;

    SelectObject (hdc, CreateHatchBrush (HS_DIAGCROSS, RGB (0, 0, 255))) ;
    SetBkColor (hdc, RGB (255, 0, 0)) ;
    SetBkMode (hdc, OPAQUE) ;

    StrokeAndFillPath (hdc) ;
    DeleteObject (SelectObject (hdc, GetStockObject (LTGRAY_BRUSH)));
    SelectObject (hdc, GetStockObject (SYSTEM_FONT)) ;
    DeleteObject (hFont) ;

    cam->ShowImage(cam->drawIntensityHistogram(),hdcHistogram,1,1,
        rHistogram.right-rHistogram.left,rHistogram.bottom-rHistogram.top,0,0);


    InvalidateRect( hwnd, &r, 1 );
    RedrawWindow(hwnd,&r,0,RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW);
}

添加此代码:

case WM_CTLCOLORSTATIC:
case WM_CTLCOLORDLG:
case WM_CTLCOLORBTN:
    return (LRESULT)GetStockObject(LTGRAY_BRUSH);

导致仅绘制没有框架的轨迹栏。 我尝试使用计时器来使控件无效并重绘,但它并没有帮助。 现在我不知道如何解决它。有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:0)

删除WM_PAINT的处理程序并在计时器触发时调用PaintProcedure():示例代码

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{   HWND hwnd;
    MSG msg;
    WNDCLASS w;

    memset(&w,0,sizeof(WNDCLASS));
    w.style = CS_HREDRAW | CS_VREDRAW;
    w.lpfnWndProc = WndProc;
    w.hInstance = hInstance;
    w.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); // WHITE_BRUSH
    w.lpszClassName = L"My Class";
    w.hCursor = LoadCursor(NULL, IDC_ARROW);
    RegisterClass(&w);
    hwnd = CreateWindow(L"My Class", L"My title", WS_OVERLAPPEDWINDOW, 300, 200, 800, 600, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd,nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0))
    {   TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

UINT_PTR uTimerId;
LOGBRUSH lbrBk;
LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{   switch (Message)
    {   case WM_CREATE:
        {   LRESULT lRes = DefWindowProc(hwnd, Message, wparam, lparam); // I don't have any controls in the main window
            HBRUSH hbr = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
            GetObject(hbr, sizeof(lbrBk), &lbrBk); // for use in timer logic
            uTimerId = SetTimer(hwnd, 123, 1000, NULL);
            return lRes;
        }

        case WM_TIMER:
        {   TCHAR szTime[128];
            HDC hdc = GetDC(hwnd);
            _tstrtime_s(szTime, _countof(szTime));
            SetBkColor(hdc, lbrBk.lbColor);
            TextOut(hdc, 200, 200, szTime, _tcslen(szTime));
            ReleaseDC(hwnd, hdc);
            break;
        }

        case WM_DESTROY: 
            KillTimer(hwnd, uTimerId);
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, Message, wparam, lparam);
    }
    return 0;
}

答案 1 :(得分:0)

哦,我多年没有使用winapi,所以我可以忘记gdi是如何工作的,但我不明白我怎么能得不到它,你在谈论:删除WM_PAINT - 整个处理程序,不仅我做的电话。我早些时候解决了直方图闪烁的问题。这是一个非常快的程序,我在相机类中编写和检查python中的所有算法,所以我确信它应该工作。它有效。我不得不关闭代码优化。
您的提示解决了定时器闪烁问题和整体接口效率。但现在还没有解决 白色背景 问题。我应该使用子类来为我的控件提供我选择的颜色吗?我试过WM_CREATE

hStatic = CreateWindowEx( 0, "STATIC", NULL, WS_CHILD| WS_VISIBLE |SS_LEFT, 10, 130, 200, 20, hwnd, NULL, hInst, NULL );
        SetBkColor(GetDC(hStatic), lbrBk.lbColor);
        SetWindowText( hStatic, buf );

但没有效果。
这是我现在的界面:https://www.dropbox.com/s/7123ef7wi1sars8/follower2.png
谢谢。 :d

答案 2 :(得分:0)

使用静态控件的源代码:

LONG WINAPI WndProc(HWND, UINT, WPARAM,LPARAM);

HWND hWndStatic;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{   HWND hwnd;
    MSG msg;
    WNDCLASS w;

    memset(&w,0,sizeof(WNDCLASS));
    w.style = CS_HREDRAW | CS_VREDRAW;
    w.lpfnWndProc = WndProc;
    w.hInstance = hInstance;
    w.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
    w.lpszClassName = L"My Class";
    w.hCursor = LoadCursor(NULL, IDC_ARROW); 
    RegisterClass(&w);

    hwnd = CreateWindow(L"My Class", L"My title", WS_OVERLAPPEDWINDOW, 300, 200, 800, 600, NULL, NULL, hInstance, NULL);
    hWndStatic = CreateWindowEx(0, L"Static", NULL, WS_CHILD| WS_VISIBLE |SS_LEFT, 10, 130, 200, 20, hwnd, NULL, hInstance, NULL);

    ShowWindow(hwnd,nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0))
    {   TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

UINT_PTR uTimerId;
LOGBRUSH lbrBk;

LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
    switch (Message)
    {   case WM_CREATE:
        {   LRESULT lRes = DefWindowProc(hwnd, Message, wparam, lparam);
            HBRUSH hbr = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
            GetObject(hbr, sizeof(lbrBk), &lbrBk);
            uTimerId = SetTimer(hwnd, 123, 1000, NULL);
            return lRes;
        }

        case WM_CTLCOLORSTATIC:
            if ((HWND)lparam == hWndStatic)
            {   SetBkColor((HDC)wparam, lbrBk.lbColor);
                return (BOOL) GetStockObject(LTGRAY_BRUSH);
            }
            return FALSE;

        case WM_TIMER:
        {   TCHAR szTime[128];
            HDC hdc = GetDC(hwnd);
            _tstrtime_s(szTime, _countof(szTime));
            SetWindowText(hWndStatic, szTime);
            ReleaseDC(hwnd, hdc);
            break;
        }

        case WM_DESTROY: 
            KillTimer(hwnd, uTimerId);
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, Message, wparam, lparam);
    }
    return 0;
}