Skip to content

DPI scaling support for .NET Framework|Core WinForms apps #563

@Ichibot200

Description

@Ichibot200

Plot draggables are not DPI aware:
I have had a little problem with draggable lines on my 4K screen, with 144 DPI.
The cursor handle was offset to the left from the vertical draggable line exactly 1.5 times - the ratio of 144 and 96 DPI.

In terms of a fix:
There was a little start in the FormsPlot.cs, but was not enough to support draggables on higher DPI screens.

So I dug in to the code and here is my quick fix for this situation:

IDraggable plottableBeingDragged = null;
private bool isMovingDraggable { get { return (plottableBeingDragged != null); } }
private double dpiScale = Settings.DPIScale; //Heres hoping that WinForms becomes DPI aware

---

private void PbPlot_MouseDown(object sender, MouseEventArgs e)
        {
            var mousePixel = e.Location;
            plottableBeingDragged = plt.GetDraggableUnderMouse(mousePixel.X / dpiScale, mousePixel.Y / dpiScale);

---

public PointF mouseCoordinates { get { return plt.CoordinateFromPixel(mouseLocation); } }
        Point mouseLocation;

        private void PbPlot_MouseMove(object sender, MouseEventArgs e)
        {

---

private void MouseMovedToMoveDraggable(MouseEventArgs e)
        {
            plottableBeingDragged.DragTo(
                plt.CoordinateFromPixelX(e.Location.X / dpiScale),
                plt.CoordinateFromPixelY(e.Location.Y / dpiScale),
                isShiftPressed, isAltPressed, isCtrlPressed);
            OnMouseDragPlottable(EventArgs.Empty);
            Render(true, lowQuality: lowQualityWhileDragging);
        }

---

private void MouseMovedWithoutInteraction(MouseEventArgs e)
        {
            if (showCoordinatesTooltip)
            {
                double coordX = plt.CoordinateFromPixelX(e.Location.X);
                double coordY = plt.CoordinateFromPixelY(e.Location.Y);
                tooltip.Show($"{coordX:N2}, {coordY:N2}", this, e.Location.X + 15, e.Location.Y);
            }

            // set the cursor based on what's beneath it
            var draggableUnderCursor = plt.GetDraggableUnderMouse(e.Location.X/dpiScale, e.Location.Y/dpiScale);
            var spCursor = (draggableUnderCursor is null) ? Config.Cursor.Arrow : draggableUnderCursor.DragCursor;
            pbPlot.Cursor = GetCursor(spCursor);
        }

PS! Talking already about DPI awareness... There is also an issue when I try to drag the WinForms window from one screen to another (from 96 to 144 and visa-versa), the plot does not rescale to fit the bounds of the parent container. The resizing works once I maximize the window to full screen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions