Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Mouse border in C#

Hii,
 How do i set a border to upear near the mouse pointer on my form, when i move the mouse pointer, the border move with it


thank you
0
madmare
Asked:
madmare
  • 3
  • 2
1 Solution
 
existenz2Commented:
The following code is an example. This code makes use of the MouseMoveEvent which only works when the mouse is placed upon the form. This can go wrong sometimes. You also can make it work outside the form, by creating a thread or timer which checks every few (mili)seconds if the position has changed and the refreshes the window.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MouseTest
{
      public class MainForm : System.Windows.Forms.Form
      {
            public MainForm()
            {
                  InitializeComponent();
            }
            
            [STAThread]
            public static void Main(string[] args)
            {
                  Application.Run(new MainForm());
            }
            
            private void InitializeComponent() {
                  //
                  // MainForm
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(320, 285);
                  this.Name = "MainForm";
                  this.Text = "MainForm";
                  this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainFormMouseMove);
            }
            #endregion
            void MainFormMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  this.Width = Cursor.Position.X;
                  this.Height = Cursor.Position.Y;
            }
            
      }
}
0
 
madmareAuthor Commented:
i wanted to move a Rectangel when the mouse pointer is moveed, the Rectangle i wanted to be an aqua color thats mean it has a border but filled with NO color
0
 
existenz2Commented:
Oh sorry, then I misunderstood your question. In that case this is probally what you want:

I added 2 extra mouseevents so you can stop it from resizing and restart it resizing on mousemovement.
You can use the following code to fill the rect instead of just drawing a border:
SolidBrush brush = new SolidBrush (Color.Aqua);
g.DrawRectangle(brush ,0,0,x,y);
instead of:
Pen pen = new Pen(Color.Aqua);
g.DrawRectangle(pen,0,0,x,y);



using System;
using System.Drawing;
using System.Windows.Forms;

namespace MouseTest
{
      public class MainForm : System.Windows.Forms.Form
      {
            private bool moveRect = true;
            private int x,y = 20;

            public MainForm()
            {
                  InitializeComponent();
            }
         
            [STAThread]
            public static void Main(string[] args)
            {
                  Application.Run(new MainForm());
            }
         
            private void InitializeComponent()
            {
                  //
                  // MainForm
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(832, 637);
                  this.Name = "MainForm";
                  this.Text = "MainForm";
                  this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseDown);
                  this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseUp);
                  this.Paint += new System.Windows.Forms.PaintEventHandler(this.Painting);
                  this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainFormMouseMove);

            }

            private void Painting(object sender,PaintEventArgs e)
            {
                  Graphics g = e.Graphics;
                  Pen pen = new Pen(Color.Aqua);
                  g.DrawRectangle(pen,0,0,x,y);
            }


            void MainFormMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  x = Cursor.Position.X;
                  y = Cursor.Position.Y;
                  if(moveRect)
                        this.Refresh();
            }

            private void MainForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  moveRect = false;      
            }

            private void MainForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  moveRect = true;
            }          
      }
}
0
 
madmareAuthor Commented:
what is this "moveRect" ?
0
 
existenz2Commented:
moveRect is a boolean which can be either true or false. If it's false, the rectangle wont resize with the border when the form size is changed, if it's true the rectangle will resize with the border when the form size is changed. So it's only added to control the resizing from the rectangle.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now