ListView and overrided WndProc

C

cyrille

Hello

from example from web i did a little code to avoiding columnHeader resize.
this code seems to work well,
but when I put a 'normal' ListView on the same Form than my overrided ListView
it seems to stop working.

if I'm resizing 'normal' ListView ColumnHeaders, then I can resizing ColumnHeaders of my overrided ListView.
If I don't resizing 'normal' ListView ColumnHeaders, then resizing of my overrided ListView ColumnHeaders is disabled.

Here is the code.
To test, create a Form, put on it a ListView and one MyListView.

To test, move the mouse on MyListView columnHeaders and you can see that resizing is disabled.
Then resize ListView columnsHeaders, then you will be able to resize MyListView columnHeaders.

It's looking like loosing the NativeWindow Handle ;o{{


cyrille



using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics ;

using System.Runtime.InteropServices ; // DllImport, StructLayoutAttribute

namespace MyListView
{
/// <summary>
/// Description résumée de MyListView.
/// </summary>
public class MyListView : ListView
{

#region Code généré par le Concepteur de composants

/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
private System.ComponentModel.Container components = null;

/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}

#endregion

public MyListView()
{
// Cet appel est requis par le Concepteur de formulaires Windows.Forms.
InitializeComponent();

// TODO : ajoutez les initialisations après l'appel à InitComponent

}

/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

[DllImport("User32.dll")]
static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);

//************* Win32 messages constants, structs and functions **************
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 ;

[StructLayout(LayoutKind.Sequential)]
struct HDHITTESTINFO
{
public Point pt;
public int flags;
public int iItem;
}

protected override void OnHandleCreated(EventArgs e)
{
Debug.WriteLine( "OnHandleCreated() " + e.ToString() );

base.OnHandleCreated( e );

/*
lResult = SendMessage( // returns LRESULT in lResult
(HWND) hWndControl, // handle to destination control
(UINT) LVM_GETHEADER, // message ID
(WPARAM) wParam, // = 0; not used, must be zero
(LPARAM) lParam // = 0; not used, must be zero );
*/
IntPtr hHeader = (IntPtr)SendMessage( Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);

HeaderNativeWindow nativeWindow = new HeaderNativeWindow( hHeader );

}

class HeaderNativeWindow : NativeWindow
{
public HeaderNativeWindow(IntPtr hHeader)
{
Debug.WriteLine( "HeaderNativeWindow() " + hHeader );

AssignHandle( hHeader );
}

protected override void WndProc(ref Message m)
{
Debug.WriteLine( "WndProc() m.Msg = " + m.Msg);

switch(m.Msg)
{
case WM_SETCURSOR:
// do not allow change cursor to "resizable"
return;

case WM_LBUTTONDOWN:

Debug.WriteLine( "WndProc() WM_LBUTTONDOWN" );

//Get cursor position(in client coordinates)
Int16 x = (Int16)m.LParam;
Int16 y = (Int16)((int)m.LParam >> 16);
Point cursorPosition = new Point(x, y);

//sending HDM_HITTEST message for determination further actions
HDHITTESTINFO hitInfo = new HDHITTESTINFO();
hitInfo.pt = cursorPosition ;
IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(hitInfo));
Marshal.StructureToPtr(hitInfo, buffer, true);

SendMessage(m.HWnd, HDM_HITTEST, IntPtr.Zero, buffer);

hitInfo = (HDHITTESTINFO)Marshal.PtrToStructure(buffer, typeof(HDHITTESTINFO));
Marshal.FreeHGlobal(buffer);

if( hitInfo.flags == HHT_ONDIVIDER )
{
//do not allow handling WM_LBUTTONDOWN (mouse was pressed for column resizing)
return ;
}
//allow handling WM_LBUTTONDOWN
break;
}
base.WndProc(ref m);
}
}



/* ListView WndProc
*
protected override void WndProc(ref Message m)
{
if (m.Msg==WM_NOTIFY )
{
NMHDR nm =(NMHDR) m.GetLParam(typeof(NMHDR));
if (nm.code==HDN_ITEMCHANGEDW)
{
int right=0;
int left=0;
TOOLINFO ti=new TOOLINFO();
ti.cbSize=Marshal.SizeOf(ti);
ti.uFlags=TTF_SUBCLASS;
ti.hWnd=headerHandle;
for (int i=0; i<this.Columns.Count; i++)
{
if (i<this.Columns.Count)
right+=this.Columns.Width;
else
right=this.Width;
ti.uID=i;
if (i<this.Columns.Count)
ti.pszText=this.Columns.Text;
else
ti.pszText="";
Rectangle rect= new System.Drawing.Rectangle(left,0,right,20);
ti.rect=rect;
// SendMessage(toolTipHandle, TTM_DELTOOLW, 0, ref ti);
SendMessage(toolTipHandle, TTM_ADDTOOLW, 0, ref ti);
left=right;
}
}
}
base.WndProc (ref m);
}
*/

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top