c print preview control with the mouse to move the page function code sharing

  • 2020-05-26 10:03:03
  • OfStack

This function could have been achieved by pulling the horizontal and vertical scroll bars, but in practice, users tend to drag the page directly with the mouse to achieve this, and many picture-viewing software have this kind of similar function. The.net print preview control unfortunately does not provide this 1 function, only to find their own way to achieve it.

My approach is to use the code to control the horizontal to vertical position of the scroll bar in the print preview control, indirectly and directly drag the scroll bar 1 like effect with the mouse.

The biggest difficulty in implementing this 1 feature is that the print preview control does not allow the programmer to directly call the methods or properties on the scroll bar. So we had to turn to WinAPI for help.

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

private static extern int SetScrollPos(IntPtr hwnd, int nBar, int nPos, bool bRedraw);
private static extern int GetScrollPos(IntPtr hwnd, int nBar);
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;

Brief explanation 1:

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

GetScrollPos: gets the location of the scroll button for the specified scroll bar

GetScrollRange: gets the maximum and minimum position of the scroll button for the specified scroll bar

PostMessage: this function is the key of the keys and is responsible for sending the appropriate message to the Windows control to actually perform the appropriate action. Some netizens realized the movement of the slider position in the scroll bar, but did not cause the movement of the content in the control, the reason is because it did not call this function, did not send the message of the mobile content to the control.

SB_HORZ: represents the horizontal scroll bar

SB_VERT: represents the vertical scroll bar

WM_HSCROLL: represents horizontal scrolling events

WM_VSCROLL: represents vertical scrolling events

SB_THUMBPOSITION: as for this constant, I am not very clear about its meaning either. If you know, please reply to me.

All right, we're ready to go.

Declare a few variables first:

bool Preview_move = false;// Whether the mouse is pressed or not indicates the processing of the moving state. 
Point MoveStart;// Move the mouse at the start of the coordinate point 
Point MoveEnd;// Move the mouse coordinates during the process 

In the MouseDown event of the control, start moving the page when the mouse is pressed, and note the starting point:

private void previewer_MouseDown(object sender, MouseEventArgs e)
    Preview_move = true;
    MoveStart = e.Location;

In the MouseUp event of the control, remember to return to the non-moving state when the mouse is released:

private void previewer_MouseUp(object sender, MouseEventArgs e)
    Preview_move = false;

The following is the key part to realize the mobile page. In the MouseMove of the control, the code is used to indirectly control the position of the scroll bar of the control and realize the real-time movement of the page:

private void previewer_MouseMove(object sender, MouseEventArgs e)
    if (!Preview_move) return;
    MoveEnd = e.Location;
    int MinH,MaxH,MinV,MaxV;
// Get the mouse in X and Y The amount of movement in both directions. Divided by the 10 Is to slow down the movement of the page 1 Points. The negative sign in front is used to adjust the direction of the page movement. 
    int MoveX = -(MoveEnd.X - MoveStart.X)/10; 
    int MoveY = -(MoveEnd.Y - MoveStart.Y)/10; 
// Gets 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 does not exceed the maximum or minimum value) 
    int PosH1 = PosH + MoveX;
    if (PosH1 >= MinH && PosH1 <= MaxH)
  SetScrollPos(previewer.Handle, SB_HORZ, PosH1, true);// Sets the position of the scroll bar 
  PostMessage(previewer.Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * PosH1, 0);// Tells the control to move the page content to the appropriate location 
    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 that can move the page content with the mouse in real time. In fact, many of the controls in.net can be controlled using similar aspects

Related articles: