Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm upgrading the VC++6.0 32bit MFC application project to VC++2015 to be deployed into windows 7. Following is the method for alignment of the text.

Columns method:
C++
/**** "|" symbol are used as separators ****/

int CListCtrlEx::CreateColumns(LPCSTR fmt, ...)
{
   ClearAll();
   
   char* buffer = new char[strlen(fmt) + 256];
   
   va_list args;
   va_start (args,fmt);
   vsprintf (buffer,fmt,args);
   va_end (args);

   CStringEx cols = buffer;
   int col;
   for( col = 0; col < cols.GetFieldCount("|"); col++ )
      AddColumn(cols.GetField("|", col), col);

   delete [] buffer;

   delete [] m_arrayColumnClicks;
   m_arrayColumnClicks = NULL;
   m_arrayColumnClicks = new int[col];

   int i;
   for( i = 0; i < col; i++)
      m_arrayColumnClicks[i] = 1;

   AutoSizeColumns();

   return i; // return column count
}

int CListCtrlEx::AddColumn(LPCSTR desc, int col, LVCOLUMN* lvc)
{
   if( lvc && lvc->pszText == NULL )
      lvc->pszText = (char*)desc;

   LV_COLUMN t_lvc;

   t_lvc.mask    = lvc ? lvc->mask     : LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
   t_lvc.fmt     = lvc ? lvc->fmt      : LVCFMT_LEFT;
   t_lvc.cx      = lvc ? lvc->cx       : 75; // in pixels
   t_lvc.pszText = lvc ? lvc->pszText  : (char*)desc;

   return InsertColumn(col, &t_lvc);
}

void CListCtrlEx::AutoSizeColumns(int col)
{
//   SetRedraw(FALSE);
   int mincol =   col < 0 ? 0 : col;
   int maxcol =   GetColumnCount();

   for( col = mincol; col < maxcol; col++)
   {
      SetColumnWidth(col, LVSCW_AUTOSIZE);
      int wc1 = GetColumnWidth(col);

      SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
      int wc2 = GetColumnWidth(col);
      
      int wc = max(wc1, wc2);
      SetColumnWidth(col,wc);
   }

   Invalidate();
}


The above code works fine with VC++ 6.0 target OS windows xp. But doesn't in VC++2015 target Windows 7.
The worst part is the Debug version and Release version shows different type of alignment errors in column and text.

What I have tried:

It doesn't seems to be an issue with the code.
Is there any CListCtrl associated dll's that doesn't match the recent versions. I have included "legacy_stdio_definitions.lib" as my project mandates ? OR
May be the buffer overflow due to bit size not concurrent with the earlier developed project in the current version ? OR
Any other prerequisites to be done in the settings ?
Posted
Updated 16-Oct-16 23:26pm
v8
Comments
Jochen Arndt 5-Oct-16 3:42am    
What do you mean by "alignment error"?

The rendering of list controls and fonts has been changed since Windows XP so that getting a different representation on screen is normal.
Member 12776813 5-Oct-16 6:56am    
Can you suggest me how do I rectify those rendering errors.
I have to do Code wise or adding any external libraries would help.
*I'm new to VS, could you please provide me the details.
Jochen Arndt 5-Oct-16 7:14am    
I can't do so without knowing how these "errors" look like.
It would also be helpful to see the code of your AutoSizeColumns() function and may be the PlaceControls() code if it is involved in some way.

The list control is drawn by the system. The only method to change the look is using the provided API functions or using an owner drawn list (custom draw).
Member 12776813 5-Oct-16 7:36am    
Added the requested functions.
The display looks something like this,
Expected:
Field Columns
-both in different columns

Actual in Debug version:
Field|Status
In Release version:
Field Field|
-in different columns



int CStringEx::GetFieldCount( LPCTSTR delim )
{
LPTSTR lpsz, lpszRemainder = CA2T(CString::GetString());
int lenDelim = lstrlen( delim );

int iCount = 1;
while( (lpsz = _tcsstr(lpszRemainder, delim)) != NULL )
{
lpszRemainder = lpsz + lenDelim;
iCount++;
}

return iCount;
}

CStringEx CStringEx::GetField( TCHAR delim, int fieldnum)
{
LPTSTR lpsz, lpszRemainder = CA2T(CString::GetString()), lpszret;
int retlen;

while( fieldnum-- >= 0 )
{
lpszret = lpszRemainder;
lpsz = _tcschr(lpszRemainder, (_TUCHAR)delim);
if( lpsz )
{
// We did find the delim
retlen = lpsz - lpszRemainder;
lpszRemainder = lpsz + 1;
}
else
{
retlen = lstrlen( lpszRemainder );
lpszRemainder += retlen; // change to empty string
}
}
return Mid( lpszret - CString::GetString(), retlen );
}

These are the functions which takes care of the String delimitations.
Jochen Arndt 5-Oct-16 8:57am    
At first you should fix your ANSI / TCHAR conversions. The posted code should not even compile for Unicode builds:

Here you are passing a LPCSTR:
cols.GetFieldCount("|")

But the function expects a LPCTSTR:
int CStringEx::GetFieldCount( LPCTSTR delim )

Check this:
LPTSTR lpszRemainder = CA2T(CString::GetString());
With Unicode builds GetString() returns a LPCWSTR. Treating that as ANSI returns a string with a single character!

The GetField() function is declared as
CStringEx CStringEx::GetField( TCHAR delim, int fieldnum)

with TCHAR delimiter but you are passing a LPCSTR:
cols.GetField("|", col)

Overall these CStringEx functions can be implemented much better (and use one type of delimiter: character or string).

There should be even no need to do ANSI / Unicode conversions inside these functions. Just use the CString member functions and the _tXXX library functions.

If you need to use ANSI strings with Unicode builds, use the ANSI version CStringA explicitly and avoid these ATL conversion macros.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900