2

私は単純なドット グリッドを描画するアプリケーションに取り組んでいます。マウスをグリッド上のポイント間でスナップさせ、最終的にグリッド上に線を引きたいと思います。

現在のマウスの位置 (X、Y) を取得し、最も近いグリッド座標を計算するメソッドがあります。

イベントを作成し、マウスを新しい座標に移動しようとすると、システム全体がぎくしゃくします。マウスがグリッド ポイント間でスムーズにスナップしません。

私がやろうとしていることを説明するために、以下のコード サンプルをコピーしました。マウスの動きの中のびくびく感をなくす方法についてアドバイスをくれる人はいますか?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GridTest
{
    public partial class Form1 : Form
    {
        Graphics g;
        const int gridsize = 20;

        public Form1()
        {
            InitializeComponent();
            g = splitContainer1.Panel2.CreateGraphics();
            splitContainer1.Panel2.Invalidate();
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
            Drawgrid();
        }

        private void Drawgrid()
        {
            for (int x = 0; x < splitContainer1.Panel2.ClientSize.Width; x += gridsize)
            {
                for (int y = 0; y < splitContainer1.Panel2.ClientSize.Height; y += gridsize)
                { g.DrawLine(Pens.Black, new Point(x, y), new Point(x + 1, y)); }
            }
        }

        private void splitContainer1_Panel2_MouseMove(object sender, MouseEventArgs e)
        {
            Point newPosition = new Point();
            newPosition = RoundToNearest(gridsize, e.Location);
            Cursor.Position = splitContainer1.Panel2.PointToScreen(newPosition);
        }

        private Point RoundToNearest(int nearestRoundValue, Point currentPoint)
        {
            Point newPoint = new Point();
            int lastDigit;

            lastDigit = currentPoint.X % nearestRoundValue;

            if (lastDigit >= (nearestRoundValue/2))
            { newPoint.X = currentPoint.X - lastDigit + nearestRoundValue; }
            else
            { newPoint.X = currentPoint.X - lastDigit; }

            lastDigit = currentPoint.Y % nearestRoundValue;
            if (lastDigit >= (nearestRoundValue / 2))
            { newPoint.Y = currentPoint.Y - lastDigit + nearestRoundValue; }
            else
            { newPoint.Y = currentPoint.Y - lastDigit; }

            return newPoint;
        }
    }
}
4

4 に答える 4

6

カーソル位置を変更しないでください。その必要はありません。

代わりに、グリッドにスナップされているかのように描画します。ユーザーがどこかをクリックしたら、最も近いグリッド ポイントから線を引くだけです。

たとえば、ユーザーが (197,198) をクリックしても、最も近い点が実際には (200,200) であることがわかっている場合、(197,198) の代わりに (200,200) に線を引くだけです。

そして、実際のカーソル位置をいじらないでください。


マウスカーソルを非表示にする方法があるかどうかはわかりません。ある場合は、実際の位置を変更せずに、非表示にして自分で描画できます。

于 2008-12-11T14:53:48.790 に答える
2

マウスを移動しようとすると、マウスは同じポイントにスナップし続けます-そのポイントにまだ最も近いため...マウスを左に移動すると、カーソルを再計算する代わりに現在のポイントの左に移動します現在。他の3方向に申し込む...

ただし、この動作はお勧めしません。多くの刺激が発生します。マウスではなく、コントロールをグリッドにスナップします。

于 2008-12-11T13:00:23.217 に答える
1

私はあなたがどこから来ているのか理解していると思います。新しいポイントにスナップする前に、元のスナップポイント(マウスの左クリック)から少し離れている必要があります。

これが私が何を意味するかを示す50行のコードです:(新しいVB.NETプロジェクトを開始し、新しいモジュールを追加し、コードをコピーして貼り付け、参照を追加し、System、System.drawing、およびSystem.Windows.Formsに)


Imports System
Imports System.Drawing
Imports System.Windows.Forms

Module modSnap

    Public Const strApplicationTitle As String = "Snap Demo"
    Public frmSnap As SnapForm
    Public ptSnap, ptStart, ptEnd As Point

    Public Class SnapForm
        Inherits Form
        Public Sub New()
            Me.Text = "Snap Demo"
            Me.ClientSize = New Size(800, 600)
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
            Me.MaximizeBox = False
            Me.StartPosition = FormStartPosition.CenterScreen
            Me.DoubleBuffered = True
        End Sub
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            e.Graphics.Clear(Color.Black)
            For row As Integer = 20 To 780 Step 20
                For col As Integer = 20 To 580 Step 20
                    e.Graphics.DrawEllipse(Pens.Blue, New Rectangle(row - 2, col - 2, 4, 4))
                Next
            Next
            e.Graphics.DrawLine(Pens.Red, ptStart, ptEnd)
        End Sub
        Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
            MyBase.OnMouseDown(e)
            Dim x As Integer = CInt(e.X / 20) * 20
            Dim y As Integer = CInt(e.Y / 20) * 20
            ptStart = New Point(x, y)
            ptSnap = New Point(x, y)
            Windows.Forms.Cursor.Position = Me.PointToScreen(ptSnap)
        End Sub
        Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
            MyBase.OnMouseMove(e)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim x As Integer = CInt(e.X / 20) * 20
                Dim y As Integer = CInt(e.Y / 20) * 20
                ' must be some delta away from original snap point
                If (x < ptSnap.X - 15 Or x > ptSnap.X + 15) Or (y < ptSnap.Y - 15 Or y > ptSnap.Y + 15) Then
                    ptSnap = New Point(x, y)
                    ptEnd = New Point(x, y)
                    Me.Invalidate(False)
                    Windows.Forms.Cursor.Position = Me.PointToScreen(ptSnap)
                End If
            End If
        End Sub
    End Class

    Public Sub main()
        Try
            frmSnap = New SnapForm
            Application.Run(frmSnap)
        Catch ex As Exception
            MessageBox.Show(ex.Message, strApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            frmSnap.Dispose()
        End Try
    End Sub

End Module

于 2008-12-11T16:17:08.157 に答える
0

私はruijoelに同意します。カーソルの位置をいじらないでください。クリック イベントでどのポイントにスナップされるかをユーザーに示すために、スナップ ポイントに十字またはリングを描画することをお勧めします。

これをうまく機能させるには、xor-drawing を調べて、新しいスナップ ポイントに移動するとアイテムが消去されるようにすることをお勧めします。

于 2008-12-11T15:09:56.247 に答える