循环中的Win32 API SetTimer

时间:2015-04-22 12:52:54

标签: winapi

是否可以在循环中使用SetTimer()函数?在WM_TIMER中我有一个KillTimer()函数,它过早地结束了SetTimer()的循环。这里有一些代码,应该每秒绘制一条垂直线,在X坐标中增加10点。一旦我到达对话框的末尾,应再次调用SetTimer(),但这次所有新行需要在前一行的右边1点。这个循环发生了10次,它应该使对话框的整个宽度变成随机颜色。

int x5 = 0 ; int y5 = 10; int y6 = 100; //DrawTimer points global variables
int n=0 //counter global variable

INT_PTR CALLBACK DrawTimer(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
    return (INT_PTR)TRUE;

case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    {
        EndDialog(hDlg, LOWORD(wParam));
        return (INT_PTR)TRUE;
    }
    else if (LOWORD(wParam) == ID_START)
    {
        int i = 0;
        while(true)
        {
            SetTimer(hDlg,1,1000,NULL);
            x5 = 0;
            i++;
            x5+=i;
            if(i == 10)
                break;
        }
    }
    break;

    case WM_TIMER:
        switch(wParam)
        {
            case 1:
                PAINTSTRUCT ps;
                HDC hdc;
                hdc = BeginPaint(hDlg, &ps);
                HPEN hPen;
                HGDIOBJ hObj;
                RECT rcClient;
                int diff;
                GetClientRect(hDlg, &rcClient);
                diff = rcClient.right - rcClient.left;
                int r = rand()%255;
                int g = rand()%255;
                int b = rand()%255;
                hdc = GetDC(hDlg);
                hPen = CreatePen(NULL,1,RGB(r,g,b));
                hObj = SelectObject(hdc,hPen);
                MoveToEx(hdc,x5,y5,NULL);
                LineTo(hdc,x5,y6);
                x5+=10;
                n++;
                SetDlgItemInt(hDlg,ID_COUNT,n,NULL);
                if (x5 > diff)
                KillTimer(hDlg,1);
                EndPaint(hDlg, &ps);
        }
        break;
}
return (INT_PTR)FALSE;
}

0 个答案:

没有答案