📄 scroll_drag_drop.shtml
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<META NAME="GENERATOR" CONTENT="Mozilla/4.0 [en] (WinNT; I) [Netscape]">
<TITLE>Scrolling during drag and drop</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td align=center><!--#exec cgi="/cgi/ads.cgi"--><td>
</tr>
</table>
<CENTER>
<H3>
<FONT COLOR="#AOAO99">Scrolling during drag and drop</FONT></H3></CENTER>
<CENTER>
<H3>
<HR></H3></CENTER>
When dragging an item it is quite possible that the drop target is not
visible. Sometimes the user can collapse some items so that both the item
to be dragged and the item that is to be the drop target are both visible.
However, the user doesn抰 always have this recourse. Besides, most professional
apps provide some form of scrolling while the item is being dragged.
<P>In the implementation discussed below, we use a timer to enable us to
drag the outline. We also scroll at varying speed depending on how far
the cursor is from the border of the control.
<H4>
Step 1: Add member variables to class</H4>
We declare two variables. One to hold the timer id and the other to hold
a counter for the timer ticks. You may also choose to declare two more
variable - for the event id that will be generated by the timer and the
timeout value used for the timer. I have used hard coded values of 1 and
75 respectively.
<PRE><TT><FONT COLOR="#990000">protected:
UINT m_nTimerID;
UINT m_timerticks;</FONT></TT></PRE>
<H4>
Step 2: Set timer in TVN_BEGINDRAG handler</H4>
We install the timer in the TVN_BEGINDRAG handler. Place the following
statement at the end of you handler function ( I have used OnBeginDrag()
) in previous sections.
<BR>
<PRE><TT><FONT COLOR="#990000"> // Set up the timer
m_nTimerID = SetTimer(1, 75, NULL);</FONT></TT></PRE>
<H4>
Step 3: Add a WM_TIMER handler</H4>
You can use the class wizard to add the WM_TIMER handler. By default this
function is given the name OnTimer(). We first make sure that we are handling
the right timer event. The next task is to update the drag image position.
<P>We then determine whether scrolling is required. The criteria we use
for this is that the cursor should be close to the top edge of the control
or above the control. On the other end, if the cursor is near the bottom
edge of the control or below the control, then we scroll in the other direction.
We then compute a number to control the speed of scrolling. We have used
a hard coded value of six different speed which should be enough. Each
scroll speed range is 20 pixels tall. I抣l leave the rest of the logic
to control the scroll speed as an exercise for the reader - you.
<P>You may notice that the call to DragMove() uses screen coordinates.
The DragMove() function needs the screen coordinates if the call to DragEnter()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -