Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With MouseMiddleButton, Scroll or Pan #1

Open
BlokyMose opened this issue Feb 25, 2021 · 0 comments
Open

With MouseMiddleButton, Scroll or Pan #1

BlokyMose opened this issue Feb 25, 2021 · 0 comments

Comments

@BlokyMose
Copy link

I intended to add a Pan feature, or an Auto-Scroll to the program.

The idea is pretty simple, if the user hold the mouse's middle button, the screen can automatically be scrolled vertically and horizontally according to the position of the cursor when start holding, and the current position of the mouse.

I failed terribly in implementing my code for hours of trying. If there's anybody reading this, could you please add this feature or maybe teach me how to implement it?

(I try to add some codes into Canvas.cs)

`

	#region Hover Scroll: 

	bool isHoverScrollMode = false;
	Point hoverScrollOriginLocation;
	Timer hoverScrollTimer = new Timer();
	int hoverScrollTimerInterval = 5;
	int hoverScrollSpeed = 1;

	private void Canvas_MouseDown(object sender, MouseEventArgs e)
    {
        if (HasDocument)
			if (e.Button == MouseButtons.Middle)
				SetActiveHoverScroll(e, true);
    }

	private void Canvas_MouseUp(object sender, MouseEventArgs e)
	{
        if (HasDocument)
			if (e.Button == MouseButtons.Middle)
				SetActiveHoverScroll(e, false);
	}

	private void SetActiveHoverScroll(MouseEventArgs e, bool isActive)
    {
		isHoverScrollMode  = isActive;

		if (isHoverScrollMode)
        {
			hoverScrollTimer.Start();
			hoverScrollOriginLocation = e.Location;
			hoverScrollTimer.Interval = hoverScrollTimerInterval;
            hoverScrollTimer.Tick += (sender, eventArgs) => { ScrollWithHoverScroll(sender, e); } ;
		}
        else
        {
			hoverScrollTimer.Stop();
		}
    }

    private void ScrollWithHoverScroll(object sender, MouseEventArgs e)
    {
        if (e.Location.X > hoverScrollOriginLocation.X)
        {
			ScrollHorizontally(-hoverScrollSpeed);
		}
        else if (e.Location.X < hoverScrollOriginLocation.X)
        {
			ScrollHorizontally(hoverScrollSpeed);
		}

		if (e.Location.Y > hoverScrollOriginLocation.Y)
		{
			ScrollEventArgs s = new ScrollEventArgs(ScrollEventType.SmallDecrement,hoverScrollSpeed, ScrollOrientation.VerticalScroll );
			base.OnScroll(s);
		}
		else if (e.Location.Y < hoverScrollOriginLocation.Y)
		{
			ScrollEventArgs s = new ScrollEventArgs(ScrollEventType.SmallIncrement, hoverScrollSpeed, ScrollOrientation.VerticalScroll);
			base.OnScroll(s);
		}

		//document_NeedsRedraw(sender, e);
		UpdateDocumentOffset();
	}

    #endregion

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant