Click here to Skip to main content
15,887,683 members
Home / Discussions / C#
   

C#

 
QuestionUPnP AV Client in C# Pin
N-O-R-B-E-R-T12-Aug-07 22:37
N-O-R-B-E-R-T12-Aug-07 22:37 
AnswerRe: UPnP AV Client in C# Pin
leppie12-Aug-07 23:39
leppie12-Aug-07 23:39 
AnswerRe: UPnP AV Client in C# Pin
leppie12-Aug-07 23:40
leppie12-Aug-07 23:40 
Questionhow to setup C# TCPClient timeout Pin
George_George12-Aug-07 22:36
George_George12-Aug-07 22:36 
QuestionConnection to SQL ! Pin
mrkeivan12-Aug-07 21:59
mrkeivan12-Aug-07 21:59 
AnswerRe: Connection to SQL ! Pin
Giorgi Dalakishvili12-Aug-07 22:09
mentorGiorgi Dalakishvili12-Aug-07 22:09 
Questionvertical align in richtextbox Pin
Cuciu12-Aug-07 21:38
Cuciu12-Aug-07 21:38 
Questionc# - Painting problem Pin
T.D.Prasad12-Aug-07 21:27
T.D.Prasad12-Aug-07 21:27 
I am trying to draw a rubberband rectangle using one of the methods suggested in this forum. My problem is: after drawing the rectangle, if I move another window (for example a messagebox) over the rectangle (ruberband rectangle drawn earlier) part of this rectangle is not getting pained properly. Any help is highly appreciated.

The code that I am using is given below.

<br />
//<br />
// MainForm.cs - Demo shell application for the RubberbandRectangle class.<br />
// project created on 9/4/2003 at 11:43 PM by cthomas<br />
//<br />
<br />
using System;<br />
using System.Windows.Forms;<br />
using System.Drawing;<br />
<br />
namespace RubberbandShape<br />
{<br />
	class MainForm : System.Windows.Forms.Form<br />
	{<br />
		public MainForm()<br />
		{<br />
			InitializeComponent();<br />
        }<br />
	<br />
		// THIS METHOD IS MAINTAINED BY THE FORM DESIGNER<br />
		// DO NOT EDIT IT MANUALLY! YOUR CHANGES ARE LIKELY TO BE LOST<br />
		void InitializeComponent() {<br />
            this.button1 = new System.Windows.Forms.Button();<br />
            this.SuspendLayout();<br />
            // <br />
            // button1<br />
            // <br />
            this.button1.Location = new System.Drawing.Point(323, 256);<br />
            this.button1.Name = "button1";<br />
            this.button1.Size = new System.Drawing.Size(75, 23);<br />
            this.button1.TabIndex = 0;<br />
            this.button1.Text = "button1";<br />
            this.button1.UseVisualStyleBackColor = true;<br />
            this.button1.Click += new System.EventHandler(this.button1_Click);<br />
            // <br />
            // MainForm<br />
            // <br />
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br />
            this.ClientSize = new System.Drawing.Size(401, 282);<br />
            this.Controls.Add(this.button1);<br />
            this.Name = "MainForm";<br />
            this.Text = "Rubber Band Shape Drawing";<br />
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);<br />
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseUp);<br />
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseMove);<br />
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseDown);<br />
            this.ResumeLayout(false);<br />
<br />
		}<br />
			<br />
		[STAThread]<br />
		public static void Main(string[] args)<br />
		{<br />
			Application.Run(new MainForm());<br />
		}<br />
<br />
		private bool mouseDown = false;<br />
		private bool mouseMoving = false;<br />
		private bool haveRect = false;<br />
		private int XDown;<br />
		private int YDown;<br />
        private int oldX;<br />
        private Button button1;<br />
		private int oldY;<br />
