TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
albert albert
NA
524
0
Moving picturebox with Mouse
Sep 25 2011 5:33 AM
Hi Everyone,
I have this:
[code]
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;
using System.Drawing.Drawing2D;
namespace DrawingFigureWithMouse
{
public partial class Form1 : Form
{
private Point[] vertices = {new Point(50,100), new Point(100,50), new Point(150,100)};
private GraphicsPath p = new GraphicsPath();
private Region region;
private int oldX = 100;
private int oldY = 75;
private int x = 0;
private int y = 0;
private PictureBox circle = new PictureBox();
private Bitmap circleShape = new Bitmap(Properties.Resources.circle);
private Point _offset = Point.Empty;
private bool inside = false;
private bool drag = false;
public Form1()
{
InitializeComponent();
Size = new Size(400,400);
Text = "Moving shape with mouse";
BackColor = Color.White;
p.AddPolygon(vertices);
region = new Region(p);
circle.Size = new Size(200, 200);
circle.Visible = true;
circle.Location = new Point(40 + circle.Width, 150);
circle.Image = Properties.Resources.circle;
circle.MouseDown += new MouseEventHandler(circle_MouseDown);
circle.MouseUp += new MouseEventHandler(circle_MouseUp);
circle.MouseMove +=new MouseEventHandler(circle_MouseMove);
Controls.Add(circle);
}
private void circle_MouseDown(object sender, MouseEventArgs e)
{
drag = true;
x = e.X;
y = e.Y;
}
private void circle_MouseUp(object sender, MouseEventArgs e)
{
_offset = Point.Empty;
//drag = false;
}
private void circle_MouseMove(object sender, MouseEventArgs e)
{
/*
if (_offset != Point.Empty)
{
Point newLocation = this.Location;
circle.Left += e.X - x;
circle.Top += e.Y - y;
}
*/
if (drag)
{
circle.Left += e.X -x;
circle.Top += e.Y -y;
x = e.X;
y = e.Y;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRegion(Brushes.Blue,region);
base.OnPaint(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if(region.GetBounds(CreateGraphics()).Contains(e.X, e.Y))
{
oldX = e.X;
oldY = e.Y;
//pictureBox1.Width = e.X;
// pictureBox1.Height = e.Y;
inside = true;
}
base.OnMouseDown(e);
}//End method
protected override void OnMouseUp(MouseEventArgs e)
{
if (inside)
{
region = region.Clone();
region.Translate(e.X - oldX, e.Y - oldY);
oldX = e.X;
oldY = e.Y;
inside = false;
Invalidate();
}
base.OnMouseUp(e);
}
}
}
[/code]
I try to move the green circle with the mouse but there is a lot of flickering.
THX for helping!!
Reply
Answers (
1
)
Mobile Developemet using C#
How to send message to specific client from a server?