Click here to Skip to main content
15,904,416 members
Home / Discussions / C#
   

C#

 
GeneralRe: "View Code" Dropdown Pin
Dave Kreskowiak6-Dec-09 17:49
mveDave Kreskowiak6-Dec-09 17:49 
QuestionStrange Behavior: base.OnPaint [Solved] Pin
Saksida Bojan6-Dec-09 5:48
Saksida Bojan6-Dec-09 5:48 
AnswerRe: Strange Behavior: base.OnPaint [Solved] Pin
Shameel6-Dec-09 23:14
professionalShameel6-Dec-09 23:14 
QuestionSerialization Pin
hadad6-Dec-09 4:26
hadad6-Dec-09 4:26 
AnswerRe: Serialization Pin
Natza Mitzi6-Dec-09 10:02
Natza Mitzi6-Dec-09 10:02 
QuestionDataGridview Drag and drop visualization Pin
anishkannan6-Dec-09 4:25
anishkannan6-Dec-09 4:25 
AnswerRe: DataGridview Drag and drop visualization Pin
Mycroft Holmes6-Dec-09 13:40
professionalMycroft Holmes6-Dec-09 13:40 
QuestionDraw resizeable and draggable rectangle on picturebox to crop image Pin
thungphan5-Dec-09 21:31
thungphan5-Dec-09 21:31 
Hi everyone!
I have an app: draw a resizeable and draggable rectangle on form.
Now i want to modify it to draw a resizeable and draggable rectangle on picturebox not on Form to select area and crop image.
I tried a few ways but i can't. I'm newbie in C#. Please help me! Thank you so much!
Here's my code:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace DragResizeRectangle
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form		
	{					
		private RectangleF DragRect=new RectangleF(20, 20, 100, 80);
		
		private System.Drawing.RectangleF[] Adornments = new RectangleF[0];
		
		private bool MouseInRect = false;
		private bool RectDragging = false;
	
		private bool RectResizing = false;
	
		private int ActiveAdornment = -1;
	
		private Point MousePos;
		private SolidBrush BackBrush;
		
		private System.Drawing.Pen[] LinePen = new Pen[0];
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			Adornments=new RectangleF[8];
			LinePen=new Pen[2];
			
			BackBrush = new SolidBrush(BackColor);
			LinePen[0] = new Pen(ForeColor);
			LinePen[1] = new Pen(ForeColor);
			LinePen[1].DashPattern = new float[] { 5, 3 };
	
			CalcAdornments();
	
			SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);														
						
		}
		private void CalcAdornments()
		{
			Adornments[0] = new RectangleF(DragRect.X - 4, DragRect.Y - 4, 8, 8);
			Adornments[1] = new RectangleF(DragRect.X + DragRect.Width / 2 - 4, DragRect.Y - 4, 8, 8);
			Adornments[2] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y - 4, 8, 8);
			Adornments[3] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y + DragRect.Height / 2 - 4, 8, 8);
			Adornments[4] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
			Adornments[5] = new RectangleF(DragRect.X + DragRect.Width / 2 - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
			Adornments[6] = new RectangleF(DragRect.X - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
			Adornments[7] = new RectangleF(DragRect.X - 4, DragRect.Y + DragRect.Height / 2 - 4, 8, 8);
		}
		
		private void SetAdornmentCursor()
		{
			if (ActiveAdornment != -1) {
				switch (ActiveAdornment) {
					case 0:
						Cursor = Cursors.SizeNWSE;
						break;
					case 1:
						Cursor = Cursors.SizeNS;
						break;
					case 2:
						Cursor = Cursors.SizeNESW;
						break;
					case 3:
						Cursor = Cursors.SizeWE;
						break;
					case 4:
						Cursor = Cursors.SizeNWSE;
						break;
					case 5:
						Cursor = Cursors.SizeNS;
						break;
					case 6:
						Cursor = Cursors.SizeNESW;
						break;
					case 7:
						Cursor = Cursors.SizeWE;
						break;
				}
			}
			else {
				Cursor = Cursors.Default;
			}			
		}
							
		void MainFormMouseDown(object sender, MouseEventArgs e)
		{
			
			if (e.Button == MouseButtons.Left) {
				if (MouseInRect & ActiveAdornment == -1) {
					RectDragging = true;
					MousePos = new Point(e.X, e.Y);
					MousePos = PointToScreen(MousePos);
				}
				else if (ActiveAdornment != -1) {
					RectResizing = true;
					MousePos = new Point(e.X, e.Y);
					MousePos = PointToScreen(MousePos);
				}
			}
			
		}
						
		void MainFormMouseMove(object sender, MouseEventArgs e)
		{			
			
			if (e.Button == MouseButtons.Left) {
				Point CurrMouse = new Point(e.X, e.Y);
	
				CurrMouse.X = Math.Max(0, Math.Min(CurrMouse.X, ClientSize.Width));
				CurrMouse.Y = Math.Max(0, Math.Min(CurrMouse.Y, ClientSize.Height));
	
				CurrMouse = PointToScreen(CurrMouse);
	
				if (RectDragging) {
					if (Math.Abs(CurrMouse.X - MousePos.X) > 2 | Math.Abs(CurrMouse.Y - MousePos.Y) > 2) {
						DragRect.Offset(CurrMouse.X - MousePos.X, CurrMouse.Y - MousePos.Y);
	
						DragRect.X = Math.Max(0, Math.Min(DragRect.X, ClientSize.Width - DragRect.Width));
						DragRect.Y = Math.Max(0, Math.Min(DragRect.Y, ClientSize.Height - DragRect.Height));
	
						MousePos = new Point(e.X, e.Y);
	
						MousePos.X = Math.Max(0, Math.Min(MousePos.X, ClientSize.Width));
						MousePos.Y = Math.Max(0, Math.Min(MousePos.Y, ClientSize.Height));
	
						MousePos = PointToScreen(MousePos);
						CalcAdornments();
						Invalidate();
					}
				}
				else if (RectResizing) {
					if (ActiveAdornment != -1) {
	
						if (Math.Abs(CurrMouse.X - MousePos.X) > 2 | Math.Abs(CurrMouse.Y - MousePos.Y) > 2) {
							switch (ActiveAdornment) {
								case 0:
									DragRect.Height -= CurrMouse.Y - MousePos.Y;
									if (!(DragRect.Height < 20)) {
										DragRect.Y -= -CurrMouse.Y + MousePos.Y;
									}
									else {
										DragRect.Height = 20;
									}
	
	
									DragRect.Width -= CurrMouse.X - MousePos.X;
									if (!(DragRect.Width < 20)) {
										DragRect.X -= -CurrMouse.X + MousePos.X;
									}
									else {
										DragRect.Width = 20;
									}
	
									break;
								case 1:
									DragRect.Height -= CurrMouse.Y - MousePos.Y;
									if (!(DragRect.Height < 20)) {
										DragRect.Y -= -CurrMouse.Y + MousePos.Y;
									}
									else {
										DragRect.Height = 20;
									}
	
									break;
								case 2:
									DragRect.Height -= CurrMouse.Y - MousePos.Y;
									if (!(DragRect.Height < 20)) {
										DragRect.Y -= -CurrMouse.Y + MousePos.Y;
									}
									else {
										DragRect.Height = 20;
									}
	
	
									DragRect.Width += CurrMouse.X - MousePos.X;
									DragRect.Width = Math.Max(20, DragRect.Width);
									break;
								case 3:
									DragRect.Width += CurrMouse.X - MousePos.X;
									DragRect.Width = Math.Max(20, DragRect.Width);
									break;
								case 4:
									DragRect.Width += CurrMouse.X - MousePos.X;
									DragRect.Width = Math.Max(20, DragRect.Width);
	
									DragRect.Height += CurrMouse.Y - MousePos.Y;
									DragRect.Height = Math.Max(20, DragRect.Height);
									break;
								case 5:
									DragRect.Height += CurrMouse.Y - MousePos.Y;
									DragRect.Height = Math.Max(20, DragRect.Height);
									break;
								case 6:
									DragRect.Height += CurrMouse.Y - MousePos.Y;
									DragRect.Height = Math.Max(20, DragRect.Height);
	
									DragRect.Width -= CurrMouse.X - MousePos.X;
									if (!(DragRect.Width < 20)) {
										DragRect.X -= -CurrMouse.X + MousePos.X;
									}
									else {
										DragRect.Width = 20;
									}
	
									break;
								case 7:
									DragRect.Width -= CurrMouse.X - MousePos.X;
									if (!(DragRect.Width < 20)) {
										DragRect.X -= -CurrMouse.X + MousePos.X;
									}
									else {
										DragRect.Width = 20;
									}
	
									break;
							}
	
							MousePos = new Point(e.X, e.Y);
	
							MousePos.X = Math.Max(0, Math.Min(MousePos.X, ClientSize.Width));
							MousePos.Y = Math.Max(0, Math.Min(MousePos.Y, ClientSize.Height));
	
							MousePos = PointToScreen(MousePos);
							CalcAdornments();
							Invalidate();
						}
					}
				}
			}
			else {
				MouseInRect = false;
				ActiveAdornment = -1;
	
				RectangleF tmprect = default(RectangleF);
				int i = 0;
				foreach (RectangleF tmprect_loopVariable in Adornments) {
					tmprect = tmprect_loopVariable;
					if (tmprect.Contains(e.X, e.Y)) {
						MouseInRect = true;
						ActiveAdornment = i;
						SetAdornmentCursor();
						Invalidate();
						return;
					}
					i += 1;
				}
	
				if (DragRect.Contains(e.X, e.Y)) {
					MouseInRect = true;
					Cursor = Cursors.SizeAll;
					Invalidate();
					return;
				}
	
				Cursor = Cursors.Default;
				Invalidate();
			}			
			
		}						
		
		void MainFormMouseUp(object sender, MouseEventArgs e)
		{
			
			if (e.Button == MouseButtons.Left) {
				RectDragging = false;
				RectResizing = false;
				Invalidate();
			}
			
		}
		
		void MainFormPaint(object sender, PaintEventArgs e)
		{	
			
			Graphics g = e.Graphics;			
			
			
			g.FillRectangle(BackBrush, g.ClipBounds);
			
			if (!MouseInRect) {
				g.DrawRectangle(LinePen[0], DragRect.X, DragRect.Y, DragRect.Width, DragRect.Height);												
			}
			else {
				g.DrawRectangle(LinePen[1], DragRect.X, DragRect.Y, DragRect.Width, DragRect.Height);
				g.FillRectangles(Brushes.White, Adornments);
				g.DrawRectangles(Pens.Black, Adornments);
			}								
		}																																			
	}
}