<br />
		// The MouseDown event will be used to initialize the one corner<br />
		// of the RubberRectangle when the Left mouse button is down.<br />
		// If the Right mouse button is pressed and there is a<br />
		// RubberRectangle on the screen, it is erased.<br />
		void MainForm_MouseDown(object sender,<br />
		                       System.Windows.Forms.MouseEventArgs e)<br />
		{<br />
			if( e.Button == MouseButtons.Left )<br />
			{<br />
				mouseDown = true;<br />
				XDown = e.X;<br />
				YDown = e.Y;<br />
				mouseMoving = false;<br />
			}<br />
			else if( e.Button == MouseButtons.Right )<br />
			{<br />
				if( haveRect )<br />
				{<br />
					haveRect = false;	// Clear the flag<br />
					Invalidate();		// Clear out the rectangle.<br />
				}<br />
			}<br />
		}<br />
		<br />
		// The MouseMove event is used top acutally draw the rectangle<br />
		// after erasing to previously draw one if the mouse has moved since.<br />
		void MainForm_MouseMove(object sender,<br />
		                       System.Windows.Forms.MouseEventArgs e)<br />
		{<br />
			if( mouseDown )<br />
			{<br />
				if( mouseMoving )<br />
					RubberRectangle( XDown, YDown, oldX, oldY );<br />
				RubberRectangle( XDown, YDown, e.X, e.Y );<br />
				mouseMoving = true;<br />
				oldX = e.X;<br />
				oldY = e.Y;<br />
			}<br />
		}<br />
		<br />
		// Clears that flags that were used during drawing and sets a flag<br />
		//that indicates the presence of the rectangle.<br />
		void MainForm_MouseUp(object sender,<br />
		                     System.Windows.Forms.MouseEventArgs e)<br />
		{<br />
			mouseDown = false;<br />
			mouseMoving = false;<br />
			haveRect = true;<br />
		}<br />
		<br />
		// Draws the RubberRectangle onto the client area using the<br />
		// RubberbandRectangle class.<br />
		private void RubberRectangle( int X1, int Y1, int X2, int Y2 )<br />
		{<br />
			System.Drawing.Graphics g = Graphics.FromHwnd( this.Handle );<br />
			<br />
			RubberbandRectangle rect<br />
			= new RubberbandRectangle();<br />
			rect.DrawXORRectangle( g, X1, Y1, X2, Y2 );<br />
		}<br />
		<br />
		// Paints the test objects onto the client area.  If a RubberRectangle<br />
		// was there also, repaints that, too.<br />
		void MainForm_Paint(object sender,<br />
		                   System.Windows.Forms.PaintEventArgs e)<br />
		{            <br />
			Graphics g = Graphics.FromHwnd( this.Handle );<br />
/*<br />
			g.FillRectangle( Brushes.Black, 0, 0, 150, 150 );<br />
			g.FillRectangle( Brushes.Wheat, 150, 0, 150, 150 );<br />
			g.FillRectangle( Brushes.Red, 0, 150, 100, 100 );<br />
			g.FillRectangle( Brushes.Green, 100, 150, 100, 100 );<br />
			g.FillRectangle( Brushes.Blue, 200, 150, 100, 100 );<br />
 */<br />
			if( haveRect )<br />
				RubberRectangle( XDown, YDown, oldX, oldY );<br />
		}<br />
<br />
        private void button1_Click(object sender, EventArgs e)<br />
        {<br />
            MessageBox.Show("err yty");<br />
        }<br />
		<br />
	}	<br />
	<br />
	//*******************************************************<br />
    public enum PenStyles<br />
    {<br />
        PS_SOLID = 0,<br />
        PS_DASH = 1,<br />
        PS_DOT = 2,<br />
        PS_DASHDOT = 3,<br />
        PS_DASHDOTDOT = 4<br />
    }<br />
    public class RubberbandRectangle<br />
    {<br />
        // These values come from the larger set of defines in WinGDI.h,<br />
        // but are all that are needed for this application.  If this class<br />
        // is expanded for more generic rectangle drawing, they should be<br />
        // replaced by enums from those sets of defones.<br />
        private int NULL_BRUSH = 5;<br />
        private int R2_XORPEN = 7;<br />
        private PenStyles penStyle;<br />
        private int BLACK_PEN = 0;<br />
<br />
        // Default contructor - sets member fields<br />
        public RubberbandRectangle()<br />
        {<br />
            penStyle = PenStyles.PS_DOT;<br />
        }<br />
<br />
        // penStyles property get/set.<br />
        public PenStyles PenStyle<br />
        {<br />
            get { return penStyle; }<br />
            set { penStyle = value; }<br />
        }<br />
<br />
        public void DrawXORRectangle(Graphics grp,<br />
                                      int X1, int Y1, int X2, int Y2)<br />
        {<br />
            // Extract the Win32 HDC from the Graphics object supplied.<br />
            IntPtr hdc = grp.GetHdc();<br />
<br />
            // Create a pen with a dotted style to draw the border of the<br />
            // rectangle.<br />
            IntPtr gdiPen = CreatePen(penStyle,<br />
                          1, BLACK_PEN);<br />
<br />
            // Set the ROP cdrawint mode to XOR.<br />
            SetROP2(hdc, R2_XORPEN);<br />
<br />
            // Select the pen into the device context.<br />
            IntPtr oldPen = SelectObject(hdc, gdiPen);<br />
<br />
            // Create a stock NULL_BRUSH brush and select it into the device<br />
            // context so that the rectangle isn't filled.<br />
            IntPtr oldBrush = SelectObject(hdc,<br />
                                 GetStockObject(NULL_BRUSH));<br />
<br />
            // Now XOR the hollow rectangle on the Graphics object with<br />
            // a dotted outline.<br />
            Rectangle(hdc, X1, Y1, X2, Y2);<br />
<br />
            // Put the old stuff back where it was.<br />
            SelectObject(hdc, oldBrush); // no need to delete a stock object<br />
            SelectObject(hdc, oldPen);<br />
            DeleteObject(gdiPen);		// but we do need to delete the pen<br />
<br />
            // Return the device context to Windows.<br />
            grp.ReleaseHdc(hdc);<br />
        }<br />
<br />
        // Use Interop to call the corresponding Win32 GDI functions<br />
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]<br />
        private static extern int SetROP2(<br />
                IntPtr hdc,		// Handle to a Win32 device context<br />
                int enDrawMode	// Drawing mode<br />
                );<br />
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]<br />
        private static extern IntPtr CreatePen(<br />
                PenStyles enPenStyle,	// Pen style from enum PenStyles<br />
                int nWidth,				// Width of pen<br />
                int crColor				// Color of pen<br />
                );<br />
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]<br />
        private static extern bool DeleteObject(<br />
                IntPtr hObject	// Win32 GDI handle to object to delete<br />
                );<br />
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]<br />
        private static extern IntPtr SelectObject(<br />
                IntPtr hdc,		// Win32 GDI device context<br />
                IntPtr hObject	// Win32 GDI handle to object to select<br />
                );<br />
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]<br />
        private static extern void Rectangle(<br />
                IntPtr hdc,			// Handle to a Win32 device context<br />
                int X1,				// x-coordinate of top left corner<br />
                int Y1,				// y-cordinate of top left corner<br />
                int X2,				// x-coordinate of bottom right corner<br />
                int Y2				// y-coordinate of bottm right corner<br />
                );<br />
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]<br />
        private static extern IntPtr GetStockObject(<br />
                int brStyle	// Selected from the WinGDI.h BrushStyles enum<br />
                );<br />
