?
Solved

Forcing Read-only text field to the right

Posted on 2004-08-24
4
Medium Priority
?
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 375 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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