C# UserControl DesignTime 觸發 MouseMove 事件

Zero000000 0 Reputation points
2023-02-14T14:57:59.98+00:00

Hi 各位先進好,小弟在學習C#上有些問題,特來請教,小弟使用 Visual C#,

建立 UserControl,新建的元件是繼承 PictureBox,小弟需要元件在 DesignTime

時期能觸發 MouseMove 事件,確發現在 DesignTime 時期該 UserControl 無法

觸發 MouseMove 事件,請問這是架構上就無法觸發該事件,還是如何呢?,有辨

法加程式碼讓它在 DesignTime 時期觸發 MouseMove嗎,如果各位先進無法明確

的告知我如何逹成,能否指點方向讓小弟自己去查呢,感激不盡

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,340 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 49,126 Reputation points Microsoft External Staff
    2023-02-15T02:55:52.0533333+00:00

    Hi @Zero000000 ,Welcome to Q&A.

    This forum only accepts English service, please modify your question and answer in English.

    Regarding the Mousemove event, you need to add it manually with code.

    Of course, you need to consider whether it will conflict with other controls.

    I wrote you an example of dragging a usercontrol (you can also change the picturbox dock property to fill)

    namespace WindowsFormsControlLibrary1
    {
        public partial class UserControl1 : UserControl
        {
            private bool isDragging = false;   // whether the control is being dragged
            private Point mouseOffset;         // The offset between the mouse position and the control position
            public UserControl1()
            {
                InitializeComponent();
                // Subscribe to mouse down and move events
                this.MouseDown += MyUserControl_MouseDown;
                this.MouseMove += MyUserControl_MouseMove;
                this.MouseUp += MyUserControl_MouseUp;
            }
            private void MyUserControl_MouseDown(object sender, MouseEventArgs e)
            {
                // Record the offset when the mouse is down and mark isDragging as true
                mouseOffset = new Point(-e.X, -e.Y);
                isDragging = true;
            }
    
            private void MyUserControl_MouseMove(object sender, MouseEventArgs e)
            {
                if (isDragging)
                {
                    // Computes the new position of the control
                    Point newLocation = this.Location;
                    newLocation.X += e.X + mouseOffset.X;
                    newLocation.Y += e.Y + mouseOffset.Y;
    
                    // mobile controls
                    this.Location = newLocation;
                    textBox1.Text = newLocation.X.ToString();
                    textBox2.Text = newLocation.Y.ToString();
                }
            }
            private void MyUserControl_MouseUp(object sender, MouseEventArgs e)
            {
                // Mark isDragging to false when the mouse is up
                isDragging = false;
            }
        }
    }
    

    This demo is the use of usercontrol in winform:

    2-15

    Best Regards, Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.