C # How to move a page with the mouse

Source: Internet
Author: User

The following functions must be implemented in the project:

In the print preview control, you can drag the page to view content beyond the display range.

This function can be implemented by pulling the horizontal and vertical scroll bars. However, in actual use, users tend to directly use the mouse to drag the page, many image-based software have similar functions. However, the. NET print preview control does not provide this function, so you only need to find a way to implement it.

Yes, but there are always some solutions.

My solution is to useCodeTo control the horizontal and vertical scroll bar position in the print preview control, and indirectly achieve the same effect as dragging the scroll bar with the mouse.

In the process of implementing this function, the biggest difficulty is that the print preview control does not allowProgramThe method or attribute about the scroll bar that the member calls directly. So I had to ask winapi for help.

The following API functions and constants are the key to implementing the above functions:

[Dllimport ("user32.dll")]
Private Static extern int setscrollpos (intptr hwnd, int nbar, int NPOs, bool bredraw );
[Dllimport ("user32.dll")]
Private Static extern int getscrollpos (intptr hwnd, int nbar );
[Dllimport ("user32.dll")]
Private Static extern bool postmessage (intptr hwnd, int nbar, int wparam, int lparam );
[Dllimport ("USER32", charset = charset. Auto)]
Private Static extern bool getscrollrange (intptr hwnd, int nbar, out int lpminpos, out int lpmaxpos );

Private const int sb_horz = 0x0;
Private const int sb_vert = 0x1;
Private const int wm_hscroll = 0x114;
Private const int wm_vscroll = 0x115;
Private const int sb_thumbposition = 4;

Briefly describe:

Setscrollpos: Set the position of the scroll button in the specified scroll bar

Getscrollpos: gets the position of the scroll button of the specified scroll bar.

Getscrollrange: obtains the maximum and minimum positions of the scroll button of the specified scroll bar.

Postmessage: this function is the key. It is responsible for sending the corresponding message to the Windows Control to perform the corresponding operations. Some netizens implemented the slider position movement in the scroll bar, but did not cause the movement of the content in the control. The reason is that they did not call this function and did not send the message of the moving content to the control.

Sb_horz: horizontal scroll bar

Sb_vert: vertical scroll bar

Wm_hscroll: indicates a Horizontal Rolling event.

Wm_vscroll: vertical rolling event

Sb_thumbposition: I am not very clear about the meaning of this constant. If you know it, please reply to me.

Now that the preparation is complete, you can start the project.

Declare several variables first:
Bool preview_move = false; // whether to press the mouse to indicate processing the movement status.
Point movestart; // The coordinate point of the mouse when the cursor starts to move.
Point moveend; // The coordinate point of the mouse during movement

In the control's mousedown event, move the page as you press the mouse and write down the starting coordinate point:
Private void previewer_mousedown (Object sender, mouseeventargs E)
{
Preview_move = true;
Movestart = E. location;
}

In the control's mouseup event, remember to move the mouse back to a non-moving state after it is opened:

Private void previewer_mouseup (Object sender, mouseeventargs E)
{
Preview_move = false;
}

The following is the key part of page movement. In the control's mousemove implementation, Code indirectly controls the widget's scroll bar position and achieves real-time page movement:

Private void previewer_mousemove (Object sender, mouseeventargs E)
{
If (! Preview_move) return;
Moveend = E. location;
Int Minh, maxh, MINV, maxv;
// Obtain the movement of the mouse in the X and Y directions. Dividing by 10 slows down the page movement speed. The front minus sign is used to adjust the page movement direction.
Int movex =-(moveend. X-movestart. X)/10;
Int Movey =-(moveend. Y-movestart. Y)/10;
// Obtain the maximum, minimum, and current position of the scroll bar.
Getscrollrange (previewer. Handle, 0, out Minh, out maxh );
Getscrollrange (previewer. Handle, 1, out MINV, out maxv );
Int posh = getscrollpos (previewer. Handle, 0 );
Int posv = getscrollpos (previewer. Handle, 1 );
// Calculate the position of the final scroll bar (note that the final position should not exceed the maximum and minimum values)
Int posh1 = posh + movex;
If (posh1> = Minh & posh1 <= maxh)
{
Setscrollpos (previewer. Handle, sb_horz, posh1, true); // you can specify the position of a scroll bar.
Postmessage (previewer. Handle, wm_hscroll, sb_thumbposition + 0x10000 * posh1, 0); // tells the control to move the page content to the corresponding position.
}

Int posv1 = posv + Movey;
If (posv1> = MINV & posv1 <= maxv)
{
Setscrollpos (previewer. Handle, sb_vert, posv1, true );
Postmessage (previewer. Handle, wm_vscroll, sb_thumbposition + 0x10000 * posv1, 0 );
}
}

OK, a print preview function that allows you to move the page content with the mouse in real time. In fact, many controls in. Net can use similar aspects to control the scroll bar.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.