在窗口中更改标签

时间:2013-03-12 08:03:03

标签: c++ winapi visual-c++

我想在vc ++窗口中更改标签。标签显示实时数据,每100毫秒更新一次。那么我怎么能在vc ++窗口中这样做呢?任何人请帮忙

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HWND hRPMEx,hAvgTimeEx;
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch(message)
    {
    case WM_TIMER:
        break;

    case WM_CREATE:
        hRPMEx  = CreateWindowA("Static","",WS_CHILD | WS_VISIBLE | SS_LEFT,
            20,100,220,25,
            hWnd, (HMENU)IDC_RPM_EX, hInstance, NULL);  

        hAvgTimeEx  = CreateWindowA("Static","",WS_CHILD | WS_VISIBLE | SS_LEFT, 
            20,150,220,25,
            hWnd, (HMENU)IDC_AVGTIME_EX, hInstance, NULL);  
        break;

    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:

        switch (wmId)
        {
        case IDC_BTN_CONNECT:
            break;

        case IDC_RPM_EX:
            break;

        case IDC_BTN_DISCONNECT:
            break;

        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;

        case IDM_OPEN:
            break;

        case IDM_EXIT:
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }

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

我应该在哪里调用更新标签的函数(静态文本)

1 个答案:

答案 0 :(得分:1)

两件事:

  1. 要定期更新内容,您可以使用计时器。一个例子:

        // To set the timer
        SetTimer(hwnd,             // handle to main window 
        IDT_TIMER1,            // timer identifier 
        10000,                 // 10-second interval 
        (TIMERPROC) NULL);     // no timer callback 
    
        // In you WindProc Callback :
        case WM_TIMER:
        {
            if ( wParam == IDT_TIMER1)
            {
                  // put your code here
            } 
    
        }
    
  2. 此处的示例:http://msdn.microsoft.com/en-us/library/windows/desktop/ms644901(v=vs.85).aspx

    1. 要更改窗口的标签:

      BOOL WINAPI SetWindowText(
        _In_      HWND hWnd,
        _In_opt_  LPCTSTR lpString
      );
      
    2. 此处的示例:http://msdn.microsoft.com/en-us/library/windows/desktop/ms633546(v=vs.85).aspx