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.