dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: CEdit alignment problem

  1. #1
    Guest

    CEdit alignment problem

    I have an edit control, which has the text right aligned. It works fine in Win98 an NT but in the Win95 the text stays left aligned...
    The only thing I do is ModifyStyle(0, ES_RIGHT), but i doesn't seems to work.
    Any clue ???


  2. #2
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    2,851

    Re: CEdit alignment problem

    You can't modify an edit control's style after it has been created, i have a ceditview based application, and i tried to toggle wordwrapping.
    here is the way to change the style:

    // preserve original control's state.
    int nLen = m_edit.GetBufferLength();
    TCHAR* pSaveText = new TCHAR[m_edit.GetBufferLength()+1];
    m_edit.GetWindowText(pSaveText, nLen+1);

    // create new edit control with appropriate style and size.
    DWORD dwStyle = m_edit.GetStyle() & ~ES_RIGHT;
    //if i wan't to add the style:
    dwStyle |= ES_RIGHT;

    CWnd* pParent = m_edit.GetParent();
    CRect rect;
    m_edit.GetWindowRect(rect);
    pParent->ScreenToClient(rect);
    CWnd* pFocus = m_edit.GetFocus();

    UINT nID = m_edit.GetDlgCtrlID();

    HWND hWnd = ::CreateWindowEx(WS_EX_CLIENTEDGE, _T("edit"), NULL, dwStyle,
    rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
    pParent->m_hWnd, (HMENU)nID, AfxGetInstanceHandle(), NULL);

    if (hWnd == NULL)
    {
    delete[] pSaveText;
    return FALSE;
    }

    // set the window text to nothing to make sure following set doesn't fail
    m_edit.SetWindowText(NULL);

    // restore visual state
    ::SetWindowText(hWnd, pSaveText);
    delete[] pSaveText;
    // detach old window, attach new
    m_edit.SetDlgCtrlID(nID+1);
    HWND hWndOld = m_edit.Detach();
    ::SetWindowLong(hWndOld, GWL_WNDPROC, (LONG)*GetSuperWndProcAddr());
    ASSERT(m_edit.m_hWnd == NULL);
    m_edit.SubclassWindow(hWnd);
    ASSERT(m_edit.m_hWnd == hWnd);
    m_edit.GetClientRect(&rect);
    m_edit.SetWindowPos(NULL, 0, 0, 0, 0,
    SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_NOZORDER|SWP_SHOWWINDOW);
    m_edit.SetWindowPos(NULL, 0, 0, 0, 0,
    SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_NOZORDER|SWP_DRAWFRAME);
    m_edit.SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
    m_edit.UpdateWindow();

    // destroy old
    ::SetWindowPos(hWndOld, NULL, 0, 0, 0, 0,
    SWP_HIDEWINDOW|SWP_NOREDRAW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
    SWP_NOZORDER);
    :estroyWindow(hWndOld);

    // restore rest of state...
    if (pFocus == &m_edit)
    m_edit.SetFocus();

    return TRUE;
    }



    Daniel


  3. #3
    Guest

    Re: CEdit alignment problem

    Very good !! But i still have the alignment problem in Win95 machines...


  4. #4
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    2,851

    Re: CEdit alignment problem

    I don't have win95, but try checking if ES_LEFT is in the style, if it is, remove it.


  5. #5
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    2,851

    Re: CEdit alignment problem

    FROM MSDN:

    ES_LEFT, ES_RIGHT, and ES_CENTER specify the alignment the text in an edit control should have. Single-line edit controls can only be left-aligned. ES_LEFT is the default for both single-line and multiple-line edit controls. Single-line edit controls ignore ES_RIGHT and ES_CENTER. Multiple-line edit controls can be right-aligned, left-aligned, or centered. Right-aligned and centered edit boxes cannot have horizontal scroll bars and cannot have the ES_AUTOHSCROLL style.

    Hope this solves your problem,
    Daniel


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)