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.
My blog | My articles | MoXAML PowerToys | Onyx