自己定制了一个文本标签,根据文本的个数填充整个标签框,想让标签框的每个文字按照红绿蓝闪起来,在WM_TIMER 绘制中未每个标签做了个定时器,想调用TimerProc函数让那些文字闪起来,但是效果没有出来,不知问题在哪,请大家指点,还有如果可以闪的话多个标签框公用那个 K 感觉有得问题,不知如何改好?
#include <windows.h>COLORREF color[3] = { RGB(255,0,0) ,RGB(0,255,0),RGB(0,0,255)};
HINSTANCE hInst;
int k=1;VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent,  DWORD dwTime);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR  lpCmdLine,int nCmdShow)
{
MSG msg;
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName ="mywin";
wcex.hIconSm = 0; RegisterClassEx(&wcex);
CreateWindow("mywin", "", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
      0, 0, 1000, 600, NULL, NULL, hInstance, NULL); while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return 0;
}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
/*int wmId, wmEvent;*/
PAINTSTRUCT ps;
HDC hdc;
char data[30];
int r;
int lw,lh;//控件大少
int fw,fh;//字体大小
int i;
RECT rect;
HFONT hfont;
HINSTANCE hInst =GetModuleHandle(0); switch (message)
{

case WM_DRAWITEM:
DRAWITEMSTRUCT *dis;

dis = (LPDRAWITEMSTRUCT) lParam;
if ( dis->CtlType == ODT_STATIC )
{
rect = dis->rcItem;
lw = dis->rcItem.right - dis->rcItem.left;
lh = dis->rcItem.bottom - dis->rcItem.top;
fh = lh; r = GetWindowText(dis->hwndItem,data,30);
data[r] = 0 ;
fw = lw/r;
hfont = CreateFont ( fh,fw,0,0,700,FALSE,FALSE,FALSE,
GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,DEFAULT_PITCH,"myf");
hfont =(HFONT) SelectObject ( dis->hDC,hfont);
SetBkMode ( dis->hDC,TRANSPARENT);
rect.right = rect.left + 2*fw;
for(i=0;i<r;++i)
{

SetTextColor ( dis->hDC,color[i%3]);
DrawText( dis->hDC,data+2*i,2,&rect,DT_BOTTOM  );
rect.left += 2*fw;
rect.right += 2*fw;
}
hfont =(HFONT) SelectObject ( dis->hDC,hfont);
DeleteObject(hfont);
SetTimer(dis->hwndItem,100,1000,TimerProc);
}
break;
case WM_CREATE:

CreateWindowA("STATIC","定制标签示例",WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
0,0,100,50,hWnd,0,hInst,0);
CreateWindowA("STATIC","标准标签",WS_CHILD | WS_VISIBLE ,
0,50,100,50,hWnd,0,hInst,0);
CreateWindowA("STATIC","12345678901234567890",WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
300,50,500,400,hWnd,0,hInst,0);

break;
//case WM_COMMAND:
// wmId    = LOWORD(wParam);
// wmEvent = HIWORD(wParam);
// // 分析菜单选择:
// switch (wmId)
// {
//
// default:
// return DefWindowProc(hWnd, message, wParam, lParam);
// }
// break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent,  DWORD dwTime)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
HFONT hfont;
char data[32];
int r;
int fw,fh;
int i; r = GetWindowText(hwnd,data,30);
data[r] = 0;
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
fw = (rect.right - rect.left)/r;
fh = rect.bottom -rect.top ;
hfont = CreateFont ( fh,fw,0,0,700,FALSE,FALSE,FALSE,
GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,DEFAULT_PITCH,"myf");
hfont =(HFONT) SelectObject (hdc,hfont);
SetBkMode ( hdc,TRANSPARENT);
rect.right = rect.left + 2*fw;
for(i=0;i<r;++i)
{

SetTextColor ( hdc,color[(k+i)%3]);
DrawText( hdc,data+2*i,2,&rect,DT_BOTTOM  );
rect.left += 2*fw;
rect.right += 2*fw;
}
hfont =(HFONT) SelectObject ( hdc,hfont);
DeleteObject(hfont);
EndPaint(hwnd,&ps);
++k;
}

