begin process at 2008 09 06 04:53:28
1 237 644 membres
42 nouveaux aujourd'hui
14 313 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

NOTRESIZABLELISTVIEW : FIGER LA LARGEUR DES COLONNES


Information sur la source

Description

Cette classe bloque le redimensionnement des colonnes.

- Recuperation de l'objet columnheader.
- Vérification de la position du curseur et du hittest.
- Ne bloque les autres actions (Resize automatique , tri ...)

Source

  • //COPYRIGHT Original author ....
  • public class NotResizableListView: ListView
  • {
  • #region Interop
  • #region Consts
  • const int LVM_FIRST = 0x1000;
  • const int LVM_GETHEADER = LVM_FIRST + 31;
  • const int HDM_FIRST = 0x1200;
  • const int HDM_HITTEST = HDM_FIRST + 6;
  • const int HHT_ONDIVIDER = 0x0004;
  • const int WM_SETCURSOR = 0x0020;
  • const int WM_LBUTTONDOWN = 0x0201;
  • #endregion Consts
  • [StructLayout(LayoutKind.Sequential)]
  • struct HDHITTESTINFO
  • {
  • public Point pt;
  • public int flags;
  • public int iItem;
  • }
  • [DllImport("User32.dll")]
  • static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);
  • #endregion Interop
  • protected override void OnHandleCreated(EventArgs e)
  • {
  • base.OnHandleCreated(e);
  • //Recupere l'objet columnheader.
  • IntPtr hHeader = (IntPtr)SendMessage(Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
  • ColumnHeader nativeWindow = new ColumnHeader(hHeader);
  • }
  • private class ColumnHeader : NativeWindow
  • {
  • public ColumnHeader(IntPtr hHeader)
  • {
  • this.AssignHandle(hHeader);
  • }
  • protected override void WndProc(ref Message m)
  • {
  • switch(m.Msg)
  • {
  • case WM_SETCURSOR:
  • // Pas de changement de curseur
  • return;
  • case WM_LBUTTONDOWN:
  • Int16 x = (Int16)m.LParam;
  • Int16 y = (Int16)((int)m.LParam >> 16);
  • // Recuepere la position du cursor
  • Point cursorPosition = new Point(x, y);
  • HDHITTESTINFO hitInfo = new HDHITTESTINFO();
  • hitInfo.pt = cursorPosition;
  • IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(hitInfo));
  • Marshal.StructureToPtr(hitInfo, buffer, true);
  • //Declenche le hit, permet l'execution des autres actions lié au click sur le header
  • SendMessage(m.HWnd, HDM_HITTEST, IntPtr.Zero, buffer);
  • hitInfo = (HDHITTESTINFO)Marshal.PtrToStructure(buffer, typeof(HDHITTESTINFO));
  • Marshal.FreeHGlobal(buffer);
  • if (hitInfo.flags == HHT_ONDIVIDER)
  • //On est sur le separateur donc pas d'action
  • return;
  • break;
  • }
  • base.WndProc(ref m);
  • }
  • }
  • }
//COPYRIGHT Original author ....

	public class NotResizableListView: ListView 
	{		
		#region Interop
		#region Consts
		const int LVM_FIRST = 0x1000;    
		const int LVM_GETHEADER = LVM_FIRST + 31;    
		const int HDM_FIRST = 0x1200;
		const int HDM_HITTEST = HDM_FIRST + 6;        
		const int HHT_ONDIVIDER = 0x0004;    
		const int WM_SETCURSOR = 0x0020;    
		const int WM_LBUTTONDOWN = 0x0201;        
		#endregion Consts

		[StructLayout(LayoutKind.Sequential)]    
			struct HDHITTESTINFO    
		{
			public Point pt;        
			public int flags;       
			public int iItem;    
		}        
		[DllImport("User32.dll")]    
		static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam); 

		#endregion Interop

		protected override void OnHandleCreated(EventArgs e)    
		{       
			base.OnHandleCreated(e);     
			//Recupere l'objet columnheader.
			IntPtr hHeader = (IntPtr)SendMessage(Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);  
			ColumnHeader nativeWindow = new ColumnHeader(hHeader);  
		}   
		
		private class ColumnHeader : NativeWindow    
		{       
			public ColumnHeader(IntPtr hHeader)
			{ 
				this.AssignHandle(hHeader);       
			}   
    
			protected override void WndProc(ref Message m)
			{
				switch(m.Msg)          
				{             
					case WM_SETCURSOR:                
						// Pas de changement de curseur
						return;             
					case WM_LBUTTONDOWN:              
						Int16 x = (Int16)m.LParam;                
						Int16 y = (Int16)((int)m.LParam >> 16);
						// Recuepere la position du cursor
						Point cursorPosition = new Point(x, y);
						HDHITTESTINFO hitInfo = new HDHITTESTINFO();
						hitInfo.pt = cursorPosition;                
						IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(hitInfo));                
						Marshal.StructureToPtr(hitInfo, buffer, true);         
						//Declenche le hit, permet l'execution des autres actions lié au click sur le header
						SendMessage(m.HWnd, HDM_HITTEST, IntPtr.Zero, buffer); 
						hitInfo = (HDHITTESTINFO)Marshal.PtrToStructure(buffer, typeof(HDHITTESTINFO));
						Marshal.FreeHGlobal(buffer);                
						if (hitInfo.flags == HHT_ONDIVIDER)                   
							//On est sur le separateur donc pas d'action
							return;                
						break;          
				}

				base.WndProc(ref m);       
			}    
		} 
	
	}

Conclusion

C'est un bout répéré je ne sais plus ou il y a quelques mois;
  • signaler à un administrateur
    Commentaire de wald39 le 17/03/2006 13:21:49

    il y a des erreurs dans le code ou alors il manque des using car "StructLayout" , "DllImport" , "NativeWindow" et "Message" sont introuvable.

  • signaler à un administrateur
    Commentaire de TheSaib le 17/03/2006 14:31:46 administrateur CS

    Je n'ai mis aucun using.

    Le using qui manque naturellement est le using System.Runtime.InteropServices;

    Celà paraissait logique.

Ajouter un commentaire

Discussions en rapport avec ce code source

Pub



Appels d'offres

CalendriCode

Septembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
2930     

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS