This function could have been achieved by pulling horizontal and vertical scroll bars, but in actual use, users tend to directly drag the page with the mouse, and many picture-viewing software have this similar function. Unfortunately, the .net print preview control does not provide this function, so I just think of a way to implement it myself.
My method is to use code to control the position of the horizontal and vertical scroll bars in the print preview control, indirectly implementing the same effect as directly dragging the scroll bar with the mouse.
The biggest difficulty in implementing this function is that the print preview control does not allow the programmer to call the methods or properties about the scrollbar directly. So I had to ask WinAPI for help.
The following API functions and constants are the key to implementing the above functions:
[DllImport("")]
private static extern int SetScrollPos(IntPtr hwnd, int nBar, int nPos, bool bRedraw);
[DllImport("")]
private static extern int GetScrollPos(IntPtr hwnd, int nBar);
[DllImport("")]
private static extern bool PostMessage(IntPtr hWnd, int nBar, int wParam, int lParam);
[DllImport("user32", CharSet = )]
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;
Let me give a brief explanation:
SetScrollPos: Set the position of the scroll button in the specified scroll bar
GetScrollPos: Get the position of the scroll button for the specified scrollbar
GetScrollRange: Get the maximum and minimum position value of the scroll button of the specified scroll bar
PostMessage: This function is the key of the key, and it is responsible for sending corresponding messages to Windows controls to truly perform corresponding operations. Some netizens have moved the slider position in the scroll bar, but did not cause the content in the control to move. The reason is that this function is not called and the message of moving the content is not sent to the control.
SB_HORZ: represents horizontal scroll bar
SB_VERT: represents vertical scroll bar
WM_HSCROLL: represents horizontal scrolling event
WM_VSCROLL: represents vertical scrolling event
SB_THUMBPOSITION: As for this constant, I am not very clear about its meaning. Friends who know it are welcome to reply to me.
OK, the preparations are done and the work can be started.
Declare a few variables first:
bool Preview_move = false;//Whether to press the mouse, it indicates the processing movement status.
Point MoveStart;//The coordinate point of the mouse when the movement starts
Point MoveEnd;//The coordinate points of the mouse during movement
In the MouseDown event of the control, start moving the page when the mouse is pressed and note the starting coordinate point:
private void previewer_MouseDown(object sender, MouseEventArgs e)
{
Preview_move = true;
MoveStart = ;
}
In the MouseUp event of the control, remember to reset the mouse to the non-mobile state after it is released:
private void previewer_MouseUp(object sender, MouseEventArgs e)
{
Preview_move = false;
The following is the key part of implementing the mobile page. In the MouseMove control of the control, the scroll bar position of the control is indirectly controlled by code and real-time page movement:
private void previewer_MouseMove(object sender, MouseEventArgs e)
{
if (!Preview_move) return;
MoveEnd = ;
int MinH,MaxH,MinV,MaxV;
//Get the amount of movement of the mouse in both X and Y directions. Dividing by 10 is to slow down the page. The negative sign in front is used to adjust the movement direction of the page.
int MoveX = -( - )/10;
int MoveY = -( - )/10;
//Get the maximum and minimum position of the scroll bar and the current position
GetScrollRange(, 0, out MinH, out MaxH);
GetScrollRange(, 1, out MinV, out MaxV);
int PosH = GetScrollPos(, 0);
int PosV = GetScrollPos(, 1);
//Calculate the position of the final scroll bar (note that the final position does not exceed the range of maximum and minimum values)
int PosH1 = PosH + MoveX;
if (PosH1 >= MinH && PosH1 <= MaxH)
{
SetScrollPos(, SB_HORZ, PosH1, true);//Set the position of the scrollbar
PostMessage(, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * PosH1, 0);//Tell the control to move the page content to the corresponding position
}
int PosV1 = PosV + MoveY;
if (PosV1 >= MinV && PosV1 <= MaxV)
{
SetScrollPos(, SB_VERT, PosV1, true);
PostMessage(, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * PosV1, 0);
}
}
OK, a print preview function that can use the mouse to move page content in real time is ready. In fact, many controls in .net can use similar aspects to control the scrollbar.