<br />
        // C# version of Win32 RGB macro<br />
        private static int RGB(int R, int G, int B)<br />
        {<br />
            return (R | (G << 8) | (B << 16));<br />
        }<br />
    }<br />
}<br />
<br />


Cheers,
Prasad
AnswerRe: c# - Painting problem Pin
Albu Marius12-Aug-07 22:31
Albu Marius12-Aug-07 22:31 
AnswerRe: c# - Painting problem Pin
Cuciu12-Aug-07 22:42
Cuciu12-Aug-07 22:42 
QuestionRe: c# - Painting problem Pin
T.D.Prasad12-Aug-07 23:51
T.D.Prasad12-Aug-07 23:51 
AnswerRe: c# - Painting problem Pin
Christian Graus13-Aug-07 0:31
protectorChristian Graus13-Aug-07 0:31 
AnswerRe: c# - Painting problem Pin
Christian Graus12-Aug-07 23:52
protectorChristian Graus12-Aug-07 23:52 
QuestionReading Numeric and AlphaNumeric Values from Excel Pin
meeram39512-Aug-07 20:10
meeram39512-Aug-07 20:10 
AnswerRe: Reading Numeric and AlphaNumeric Values from Excel Pin
travlos718-Mar-09 6:14
travlos718-Mar-09 6:14 
AnswerRe: Reading Numeric and AlphaNumeric Values from Excel Pin
InfoBridgeSolutions17-Aug-10 19:30
InfoBridgeSolutions17-Aug-10 19:30 
QuestionDatagrid paging-c# windows application Pin
hemchandars12-Aug-07 20:06
hemchandars12-Aug-07 20:06 
AnswerRe: Datagrid paging-c# windows application Pin
Jax_qqq12-Aug-07 21:41
Jax_qqq12-Aug-07 21:41 
GeneralRe: Datagrid paging-c# windows application Pin
hemchandars13-Aug-07 1:03
hemchandars13-Aug-07 1:03 
Questionhow to Read folder contents [modified] Pin
salmonraju12-Aug-07 20:05
salmonraju12-Aug-07 20:05 
AnswerRe: how to Read folder contents Pin
Michael Sync12-Aug-07 20:18
Michael Sync12-Aug-07 20:18 
GeneralRe: how to Read folder contents Pin
salmonraju12-Aug-07 21:02
salmonraju12-Aug-07 21:02 
AnswerRe: how to Read folder contents Pin
tker12-Aug-07 20:29
tker12-Aug-07 20:29 
GeneralRe: how to Read folder contents Pin
salmonraju12-Aug-07 21:01
salmonraju12-Aug-07 21:01 
Questioncrystal reports Pin
monuSaini12-Aug-07 19:58
monuSaini12-Aug-07 19:58 

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.