Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

function to display text rightside of window

kinnerakinnera Member Posts: 7
hi all

is there any function which helps to display text to the right side of the window in the title bar.

PLZ help.

Comments

  • anthrax11anthrax11 Member Posts: 511
    You could dynamically add spaces to the beginning of the title depending on how wide the window is, though I don't know why you would want to do this.
  • kinnerakinnera Member Posts: 7
    i want to display time there.
    should i do like this

    CString str;

    str=" ";

    str = strTitle + str + strtime;

    //strtitle is the title of window

    but can i use SetTextAlign() to do so.also can u give me an example ,how to use this function.plzzz
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]No function to do that, however, here is a workaround:
    1. You need to handle WM_NCPAINT message for the window with title
    2. In that message you need to call the base (default) implementation first
    3. After the base implementation you need to figure out where is the rectangle for the title and then draw time in the rectangle using the DrawText() function with DT_RIGHT flag:
    [link=http://msdn.microsoft.com/en-us/library/dd162498(VS.85).aspx]http://msdn.microsoft.com/en-us/library/dd162498(VS.85).aspx[/link]
    [/color]
  • anthrax11anthrax11 Member Posts: 511
    This is more of a hack, it won't work in fullscreen windows, because there's a title length limit of 256 characters. I'll just post this in case you're interested. AsmGuru62's idea is more robust, so use that.
    [code]
    case WM_SIZE:
    {
    int WindowWidth, EmptyWidth;
    int Padding = 110; // This could be calculated more accurately
    SIZE TitleSize, TimeSize, SpaceSize;
    TCHAR FullTitle[1024]; // This should be dynamically allocated

    WindowWidth = LOWORD(lParam);

    GetTextExtentPoint32(GetDC(hWnd), strTitle, lstrlen(strTitle), &TitleSize);
    GetTextExtentPoint32(GetDC(hWnd), strTime, lstrlen(strTitle), &TimeSize);
    GetTextExtentPoint32(GetDC(hWnd), L" ", 1, &SpaceSize);

    EmptyWidth = WindowWidth - TitleSize.cx - TimeSize.cx - Padding;
    EmptyWidth /= SpaceSize.cx;

    lstrcpy(FullTitle, strTitle);

    if (EmptyWidth > 0)
    {
    int i;
    for (i=0; i<EmptyWidth; i++)
    lstrcat(FullTitle, L" ");
    }

    lstrcat(FullTitle, strTime);

    SetWindowText(hWnd, FullTitle);

    break;
    }
    [/code]
  • kinnerakinnera Member Posts: 7
    thanks for ur reply anthrax11. can u plz explain these lines
    EmptyWidth = WindowWidth - TitleSize.cx - TimeSize.cx - Padding;
    EmptyWidth /= SpaceSize.cx;


    i did this
    [code]
    HWND ForeGrndWnd;
    void CMainWnd::OnTimer(UINT_PTR nIDEvent)
    {


    ForeGrndWnd = ::GetForegroundWindow();
    CWnd *pWindow;
    pWindow = CWnd::FromHandle(ForeGrndWnd);

    CHAR strTime[15];
    SYSTEMTIME sm;
    GetLocalTime(&sm);
    GetTimeFormat(LOCALE_SYSTEM_DEFAULT,LOCALE_USE_CP_ACP,&sm,"hh':'mm':'ss tt",(LPSTR)strTime,sizeof(strTime));

    CRect rect;
    pWindow->GetWindowRect(&rect);


    CPoint pt,ptwidth;
    ptwidth.x = rect.Width();
    pt.x = rect.Width()-160;


    CDC *hWndDC = pWindow->GetWindowDC();

    hWndDC->TextOut(pt.x,5,strTime,(char)strlen(strTime));
    ReleaseDC(hWndDC);
    }


    [/code]

    it display time to right side of window.but which ever window i select it displays even in system tray.
    can u plz tell how to avoid this.
  • anthrax11anthrax11 Member Posts: 511
    : thanks for ur reply anthrax11. can u plz explain these lines
    : EmptyWidth = WindowWidth - TitleSize.cx - TimeSize.cx - Padding;
    : EmptyWidth /= SpaceSize.cx;

    The window caption has 4 parts: the title, the space between the title and the clock, the clock itself and the buttons. The space consists of space characters (" "), which have a fixed width. GetTextExtentPoint32 gets the width of a string. We use it to get the length of the title, the time and of a single space character.

    The goal is to get the number of space characters needed to fill the space so that the clock always stays on the right side of the window. For that we take entire width of the window, subtract the title width, the time width and the width of the buttons(Padding). Then we're left with the number of pixels needed to fill the space. If we divide that by the width of a space character, then we get the number of space characters that need to be inserted between the title and the clock.

    : it display time to right side of window.but which ever window i
    : select it displays even in system tray.
    : can u plz tell how to avoid this.

    AsmGuru's solution will avoid this. That's why you should use that instead.
Sign In or Register to comment.