解决方案 »

  1.   

    SetTimer();在OnTimer中改变颜色,然后调用InvalidateRect刷新
    OnPaint中SetTextColor()然后DrawText
      

  2.   


    OnTimer 和  OnPaint  是MFC 中的函数吗?? 2楼的大哥可以说明白一点吗,小弟不是很明白,我这个使用c写的
      

  3.   

    简单帮你改了一下
    你看看是不是你想要的效果
    另外
    你要是想每个标签变色相对独立那么可以考虑利用DRAWITEMSTRUCT的itemData参数
    #include <windows.h>COLORREF color[3] = { RGB(255,0,0) ,RGB(0,255,0),RGB(0,0,255)};
    HINSTANCE hInst;
    int k=1;VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent,  DWORD dwTime);
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR  lpCmdLine,int nCmdShow)
    {
        MSG msg;
        WNDCLASSEX wcex;
        
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc = WndProc;
        wcex.cbClsExtra = 0;
        wcex.cbWndExtra = 0;
        wcex.hInstance = hInstance;
        wcex.hIcon = 0;
        wcex.hCursor = 0;
        wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName = 0;
        wcex.lpszClassName = "mywin";
        wcex.hIconSm = 0;    RegisterClassEx(&wcex);
        CreateWindow("mywin", "", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 1000, 600, NULL, NULL, hInstance, NULL);    while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }    return 0;
    }LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        /*int wmId, wmEvent;*/
        PAINTSTRUCT ps;
        HDC hdc;
        char data[30];
        int r;
        int lw,lh;//控件大少
        int fw,fh;//字体大小
        int i;
        RECT rect;
        HFONT hfont;
        HINSTANCE hInst = GetModuleHandle(0);    switch(message)
        {
        
        case WM_DRAWITEM:
    DRAWITEMSTRUCT *dis;        dis = (LPDRAWITEMSTRUCT) lParam;
            if(dis->CtlType == ODT_STATIC )
            {
                rect = dis->rcItem;
                lw = dis->rcItem.right - dis->rcItem.left;
                lh = dis->rcItem.bottom - dis->rcItem.top;
                fh = lh;            r = GetWindowText(dis->hwndItem,data,30);
                data[r] = 0 ;
                fw = lw/r;
                hfont = CreateFont ( fh,fw,0,0,700,FALSE,FALSE,FALSE,
                    GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
                    DEFAULT_QUALITY,DEFAULT_PITCH,"myf");
                hfont = (HFONT) SelectObject ( dis->hDC,hfont);
                SetBkMode( dis->hDC, TRANSPARENT);
                rect.right = rect.left + 2*fw;
                for(i=0; i<r; ++i)
                {
                    
                    SetTextColor( dis->hDC,color[(i+k)%3]);
                    DrawText(dis->hDC, data+2*i, 2, &rect,DT_BOTTOM);
                    rect.left += 2*fw;
                    rect.right += 2*fw;
                }
                hfont =(HFONT) SelectObject ( dis->hDC,hfont);
                DeleteObject(hfont);
                SetTimer(dis->hwndItem,100,1000,TimerProc);
            }
            break;
        case WM_CREATE:
            
            CreateWindowA("STATIC","定制标签示例",WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
                                0,0,100,50,hWnd,0,hInst,0);
            CreateWindowA("STATIC","标准标签",WS_CHILD | WS_VISIBLE ,
                                0,50,100,50,hWnd,0,hInst,0);
            CreateWindowA("STATIC","12345678901234567890",WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
                                300,50,500,400,hWnd,0,hInst,0);
            
            break;
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent,  DWORD dwTime)
    {
    RECT rect;    ++k;
    GetClientRect(hwnd, &rect);
    InvalidateRect(hwnd, &rect, true);
    }
      

  4.   

    你在定时器的回函数中用BeginPaint和EndPaint是不行的。
    一般的方法将绘图还是放在WM_PAINT的处理部分中,而在定时器中用InvalidateRect进行更新。
      

  5.   

    不好意思
    刚刚弄错了
    static不能设置itemData
    应该通过窗口属性的USERDATA
    修改后代码如下
    仅供参考
    #include <windows.h>COLORREF color[3] = { RGB(255,0,0) ,RGB(0,255,0),RGB(0,0,255)};
    HINSTANCE hInst;HWND hWndStatic1;
    HWND hWndStatic2;VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent,  DWORD dwTime);
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR  lpCmdLine,int nCmdShow)
    {
        MSG msg;
        WNDCLASSEX wcex;
        
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc = WndProc;
        wcex.cbClsExtra = 0;
        wcex.cbWndExtra = 0;
        wcex.hInstance = hInstance;
        wcex.hIcon = 0;
        wcex.hCursor = 0;
        wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName = 0;
        wcex.lpszClassName = "mywin";
        wcex.hIconSm = 0;    RegisterClassEx(&wcex);
        CreateWindow("mywin", "", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 1000, 600, NULL, NULL, hInstance, NULL);    while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }    return 0;
    }LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        /*int wmId, wmEvent;*/
        PAINTSTRUCT ps;
        HDC hdc;
        char data[30];
        int r;
        int lw,lh;//控件大少
        int fw,fh;//字体大小
        int i;
        RECT rect;
        HFONT hfont;
        HINSTANCE hInst = GetModuleHandle(0);    switch(message)
        {
        
        case WM_DRAWITEM:
    DRAWITEMSTRUCT *dis;        dis = (LPDRAWITEMSTRUCT) lParam;
            if(dis->CtlType == ODT_STATIC )
            {
                rect = dis->rcItem;
                lw = dis->rcItem.right - dis->rcItem.left;
                lh = dis->rcItem.bottom - dis->rcItem.top;
                fh = lh;            r = GetWindowText(dis->hwndItem,data,30);
                data[r] = 0 ;
                fw = lw/r;
                hfont = CreateFont ( fh,fw,0,0,700,FALSE,FALSE,FALSE,
                    GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
                    DEFAULT_QUALITY,DEFAULT_PITCH,"myf");
                hfont = (HFONT) SelectObject ( dis->hDC,hfont);
                SetBkMode( dis->hDC, TRANSPARENT);
                rect.right = rect.left + 2*fw;
    long lData = GetWindowLong(hWndStatic2, GWL_USERDATA);
                for(i=0; i<r; ++i)
                {
                    
    SetTextColor( dis->hDC,color[(i+lData)%3]);
                    DrawText(dis->hDC, data+2*i, 2, &rect,DT_BOTTOM);
                    rect.left += 2*fw;
                    rect.right += 2*fw;
                }
                hfont =(HFONT) SelectObject ( dis->hDC,hfont);
                DeleteObject(hfont);
                SetTimer(dis->hwndItem,100,1000,TimerProc);
            }
            break;
    case WM_CREATE: hWndStatic1 = CreateWindowA("STATIC","定制标签示例",WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
    0,0,100,50,hWnd,0,hInst,0);
    ::SetWindowLong(hWndStatic1, GWL_USERDATA, 0);
    CreateWindowA("STATIC","标准标签",WS_CHILD | WS_VISIBLE ,
    0,50,100,50,hWnd,0,hInst,0);
    hWndStatic2 = CreateWindowA("STATIC","12345678901234567890",WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
    300,50,500,400,hWnd,0,hInst,0);
    ::SetWindowLong(hWndStatic2, GWL_USERDATA, 1);
            
            break;
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent,  DWORD dwTime)
    {
    RECT rect;
    long lTemp; lTemp = GetWindowLong(hWndStatic1, GWL_USERDATA);
    lTemp++;
    SetWindowLong(hWndStatic1, GWL_USERDATA, lTemp); lTemp = GetWindowLong(hWndStatic2, GWL_USERDATA);
    lTemp++;
    SetWindowLong(hWndStatic2, GWL_USERDATA, lTemp); GetClientRect(hwnd, &rect);
    InvalidateRect(hwnd, &rect, true);
    }