Click here to Skip to main content
15,918,275 members
Home / Discussions / C#
   

C#

 
GeneralRe: DataGrid not update Pin
dekhle17-Aug-04 5:56
dekhle17-Aug-04 5:56 
GeneralRe: DataGrid not update Pin
Heath Stewart18-Aug-04 11:02
protectorHeath Stewart18-Aug-04 11:02 
Generalclosing forms Pin
steve_rm17-Aug-04 5:20
steve_rm17-Aug-04 5:20 
GeneralRe: closing forms Pin
Anonymous17-Aug-04 5:53
Anonymous17-Aug-04 5:53 
GeneralRe: closing forms Pin
mav.northwind17-Aug-04 6:17
mav.northwind17-Aug-04 6:17 
GeneralRe: closing forms Pin
loop0722-Aug-04 17:19
loop0722-Aug-04 17:19 
GeneralRe: closing forms Pin
mav.northwind22-Aug-04 20:15
mav.northwind22-Aug-04 20:15 
GeneralMr ListView again Pin
yetanotherchris17-Aug-04 4:43
yetanotherchris17-Aug-04 4:43 
I'm trying to get DrawItem and MeasureItem working on a ListView, with a view to drawing items in LargeIcon view. I've managed to get DrawItem working okay, using the code below. My problem is with the MeasureItem event. The event is only ever called once, and most annoyingly, are never called when the view isn't the details view.

