The ability to move pages with the mouse in C #

Source: Internet
Author: User
Tags bool

The following features need to be implemented in your project:

In the print preview control, you can drag the page with the mouse to see what is outside of the display range.

This function can be pulled by pulling horizontal and vertical scroll bar to achieve, but in practice, users tend to drag the page directly with the mouse to achieve, many look at graphic software have such a similar function. The. NET Print Preview Control unfortunately does not provide this function, only to find ways to achieve it.

Oh, but there are always ways.

My approach is to use code to control the horizontal in the print preview control to the vertical scroll bar position, indirect implementation and with the mouse to drag the scroll bar the same effect.

The biggest difficulty in implementing this feature is that the print preview control does not have a method or property about the scroll bar that the programmer calls directly. So I had to ask WINAPI for help.

The following API functions and constants are key to achieving these 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;

Simply explain:

SetScrollPos: Sets the position of the scroll button in the specified scroll bar

GetScrollPos: Gets the position of the scroll button for the specified scroll bar

Getscrollrange: Gets the maximum minimum position of the scroll button for the specified scroll bar

PostMessage: This function is critical in that it is responsible for sending the corresponding message to the Windows control to actually perform the appropriate action. Some netizens realize the movement of the slider in the scroll bar, but it does not cause the contents of the control to move, the reason is because the function is not called, do not send messages to the control of the content.

Sb_horz: Represents the horizontal scroll bar

Sb_vert: Represents a vertical scroll bar

Wm_hscroll: Representing horizontal scrolling events

Wm_vscroll: Represents a vertical scrolling event

Sb_thumbposition: As for this constant, the meaning I am not very clear, there are friends who know welcome to reply to me.

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.