Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
// This is the the file for list view. I am passing handle of List Control to this function.

#include <windows.h>                                                              
#include <stdio.h>                                                                
#include <commctrl.h>                                                             
int fInsertListViewColumn(HWND hwndListView, long lCol, int iPercent, TCHAR text[])
{

	LVCOLUMN lvcolumn;
	
	RECT rect;
	GetClientRect(hwndListView, &rect);
	
	int iResult;
	
	iPercent = iPercent > 10 ? min(iPercent, 90) : 10;
	
	int iWidth = (int) (rect.right * (iPercent / 100.0));
	
	if (hwndListView == NULL)
	{
			MessageBox(NULL, TEXT("! Handle of ListView NULL (fInsertListViewColumn)"), TEXT("fire"), MB_OK | MB_ICONEXCLAMATION);
		return(0);
	}
	
	memset(&lvcolumn, 0, sizeof(lvcolumn));
	lvcolumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
	lvcolumn.fmt = LVCFMT_LEFT;
	lvcolumn.pszText = text;
	lvcolumn.iSubItem = lCol;
	lvcolumn.cx = iWidth;
	
	if (SendMessage((HWND) hwndListView, (UINT) LVM_INSERTCOLUMN, (WPARAM) (int) lCol, (LPARAM) &lvcolumn) == -1) iResult = 0; else iResult = 1;
	
	InvalidateRect(hwndListView, &rect, TRUE);
	
	return(iResult);
}

int fInsertListViewItem(HWND hwndListView, long lLin, long lCol, int iSubItemYesNo, TCHAR text[])
{
	LVITEM lvi;
	
	RECT rect;
	GetClientRect(hwndListView, &rect);
	
	int iResult;
	
	if (hwndListView == NULL)
	{
		MessageBox(NULL, TEXT("! Handle of ListView NULL (fInsertListViewItem)"),TEXT("fire"), MB_OK | MB_ICONEXCLAMATION);
		return(0);
	}
	
	memset(&lvi, 0, sizeof(lvi));
	lvi.mask = LVIF_TEXT;
	lvi.state = 0;
	lvi.stateMask = 0;
	lvi.pszText = text;
	lvi.iItem = lLin;
	lvi.iSubItem = lCol;
		
	switch(iSubItemYesNo)
	{
		case 0:
			if (SendMessage((HWND) hwndListView, (UINT) LVM_INSERTITEM, (WPARAM) 0, (LPARAM) &lvi) == -1) iResult = 0; else iResult = 1;
			break;
		case 1:
			if (SendMessage((HWND) hwndListView, (UINT) LVM_SETITEM, (WPARAM) 0, (LPARAM) &lvi) == FALSE) iResult = 0; else iResult = 1;
			break;
		default:
			MessageBox(NULL, TEXT("! Unexpected iSubItemYesNo (fInsertListViewItem)"), TEXT("fire"), MB_OK | MB_ICONEXCLAMATION);
			return(0);
			break;
	}
	
	InvalidateRect(hwndListView, &rect, TRUE) ;
	
	return(iResult);
}


//This is my Dialog Procedure
BOOL CALLBACK TcpDlgProc (HWND hTcpDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	 switch (message)
     {
     case WM_INITDIALOG :
		  GetTCPConn(hTcpDlg);
		  return TRUE ;
          
     case WM_COMMAND :
          switch (LOWORD (wParam))
          {
          case IDOK :
          case IDCANCEL :
               EndDialog (hTcpDlg, 0);
               return TRUE ;
          }

	 case WM_DESTROY:
		  EndDialog(hTcpDlg,0);
		  return TRUE ;
          break ;
     }
     return FALSE ;
}

// This is the function which i am calling in my dialog Procedure
void GetTCPConn(HWND hTcpDlg)
{

	//handle to list view
	hwndLV = GetDlgItem(hTcpDlg, IDC_LIST2);

	// Setup the list box
	fInsertListViewColumn(hwndLV, 0, 30, TEXT("A (column)"));
        fInsertListViewColumn(hwndLV, 1, 30, TEXT("B (column)"));

        fInsertListViewItem(hwndLV, 0, 0, 0, TEXT("A1(item)"));
        fInsertListViewItem(hwndLV, 1, 0, 0, TEXT("A2 (item)"));
}


When i remove List control Dialog Box display but as soon as i add List view(List Control) in dialog box it doesn't work.
Same is working fine in windows 7. Please help me asap. Have to complete my project by tomorrow morning.

Help me

Thank you in Advance



Edit: Pre Tags and missing (") added
Posted
Updated 7-Apr-12 23:36pm
v5

Did you call InitCommonControlsEx()?

Hope this helps,

Pablo.
 
Share this answer
 
Comments
amityadav4a 5-Mar-12 12:07pm    
No But same code is working fine in windows 7. It is not working on my pc windows xp.
amityadav4a 8-Apr-12 5:46am    
How to use InitCommonControlsEx()

and same code is working fine in windows 7.
This is what should go into your InitInstance function:

// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles.  Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);


I think the automatically generated comments explain what it does. Under Windows 7 the operating system does this automatically for you. Under XP it needs to be done as shown above, otherwise any window creation of a common control will fail.
 
Share this answer
 
Comments
Sergey Chepurin 8-Apr-12 9:52am    
You have to use InitCommonControlsEx() if you add common controls manually or want to add visual styles to your application. But if you add controls to Dialog Box via Resource editor (as, i guess, in this case because he addresses it by IDC_LIST2) it is already registered. And InitCommonControlsEx() has no effect.

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



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