I've tinkered about with the owner-drawn listview projects on codeproject.com, and have discovered you need to handle ITEMPREPAINT for non-details view listviews. The problem with this is, MeasureItem isn't supported (or rather, I don't know how to handle it), and so you end up having to handle or Mouse events yourself.

This removes the point of the control being derived from ListView - to utilise all the functionality a listview has in.

I also tried creating ListViewItemCollections and ListViewItems myself, but gave up with that as I was getting nowhere.

Hopefully you've read to this stage and haven't dropped off. If anyone can suggest controls, websites, FAQs etc. that'd be appreciated.


Cheers
..................................................
The code:
// Majority of this is a merger of UtilityLibrary and google groups examples
public class MyListView : ListView 
{
	private struct MEASUREITEMSTRUCT 
	{
		public int CtlType;
		public int CtlID;
		public int itemID;
		public int itemWidth;
		public int itemHeight;
		public IntPtr itemData;
	}

	[StructLayout(LayoutKind.Sequential)]
		private struct RECT
	{
		public int left;
		public int top;
		public int right;
		public int bottom;
	}

	private struct DrawItemStruct
	{
		public int ctlType;
		public int ctlID;
		public int itemID;
		public int itemAction;
		public int itemState;
		public IntPtr hWndItem;
		public IntPtr hDC;
		public RECT  rcItem;
		public IntPtr itemData;
	}

	private enum ReflectedMessages
	{
		OCM__BASE = (0x0400 + 0x1c00),
		OCM_DRAWITEM = (OCM__BASE + 0x002B),
	}

	public event System.Windows.Forms.DrawItemEventHandler DrawItem;
	public event MeasureItemEventHandler MeasureItem;
	public const int LVS_OWNERDRAWFIXED      = 0x0400;
	private DrawMode drawMode;
	
	public MyListView()
	{
		this.drawMode = DrawMode.Normal;
	}
	
	protected override CreateParams CreateParams
	{
		get
		{
			CreateParams cp = base.CreateParams;
			cp.Style |= (drawMode != DrawMode.Normal) ? LVS_OWNERDRAWFIXED : 0;
			return cp;
		}
	}
	
	public virtual DrawMode DrawMode
	{
		get { return drawMode; }
		set { drawMode = value; }
	}
	
	protected virtual void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
	{
		
	}

	public virtual void OnMeasureItem(MeasureItemEventArgs e)
	{

	}
	
	protected override void WndProc(ref System.Windows.Forms.Message m)
	{
		base.WndProc(ref m);
		
		switch (m.Msg)
		{
			case (int)ReflectedMessages.OCM_DRAWITEM:
			{
				DrawItemStruct dis =
				(DrawItemStruct)m.GetLParam(typeof(DrawItemStruct));
				
				Graphics graph = Graphics.FromHdc(dis.hDC);
				Rectangle rect = new Rectangle(dis.rcItem.left,dis.rcItem.top,dis.rcItem.right - dis.rcItem.left,dis.rcItem.bottom - dis.rcItem.top);
				int index = dis.itemID;
				DrawItemState state = DrawItemState.None;
				
				System.Windows.Forms.DrawItemEventArgs e = new System.Windows.Forms.DrawItemEventArgs(graph, Font, rect, index, state, ForeColor, BackColor);
				if ( this.DrawItem != null )
				{
					this.DrawItem(this,e);
				}
				OnDrawItem(e);
				
				graph.Dispose();
				break;
			}

			case 8236:
				this.WmReflectMeasureItem(ref m);
				break;
				
		}
	}

	private void WmReflectMeasureItem(ref Message m)
	{
		Graphics graphics1;
		MeasureItemEventArgs args1;
		MEASUREITEMSTRUCT measureitemstruct1 = (MEASUREITEMSTRUCT) m.GetLParam( typeof(MEASUREITEMSTRUCT) );
		if ((this.drawMode == DrawMode.OwnerDrawVariable) && (measureitemstruct1.itemID >= 0))
		{
			graphics1 = Graphics.FromHwnd(this.Handle);
			args1 = new MeasureItemEventArgs(graphics1, measureitemstruct1.itemID, 20);
			try
			{
				if ( this.MeasureItem != null )
				{
					this.MeasureItem(this,args1);
				}
				this.OnMeasureItem(args1);
				measureitemstruct1.itemHeight = args1.ItemHeight;
			}
			finally
			{
				graphics1.Dispose();
			}
		}

		measureitemstruct1.itemHeight = 20;//this.ItemHeight;
		Marshal.StructureToPtr(measureitemstruct1, m.LParam, false);
		m.Result = ((IntPtr) 1);
	}
}

Generalfocus on next control in a WinForm Pin
Hovik Melkomian17-Aug-04 2:33
Hovik Melkomian17-Aug-04 2:33 
GeneralRe: focus on next control in a WinForm Pin
Nick Parker17-Aug-04 3:30
protectorNick Parker17-Aug-04 3:30 
GeneralRe: focus on next control in a WinForm Pin
Hovik Melkomian17-Aug-04 6:37
Hovik Melkomian17-Aug-04 6:37 
GeneralRe: focus on next control in a WinForm Pin
Stanciu Vlad17-Aug-04 7:02
Stanciu Vlad17-Aug-04 7:02 
GeneralWeb custom control complie in vb.net Pin
patrick.hatung17-Aug-04 1:56
patrick.hatung17-Aug-04 1:56 
GeneralRe: Web custom control complie in vb.net Pin
Nick Parker17-Aug-04 3:17
protectorNick Parker17-Aug-04 3:17 
Generalembedded database Pin
khchan17-Aug-04 1:48
khchan17-Aug-04 1:48 
GeneralRe: embedded database Pin
yetanotherchris17-Aug-04 5:18
yetanotherchris17-Aug-04 5:18 
GeneralRe: embedded database Pin
Steve Maier17-Aug-04 7:31
professionalSteve Maier17-Aug-04 7:31 
GeneralRe: embedded database Pin
leppie17-Aug-04 8:21
leppie17-Aug-04 8:21 
Questionhow to change icons Pin
Stephan Wright17-Aug-04 1:15
Stephan Wright17-Aug-04 1:15 
AnswerRe: how to change icons Pin
Nick Parker17-Aug-04 3:09
protectorNick Parker17-Aug-04 3:09 
GeneralRe: how to change icons Pin
Stephan Wright17-Aug-04 3:33
Stephan Wright17-Aug-04 3:33 
GeneralRe: how to change icons Pin
Stephan Wright17-Aug-04 4:04
Stephan Wright17-Aug-04 4:04 
GeneralRe: how to change icons Pin
Stephan Wright17-Aug-04 9:02
Stephan Wright17-Aug-04 9:02 
GeneralProblem verifying certificates Pin
Escroto17-Aug-04 1:12
Escroto17-Aug-04 1:12 
GeneralActive Directory Pin
Member 130283917-Aug-04 1:05
Member 130283917-Aug-04 1:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.