how about erasing trace of Rectangle? [modified]
-
hello.. this program is based of C# and WPF i draw Rectangle as mouse but this program get stain of Rectangle i want to disply without leaving any trace of Rectangle.. help me.. im sorry short english.. Screen Capture Image DownLoad ////////////////source///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;namespace rectTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}System.Drawing.Rectangle myRec; System.Windows.Point firstPoint; bool flg = false; private void Window\_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { flg = true; firstPoint = e.GetPosition(mainwin); myRec = new System.Drawing.Rectangle((int)firstPoint.X, (int)firstPoint.Y, 0, 0); } private void mainwin\_MouseMove(object sender, MouseEventArgs e) { if (flg == true) { System.Windows.Point currentPoint; currentPoint = e.GetPosition(mainwin); double width = currentPoint.X - firstPoint.X; double height = currentPoint.Y - firstPoint.Y; if (width > 0 && height > 0) { Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle); System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5); g.DrawRectangle(myPen, myRec); myRec.Width = (int)width; myRec.Height = (int)height; InvalidateVisual(); } } } private void mainwin\_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { flg = false; } }
}
modified on Tuesday, June 1, 2010 3:37 AM
-
hello.. this program is based of C# and WPF i draw Rectangle as mouse but this program get stain of Rectangle i want to disply without leaving any trace of Rectangle.. help me.. im sorry short english.. Screen Capture Image DownLoad ////////////////source///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;namespace rectTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}System.Drawing.Rectangle myRec; System.Windows.Point firstPoint; bool flg = false; private void Window\_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { flg = true; firstPoint = e.GetPosition(mainwin); myRec = new System.Drawing.Rectangle((int)firstPoint.X, (int)firstPoint.Y, 0, 0); } private void mainwin\_MouseMove(object sender, MouseEventArgs e) { if (flg == true) { System.Windows.Point currentPoint; currentPoint = e.GetPosition(mainwin); double width = currentPoint.X - firstPoint.X; double height = currentPoint.Y - firstPoint.Y; if (width > 0 && height > 0) { Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle); System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5); g.DrawRectangle(myPen, myRec); myRec.Width = (int)width; myRec.Height = (int)height; InvalidateVisual(); } } } private void mainwin\_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { flg = false; } }
}
modified on Tuesday, June 1, 2010 3:37 AM
How about erasing the old rectangle before drawing a new one?
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
System.Drawing.Pen whitePen = new System.Drawing.Pen(System.Drawing.Color.White, 5); // define the erasing pen
g.DrawRectangle(whitePen, myRec); // erase the old rectangle
myRec.Width = (int)width; // adjunst the new rectangle coordinates
myRec.Height = (int)height;
g.DrawRectangle(myPen, myRec); // draw the new rectangle
InvalidateVisual();I have no smart signature yet...
-
How about erasing the old rectangle before drawing a new one?
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
System.Drawing.Pen whitePen = new System.Drawing.Pen(System.Drawing.Color.White, 5); // define the erasing pen
g.DrawRectangle(whitePen, myRec); // erase the old rectangle
myRec.Width = (int)width; // adjunst the new rectangle coordinates
myRec.Height = (int)height;
g.DrawRectangle(myPen, myRec); // draw the new rectangle
InvalidateVisual();I have no smart signature yet...
-
if exist background image, don't this mothod this mothod is fill the white color... are you other mothod ??....
Of course there are other methods. For example you could: 1) get the image residing in the place you drawed your first rectangle 2) put the image over the first rectangle 3) get the image residing in the place you want to draw the second rectangle 4) draw the second rectangle 5) and so on Basicly, instead of filling with white the place where was the old rectangle, you could fill it with the old image data. Or, if you are really lazy, you could paint the background image over the whole control in order to remove the old rectangle and maintain the background. PS: be carefull of performance issues, drawing an image at every 20 ms it is not a good way to waste cpu time.
I have no smart signature yet...
-
hello.. this program is based of C# and WPF i draw Rectangle as mouse but this program get stain of Rectangle i want to disply without leaving any trace of Rectangle.. help me.. im sorry short english.. Screen Capture Image DownLoad ////////////////source///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;namespace rectTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}System.Drawing.Rectangle myRec; System.Windows.Point firstPoint; bool flg = false; private void Window\_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { flg = true; firstPoint = e.GetPosition(mainwin); myRec = new System.Drawing.Rectangle((int)firstPoint.X, (int)firstPoint.Y, 0, 0); } private void mainwin\_MouseMove(object sender, MouseEventArgs e) { if (flg == true) { System.Windows.Point currentPoint; currentPoint = e.GetPosition(mainwin); double width = currentPoint.X - firstPoint.X; double height = currentPoint.Y - firstPoint.Y; if (width > 0 && height > 0) { Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle); System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5); g.DrawRectangle(myPen, myRec); myRec.Width = (int)width; myRec.Height = (int)height; InvalidateVisual(); } } } private void mainwin\_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { flg = false; } }
}
modified on Tuesday, June 1, 2010 3:37 AM
Why on earth are you using GDI+ to draw your rectangle? If your application uses WPF, as you state, why not just create a WPF Rectangle object, and let WPF take care of the drawing?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Why on earth are you using GDI+ to draw your rectangle? If your application uses WPF, as you state, why not just create a WPF Rectangle object, and let WPF take care of the drawing?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
You see this line?
Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle);
That says you're using GDI+. Here's a sample rectangle implementation that uses native WPF features instead:
private System.Windows.Shapes.Rectangle GetRectangle(double left, double top)
{
System.Windows.Shapes.Rectangle myRect = new System.Windows.Shapes.Rectangle();
myRect.Stroke = System.Windows.Media.Brushes.Black;
myRect.Fill = System.Windows.Media.Brushes.SkyBlue;
myRect.Height = 0;
myRect.Width = 0;
// Add the rectangle to the canvas.
canvas.Children.Add(myRect);
canvas.SetLeft(myRect, left);
canvas.SetTop(myRect, top);
return myRect;
}private System.Windows.Shapes.Rectangle myRec;
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
flg = true;
firstPoint = e.GetPosition(mainwin);
myRec = GetRectangle(firstPoint.X, firstPoint.y);
}private void mainwin_MouseMove(object sender, MouseEventArgs e)
{
if (!flg) return;
System.Windows.Point currentPoint = e.GetPosition(mainwin);
double width = currentPoint.X - firstPoint.X;
double height = currentPoint.Y - firstPoint.Y;if (width <= 0 || height <= 0) return;
myRec.Width = width;
myRec.Height = height;
}Please note that I've just knocked this up in Notepad, so it might require a bit of tweaking.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.