AnswerRe: Draw resizeable and draggable rectangle on picturebox to crop image Pin
Luc Pattyn6-Dec-09 1:12
sitebuilderLuc Pattyn6-Dec-09 1:12 
QuestionUnusual behaviour with Directory.Exists() method Pin
Cracked-Down5-Dec-09 21:21
Cracked-Down5-Dec-09 21:21 
AnswerRe: Unusual behaviour with Directory.Exists() method Pin
N a v a n e e t h5-Dec-09 21:39
N a v a n e e t h5-Dec-09 21:39 
QuestionThe best method for designing a low profile client application Pin
SummerBulb5-Dec-09 21:13
SummerBulb5-Dec-09 21:13 
AnswerRe: The best method for designing a low profile client application Pin
N a v a n e e t h5-Dec-09 21:45
N a v a n e e t h5-Dec-09 21:45 
GeneralRe: The best method for designing a low profile client application Pin
SummerBulb5-Dec-09 22:02
SummerBulb5-Dec-09 22:02 
GeneralRe: The best method for designing a low profile client application Pin
N a v a n e e t h5-Dec-09 22:25
N a v a n e e t h5-Dec-09 22:25 
QuestionRe: The best method for designing a low profile client application [modified] Pin
SummerBulb5-Dec-09 22:38
SummerBulb5-Dec-09 22:38 
AnswerRe: The best method for designing a low profile client application Pin
N a v a n e e t h6-Dec-09 1:54
N a v a n e e t h6-Dec-09 1:54 
AnswerRe: The best method for designing a low profile client application Pin
Daniel Grunwald6-Dec-09 6:42
Daniel Grunwald6-Dec-09 6:42 
AnswerRe: The best method for designing a low profile client application Pin
PIEBALDconsult6-Dec-09 17:08
mvePIEBALDconsult6-Dec-09 17:08 
QuestionNewbie DatagridView Question Pin
kruegersck5-Dec-09 20:56
kruegersck5-Dec-09 20:56 
AnswerRe: Newbie DatagridView Question Pin
OriginalGriff5-Dec-09 21:46
mveOriginalGriff5-Dec-09 21:46 
QuestionConvert string to property name? Pin
BDJones5-Dec-09 18:39
BDJones5-Dec-09 18:39 
AnswerRe: Convert string to property name? Pin
N a v a n e e t h5-Dec-09 18:58
N a v a n e e t h5-Dec-09 18:58 
QuestionRe: Convert string to property name? Pin
BDJones5-Dec-09 19:34
BDJones5-Dec-09 19:34 
AnswerRe: Convert string to property name? Pin
N a v a n e e t h5-Dec-09 19:39
N a v a n e e t h5-Dec-09 19:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.