Listview DragDrop

T

Thomas

Hi,
This may not be the right place, but it seems like a good starting point so
here goes:

I am trying to scroll the listview during a drag drop operation. I have the
drag drop part working, but the scrolling is trouble. How can I set the
scroll to follow my mouse position while dragging. I have tried to send
messages to the listview but this method is crashing. Can I use a
sendmessage like:

SendMessage((HWND)Handle.ToPointer(), LVM_SETITEMPOSITION32,
selection->Index, (LPARAM)&targetPoint);

Do I need to marshal the hwnd or lparam? Is there any other way to do this?

Here is a snippet of my code to illustrate what I am trying to do:

private: System::Void listView1_DragOver(Object* /*sender*/, DragEventArgs*
e)
{
// Retrieve the client coordinates of the mouse position.
Point targetPoint = listView1->PointToClient(Point(e->X, e->Y));
ListViewItem* selection = listView1->GetItemAt(e->X, e->Y);
SendMessage((HWND)listView1->Handle.ToPointer(), LVM_SETITEMPOSITION32,
selection->Index, (LPARAM)&targetPoint);
}
 
S

Sheng Jiang[MVP]

I think you need a timer and check your mouse position there. Scroll the
window if mouse position is close to the border. To scroll a listview, send
LVM_SCROLL message to the window.
 
T

Thomas

I have tried to send a message to the listview but I crash before the app
even starts:


private: System::Void listView1_DragOver(Object* /*sender*/, DragEventArgs*
e)
{
Point p = listView1->PointToClient(Point(e->X, e->Y));
SendMessage(static_cast<HWND>(listView1->Handle.ToPointer()), LVM_SCROLL,
p.X, p.Y);
}

I think this would be sending a message to its' self. Does anybody know how
to do this??
 
S

Sheng Jiang[MVP]

use HandleRef to pass window handles
If your window is not going to be recreated during the message call, you can
use Handle.ToInt32
 
T

Thomas

This is simply not working and it must be a fixable thing:

Language vc++ 2003 .net framework 2.0

I have a listview (report) that contains items that can be reordered by the
user. The use should be able to click on an item and drag it to a new
position. But, the listview should scroll when the user tries to drag the
item beyond the visible portion of the list. Using SendMesage crashes before
the app even is visible. How can I scroll while dragging?
 
S

Sheng Jiang[MVP]

You can use platform invoke to declare a managed entry point of SendMessage,
but I don't think it is necessary. I haved suggested some method to conver
your control handle to HWND, do you have any problem with them?
 
T

Thomas

Sheng,
How do I use HandleRef?

Here is my code but it still crashes:

private: System::Void listView1_DragOver(Object* /*sender*/, DragEventArgs*
e)
{
// Retrieve the client coordinates of the mouse position.
Point targetPoint = listView1->PointToClient(Point(e->X, e->Y));
ListViewItem* dropItem = listView1->GetItemAt(p.X, p.Y);
HandleRef hr = HandleRef(listView1, listView1->Handle);
SendMessage((HWND)hr.Handle.ToPointer(), LVM_SETITEMPOSITION32,
dropItem->Index, (LPARAM)&targetPoint);
}

Perhaps I am not using it correctly, this is my first try using "HandleRef".
But it also seems like there is a bigger problem here: I am trying to send a
message to the listview from an event handler of the listview.

Suggestions welcome. Thanks for you time.
 
S

Sheng Jiang[MVP]

HandleRef^ handleRef=
gcnew HandleRef(this->listView1,this->listView1->Handle);
::SendMessage(
(HWND)HandleRef::ToIntPtr(*handleRef).ToInt64(),
LVM_SCROLL
,10
,10);

Here handleRef is used to hold a handle to the control that is passed to
unmanaged code.
HandleRef::ToIntPtr is used to convert HandleRef to a interger
ToInt64 should be fine for now, who knows when we need to switch to 128 bit.
If you are using a 32 bit platform sdk, you can use ToInt32.
 

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