就是设一个定时器,第一秒显示A,第二秒显示B,一次类推.
显示的位置和字母表的顺序有关,即B显示在A的右边,C显示在B的右边,以此类推
显示完字母Z之后,又从Z到A倒着显示. case WM_TIMER:
if(bRight)
{
i++;
if (i==26)
{
bRight=FALSE;
j=0;
} }
else
{
j++;
i=26-j;
if (i==0)
{
bRight=true;
i=0;
}
} InvalidateRect(hWnd,NULL,TRUE);
break; case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT rt;
GetClientRect(hWnd, &rt);
TextOut(hdc,i*(avgWidth+15),0,(char*)&szText[i],1);
                            EndPaint(hWnd, &ps);
break;
请问哪里出错了??

解决方案 »

  1.   

    j++;
    i=26-j;
    if (i==0)
    {
    bRight=true;
    i=0;//是不是应该改成j=0;
    }
      

  2.   

    还是不行啊,这是全部的代码
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR szHello[MAX_LOADSTRING];
    LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
    RECT rect;
    char strHello[6]="hello";
    static char szText[26]={'a'};
    for (int idx=0;idx<26;idx++)
    {
    szText[idx]='a'+idx;
    }
    static int x=0,i=0,j=1;
    static int avgWidth,charHeight;
    bool bRight=TRUE; switch (message) 
    {
    case WM_COMMAND:
    wmId    = LOWORD(wParam); 
    wmEvent = HIWORD(wParam); 
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_ABOUT:
       DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
       break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
    case WM_TIMER:
    if(bRight)
    {
    i++;
    if (i==26)
    {
    bRight=FALSE;
    j=0;
    } }
    else
    {
    j++;
    i=26-j;
    if (i==0)
    {
    bRight=true;
    j=0;
    }
    } InvalidateRect(hWnd,NULL,TRUE);
    break;
    case WM_CREATE:
    hdc=GetDC(hWnd);
    TEXTMETRIC tm;
    GetTextMetrics(hdc,&tm);
    avgWidth=tm.tmAveCharWidth;
    charHeight=tm.tmHeight+tm.tmExternalLeading;
    SetTimer(hWnd,1,500,NULL);
    ReleaseDC(hWnd,hdc);
    break;
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    RECT rt;
    GetClientRect(hWnd, &rt);
    TextOut(hdc,i*(avgWidth+15),0,(char*)&szText[i],1);
    //TextOut(hdc,0,charHeight,szText,i-1);
    //  for (x=0;x<i;x+=bRight)
    //  {
    //  TextOut(hdc,0,charHeight,szText,x);
    //  TextOut(hdc,x*avgWidth,charHeight,(char*)&szText[x+1],26-x-1);
    //  }
    EndPaint(hWnd, &ps);
    break;
    case WM_DESTROY:
    KillTimer(hWnd,1);
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }// Mesage handler for about box.
    LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message)
    {
    case WM_INITDIALOG:
    return TRUE; case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
    {
    EndDialog(hDlg, LOWORD(wParam));
    return TRUE;
    }
    break;
    }
        return FALSE;
    }
      

  3.   


    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR szHello[MAX_LOADSTRING];
    LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
    RECT rect;
    char strHello[6]="hello";
    static char szText[26]={'a'};
    for (int idx=0;idx<26;idx++)
    {
    szText[idx]='a'+idx;
    }
    static int x=0,i=0,j=1;
    static int avgWidth,charHeight;
    int bRight=1; switch (message) 
    {
    case WM_COMMAND:
    wmId    = LOWORD(wParam); 
    wmEvent = HIWORD(wParam); 
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_ABOUT:
       DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
       break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
    case WM_TIMER:
    i=i%26;
    if (i==25||i==0)
    {
    bRight=-bRight; }
    i=i+bRight; InvalidateRect(hWnd,NULL,TRUE);
    break;
    case WM_CREATE:
    hdc=GetDC(hWnd);
    TEXTMETRIC tm;
    GetTextMetrics(hdc,&tm);
    avgWidth=tm.tmAveCharWidth;
    charHeight=tm.tmHeight+tm.tmExternalLeading;
    SetTimer(hWnd,1,500,NULL);
    ReleaseDC(hWnd,hdc);
    break;
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    RECT rt;
    GetClientRect(hWnd, &rt);
    TextOut(hdc,i*(avgWidth+15),0,(char*)&szText[i],1);
    EndPaint(hWnd, &ps);
    break;
    case WM_DESTROY:
    KillTimer(hWnd,1);
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }改成这样子了,还是不对啊
      

  4.   

    TextOut(hdc,i*(avgWidth+15),0,(char*)&szText[i],1);只输出一个。
    而:
    InvalidateRect(hWnd,NULL,TRUE);是全部无效,(即上次的输出会被擦除)。
    1》。如果无效全部,则要输出 上次的和这次的。
    2》。如果无效一个(即那个你要输出的字符位置),则输出一个。
      

  5.   

    TextOut(hdc,i*(avgWidth+15),0,(char*)&szText[i],1);
    是不是 i*(avgWidth)+15。15是偏移。
    这个i决定了位置。
    调试:(不要用断点,或 MessageBox)
    char prompt[40];
    sprintf(prompt,"%d",i);
    OutputDebugString(prompt);
      

  6.   

    调了一下:i最大是25
        if(bRight)
        {
            i++;
            if (i==25)
            {
                bRight=FALSE;
                j=0;
            }
        }
        else
        {
            j++;
            i=25-j;
            if (i==0)
            {
                bRight=TRUE;
            }
        }
        InvalidateRect(0);
    // X不要变,i*(avgWidth+15) 可能出界了!
        pDC->TextOut(2,2,(char*)&szText[i],1);