Solved

Forcing Read-only text field to the right

Posted on 2004-08-24
4
233 Views
Last Modified: 2013-11-20
I have an MFC text box that contains a file path. I'm running short on screen real estate, and the text box is smaller than the path. I want to have the text box display the end of the filepath instead of the beginning. Is there an easy way to do this?
0
Comment
Question by:tcf22
  • 2
4 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 11883812
You need to change it to ownerdrawn. Then you can test the size of the full string and display only the right hand part.  
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11883994
You can send a WM_SETSEL message to the control passing position of last character. Something like:

CString text = "c:\\your main folder\\your subfolder\\additional folder\\the file.txt";

yourControl.SetWindowText(text);
yourControl.SetSel(text.GetLenght(), text.GetLenght());

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 11885502
>> Is there an easy way to do this?

No, but you can do this:

void CTestDlgDlg::ShowFilePath(const CString& strPath)
{
     CWnd*     pEdit = GetDlgItem(IDC_EDIT);
     CFont*    pFont = pEdit->GetFont();
     CClientDC cdc(this);
     CFont*    pOldFont = cdc.SelectObject(pFont);
     CRect     rectEdit;
     pEdit->GetClientRect(&rectEdit);

     CString str = strPath;
     int        len;
     bool     first = true;
     while ((len = str.GetLength()) > 10)
     {
          CSize sz = cdc.GetTextExtent(str, len);
          if (sz.cx < rectEdit.Width())
               break;
          if (first)
          {
               str = str.Left(3) + "..." + str.Mid(6);  
               first = false;
          }
          else
          {
               str = str.Left(6) + str.Mid(7);
          }
     }
     cdc.SelectObject(pOldFont);

     pEdit->SetWindowText(str);  
}

Call it in OnInitDialog(..) after UpdateData(FALSE) or whenever you have to show a new file name like that

 ShowFilePath("c:\\Documents and Settings\\p602078\\more settings\\anything\\stuff\\mydata.dat",
                     IDC_EDIT);

Regards, Alex




0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11885522
Correction:

void CTestDlgDlg::ShowFilePath(const CString& strPath, UINT idcEdit)
{
     CWnd*     pEdit = GetDlgItem(idcEdit);
     ...


Regards, Alex
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question