By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,819 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,819 IT Pros & Developers. It's quick & easy.

What listview event fired when mouse over column header

P: 17
I notice that when the mouse is over a column header of a listview control, that column header is focused (or highlighted). In this case, what event is fired ?

The reason I ask is I have listview control with 14 columns,
I also have labels that I want to drag-n-drop the labels over the listview column headers. When that occurs, I will change the column header text to the label text.

I have all the codes done for the drag-n-drop, but when I drop the label on the listview column header, I struggle to find out which column is being "dropped", and what listview event is kicked off.

Thanks,
May 20 '10 #1
Share this Question
Share on Google+
9 Replies


GaryTexmo
Expert 100+
P: 1,501
I know this doesn't help, but I have no idea!!

I tried creating a new class inheriting from ListView and overriding WndProc for a WM_MOUSEMOVE message but it doesn't go when you're over the header. Very strange! It's like the header is a completely separate control somehow within the ListView control.

Now that you've brought this up, I'm quite interested to know (things that make no sense bug me, haha). I tried googling around but didn't get much on it... perhaps you should hit google too. Maybe someone else will have some ideas also.

*Edit: A thought occurred to me... I think there's some code on CodeProject that will let you see when a mouse event happens globally (not just on a form). It seems terribly inefficient, but you might be able to do something with that to see if the cursor is within the bounds of the ListView's ClientRectangle (or whatever property it is) and the control is active. That seems rather hackish, but honestly I don't have any better ideas.

I feel like there's gotta be a better way :D
May 20 '10 #2

GaryTexmo
Expert 100+
P: 1,501
(See edit below)

Muahaha, I think I got it! :D [Ed: No, I didn't. BAH!]

You need to trap on the WM_NCMOUSEMOVE message. I guess the NC means non-client, and it goes when the mouse is on the non-client area of a form. Here's the MSDN page...

http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

Here are all the windows messages...
http://www.autohotkey.com/docs/misc/SendMessageList.htm

It looks like this doesn't happen on every mouse move. It might be when you enter this section, I'm honestly not sure, but it's a start and I'll let you take it from here.

Here's some sample code to get you started...

Expand|Select|Wrap|Line Numbers
  1.     public class TestLV : ListView
  2.     {
  3.         private const int WM_MOUSEMOVE = 0x200;
  4.         private const int WM_NCMOUSEMOVE = 0xA0;
  5.  
  6.         protected override void WndProc(ref Message m)
  7.         {
  8.             base.WndProc(ref m);
  9.  
  10.             switch (m.Msg)
  11.             {
  12.                 case WM_MOUSEMOVE:
  13.                     Console.WriteLine("Mouse moving (WM_MOUSEMOVE)...");
  14.                     break;
  15.                 case WM_NCMOUSEMOVE:
  16.                     Console.WriteLine("Mouse moving (WM_NCMOUSEMOVE)...");
  17.                     break;
  18.             }
  19.         }
  20.     }
EDIT: I thought I had but I did not :( I only thought I was getting that message but I was just hitting it when mousing over the like, pixel of empty space between the control and the column header *sigh*. It looks like the only message that even comes while mousing over the column header is 4E, which is WM_NOTIFY. I'm not sure why... stumped again. Sorry!
May 20 '10 #3

Plater
Expert 5K+
P: 7,872
Its called "HotTracking" if that helps
May 20 '10 #4

P: 17
Plater:

Can you elaborate a bit more? I am newbie.

Thanks
May 20 '10 #5

GaryTexmo
Expert 100+
P: 1,501
@Plater
I think HotTracking is something else... here's the doc

//
// Summary:
// Gets or sets a value indicating whether the text of an item or subitem has
// the appearance of a hyperlink when the mouse pointer passes over it.
//
// Returns:
// true if the item text has the appearance of a hyperlink when the mouse passes
// over it; otherwise, false. The default is false.
[DefaultValue(false)]
public bool HotTracking { get; set; }
Unless you mean this can be used to see when the mouse moves over a column header... if so, how?!

(This has me rightly confused. I showed a few colleagues here at work just 'cause it was so messed up and they were as confounded as I was :P)

nghivo, you might even want to consider going to microsoft's support site and bringing this up.
May 20 '10 #6

Plater
Expert 5K+
P: 7,872
I'm not sure if it helps or not.
But the concept of having a visual reaction on mouseover in things like TabControls, TreeViews, etc is called HotTracking (thus the property). And if you turn it off, no visual changes will occur.
May 20 '10 #7

P: 17
Folks:

Thanks for your help,
I will contact Microsoft for help and research some more.
If I find the solution, I will post back here including some code.

In the meantime, if you have any new idea, please do throw in.
May 21 '10 #8

P: 17
The only way to figure out what column header is being dropped is:

1. Find the area occupied by the header
Expand|Select|Wrap|Line Numbers
  1.         [DllImport("user32.dll")]
  2.         extern static IntPtr SendMessage(IntPtr hwnd, int message, int wParam, int lParam);
  3.         [DllImport("user32.dll")]
  4.         extern static bool GetWindowRect(IntPtr hwnd, out RECT rect);
  5.  
  6.         const int LVM_GETHEADER = 4127;
  7.  
  8.         struct RECT
  9.         {
  10.             public int left, top, right, bottom;
  11.         }        
  12.             IntPtr hdrHandle = SendMessage(this.flContentLstView.Handle, LVM_GETHEADER, 0, 0);
  13.             RECT rc;
  14.             GetWindowRect(hdrHandle, out rc);
  15.  
2. Know the mouse position and the area occupied by the header, find the column that the mouse is "over". xPos is the mouse x position, yPos is the mouse y postion.

Expand|Select|Wrap|Line Numbers
  1.             if (xPos >= rc.left && xPos <= rc.right && yPos >= rc.top && yPos <= rc.bottom)
  2.             {
  3.                 int x = rc.left;
  4.                 foreach (ColumnHeader column in this.listview.Columns)
  5.                 {
  6.                     x += column.Width;
  7.                     if (xPos < x) return column;                    
  8.                 }
  9.             }
  10.        }
  11.  
May 28 '10 #9

GaryTexmo
Expert 100+
P: 1,501
How did you get the mouse position over the header? The mouse move events I tied into would only get the mouse movement over the control body itself, not the headers. I figured the solution would be something like this but I couldn't figure out how to get the mouse position in that area.

A couple of other things...

1. The code tags use square brackets, can you please replace your angle brackets with square brackets to fix your post?

2. When I was trying to help you out with this I found very little information on google. I think this would make a great article for the insights section. Would you be willing to write up a quick little article on this with some working code that people could use to figure this out?
May 28 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.