Hi fellows.
butthead, thanks for helping
hennep,
I seem to get the same results as you, clipping of the last letter occurs.
I also need a function that calculates the occupied space taking into account the font, fontsize etc. The only one I have found so far is DrawText.
Looking at the filectrl.pas file, it has the source for MinimizeName, you can see they use TextWidth
Snip from BCB Help
Description
Use TextWidth to determine the length a string will occupy in the image. TextWidth indicates whether a given string will fit in the available space. Other graphical elements in the image such as lines, or additional strings can be positioned to accommodate the width of the text.
TextWidth returns the same value as TextExtent(Text).cx.
Anyways, after all the talk, seems nothing is 100% perfect :-(
#include <vcl.h>
#include <FileCtrl.hpp>
#include <Shlwapi.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm2::Shorten_File_Name (char *string, int x)
// x = the target path length. If the extracted file name
// is large enough it will possibly exceed the target length.
// Windows filenames can get quite large and you may want to
// allow the ability to shorten that if exceeds a certain length.
// The function will handle this but the results as coded may
// not fit your preferences.
// input - C:\Program Files\Borland\CBuilder5\Examples\Apps\Cursors\readme.txt
// output - C:\Progr...\readme.txt
{
int length = strlen (string);
int a;
int b;
if (length > x)
{
char *buff1 = new char [60]; // filename max length
char *buff2 = new char [256];
strcpy (buff1, ExtractFileName(string).c_str ());
a = x - strlen (buff1);
if (a > 3) // if there is enough room.
{
for (b = 0; b < (a - 2); b++)
buff2
= string ;
buff2 = NULL;
}
else // this just appends the file name to the drive name
// with the "..." in the middle.
{
for (b = 0; b < 3; b++)
buff2 = string ;
buff2 = NULL;
}
strcat (buff2, "...\\"
;
strcat (buff2, buff1);
strcpy (string, buff2);
delete buff1;
delete buff2;
}
Label2->Caption=string;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
Label1->AutoSize = false;
Label1->Caption = "123456789012345";
Label1->Color = clWhite;
Label1->Width = 105;
Label1->Font->Name = "MS Sans Serif";
Label1->Font->Color = clWindowText;
Label1->Font->Size = 8;
Label1->Font->Style = TFontStyles() << fsBold;
Label2->AutoSize = false;
Label2->Caption = "123456789012345";
Label2->Color = clWhite;
Label2->Width = 105;
Label2->Font->Name = "MS Sans Serif";
Label2->Font->Color = clWindowText;
Label2->Font->Size = 8;
Label2->Font->Style = TFontStyles() << fsBold;
Label3->AutoSize = false;
Label3->Caption = "123456789012345";
Label3->Color = clWhite;
Label3->Width = 105;
Label3->Font->Name = "MS Sans Serif";
Label3->Font->Color = clWindowText;
Label3->Font->Size = 8;
Label3->Font->Style = TFontStyles() << fsBold;
Label4->AutoSize = false;
Label4->Caption = "123456789012345";
Label4->Color = clWhite;
Label4->Width = 105;
Label4->Font->Name = "MS Sans Serif";
Label4->Font->Color = clWindowText;
Label4->Font->Size = 8;
Label4->Font->Style = TFontStyles() << fsBold;
Label5->AutoSize = false;
Label5->Caption = "123456789012345";
Label5->Color = clWhite;
Label5->Width = 105;
Label5->Font->Name = "MS Sans Serif";
Label5->Font->Color = clWindowText;
Label5->Font->Size = 8;
Label5->Font->Style = TFontStyles() << fsBold;
//-------Label1
Label1->Caption = MinimizeName("c:\\windows\\command.com", Label1->Canvas, Label1->Width );
//-------Label2
Shorten_File_Name ("c:\\windows\\command.com", 15);
//-------Label3
RECT BoundingRect = { 0, 0, Label3->Width, Label3->Height };
String strText;
strText = "c:\\windows\\command.com";
DrawText( Label3->Canvas->Handle, strText.c_str(), strText.Length(), &BoundingRect, DT_MODIFYSTRING | DT_PATH_ELLIPSIS );
Label3->Caption = strText;
//-------Label4
Label4->Caption = "c:\\windows\\command.com";
char *p = Label4->Caption.c_str();
HANDLE TempDC = CreateCompatibleDC( this->GetDeviceContext(this->Handle) );
DrawText( TempDC, p, strlen(p), &BoundingRect, DT_PATH_ELLIPSIS + DT_MODIFYSTRING );
Label4->Caption = p;
DeleteDC( TempDC );
//-------Label5
char buffer_1[MAX_PATH] = "c:\\windows\\command.com";
char *lpStr;
lpStr = buffer_1;
int retval = PathCompactPath(Label5->Canvas->Handle,lpStr,Label5->Width );
Label5->Caption = lpStr;
//-------
}
//---------------------------------------------------------------------------
There are some new API functions PathCompactPath and PathCompactPathEx , you must have a minimum of IE4 installed though. See MSDN for more info( and there is AbbreviateName from the MS MFC file filelist.cpp.
When I have more time I'll try to look into this in more detail.