Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how about erasing trace of Rectangle? [modified]

how about erasing trace of Rectangle? [modified]

Scheduled Pinned Locked Moved C#
csharpwpfgraphicslinqcom
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sonic747
    wrote on last edited by
    #1

    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

    S P 2 Replies Last reply
    0
    • S sonic747

      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

      S Offline
      S Offline
      Stanciu Vlad
      wrote on last edited by
      #2

      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...

      S 1 Reply Last reply
      0
      • S Stanciu Vlad

        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...

        S Offline
        S Offline
        sonic747
        wrote on last edited by
        #3

        if exist background image, don't this mothod this mothod is fill the white color... are you other mothod ??....

        S 1 Reply Last reply
        0
        • S sonic747

          if exist background image, don't this mothod this mothod is fill the white color... are you other mothod ??....

          S Offline
          S Offline
          Stanciu Vlad
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • S sonic747

            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

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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.

            My blog | My articles | MoXAML PowerToys | Onyx

            S 1 Reply Last reply
            0
            • P Pete OHanlon

              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.

              My blog | My articles | MoXAML PowerToys | Onyx

              S Offline
              S Offline
              sonic747
              wrote on last edited by
              #6

              how do working GDI+ . i don't know.. you see. mothod.. . please..!!

              P 1 Reply Last reply
              0
              • S sonic747

                how do working GDI+ . i don't know.. you see. mothod.. . please..!!

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups