Solved

Forcing Read-only text field to the right

Posted on 2004-08-24
4
230 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 116
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 655
notReplace  challenge 53 125
no14 challenge 14 66
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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

775 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