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. Managed C++/CLI
  4. Getting the Brush to apply to pictureBox instead of Form

Getting the Brush to apply to pictureBox instead of Form

Scheduled Pinned Locked Moved Managed C++/CLI
graphicsquestion
11 Posts 2 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.
  • J Offline
    J Offline
    Jeffrey Webster
    wrote on last edited by
    #1

    Hi, I'm trying to draw this rotating line onto a picture box instead of a form. My question is: how do you specify that the FillRectangle and DrawLine statements should be applying to the picture box instead of the form on which it has been placed. Here's the code:

    private: System::Void timer1_Tick(System::Object * sender, System::EventArgs * e)
    {

    myAngle = myAngle + (tSpeed\*.004);
    if (myAngle > 6.283) myAngle = myAngle-6.283; 
    Graphics\* pg = CreateGraphics();
    SolidBrush\* sb= new SolidBrush(Color::LightGray);
    pg->FillRectangle(sb,10,10,200,200);
    point1=100 + 80\*sin(myAngle);
    point2=100 - 80\*cos(myAngle);
    Pen\* pen1 = new Pen(Color::Black);
    pg->DrawLine(pen1, 100,100,point1,point2);
    pg->Dispose();
    

    }

    Thanks for any suggestions. Jeff

    L 1 Reply Last reply
    0
    • J Jeffrey Webster

      Hi, I'm trying to draw this rotating line onto a picture box instead of a form. My question is: how do you specify that the FillRectangle and DrawLine statements should be applying to the picture box instead of the form on which it has been placed. Here's the code:

      private: System::Void timer1_Tick(System::Object * sender, System::EventArgs * e)
      {

      myAngle = myAngle + (tSpeed\*.004);
      if (myAngle > 6.283) myAngle = myAngle-6.283; 
      Graphics\* pg = CreateGraphics();
      SolidBrush\* sb= new SolidBrush(Color::LightGray);
      pg->FillRectangle(sb,10,10,200,200);
      point1=100 + 80\*sin(myAngle);
      point2=100 - 80\*cos(myAngle);
      Pen\* pen1 = new Pen(Color::Black);
      pg->DrawLine(pen1, 100,100,point1,point2);
      pg->Dispose();
      

      }

      Thanks for any suggestions. Jeff

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi Jeff, I have a standard reply for such a popular question, here it goes: there are several steps to correctly draw something; it does not matter how complex the paint job is: from a single line, to a complex drawing, or a real work of art. To make sure it all becomes visible on the screen and gets repainted automatically when moving, resizing, minimizing/maximizing/restoring or uncovering your Form, one should follow these steps: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics class and your variables. 4. if and when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer which ticks on the GUI thread, so you are allowed to call Invalidate() from there too. BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


      J 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi Jeff, I have a standard reply for such a popular question, here it goes: there are several steps to correctly draw something; it does not matter how complex the paint job is: from a single line, to a complex drawing, or a real work of art. To make sure it all becomes visible on the screen and gets repainted automatically when moving, resizing, minimizing/maximizing/restoring or uncovering your Form, one should follow these steps: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics class and your variables. 4. if and when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer which ticks on the GUI thread, so you are allowed to call Invalidate() from there too. BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


        J Offline
        J Offline
        Jeffrey Webster
        wrote on last edited by
        #3

        Hi, Thanks Luc. Yes, I'm aware that it is probably not a great idea to keep creating and disposing of the pens/brushes. I found in a previous application I'd written (in unmanaged C++) that it seemed to recreate the pen every time for some reason, even though I wasn't specifically telling it to do so. It kept crashing. Finally I gave up and simply created and deleted everything on each iteration. But I'll work on that again. Mostly I'm just trying to understand the syntax/object model of managed C++ well enough to actually do some basic examples. My big goal at this point is to control where the drawing is done, which unfortunately you didn't mention in your reply. The code examples I'm drawing from are from Msft VisualC++.Net by Templeman/Olsen (for anyone who is interested) and they are using the "Graphics" class to invoke the DrawLine method as you can see in the code. But how does this class know where to draw the line? Somehow it's assuming I want it drawn on the Form. What if I want it drawn somewhere else? Thanks, Jeff

        L 1 Reply Last reply
        0
        • J Jeffrey Webster

          Hi, Thanks Luc. Yes, I'm aware that it is probably not a great idea to keep creating and disposing of the pens/brushes. I found in a previous application I'd written (in unmanaged C++) that it seemed to recreate the pen every time for some reason, even though I wasn't specifically telling it to do so. It kept crashing. Finally I gave up and simply created and deleted everything on each iteration. But I'll work on that again. Mostly I'm just trying to understand the syntax/object model of managed C++ well enough to actually do some basic examples. My big goal at this point is to control where the drawing is done, which unfortunately you didn't mention in your reply. The code examples I'm drawing from are from Msft VisualC++.Net by Templeman/Olsen (for anyone who is interested) and they are using the "Graphics" class to invoke the DrawLine method as you can see in the code. But how does this class know where to draw the line? Somehow it's assuming I want it drawn on the Form. What if I want it drawn somewhere else? Thanks, Jeff

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi Jeffrey, I suggest you read points 1 and 3 once more. The Paint handler must be wired to the Control you want to paint on. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


          J 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi Jeffrey, I suggest you read points 1 and 3 once more. The Paint handler must be wired to the Control you want to paint on. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


            J Offline
            J Offline
            Jeffrey Webster
            wrote on last edited by
            #5

            Hi, Unfortunately I just can't find enough documentation on this subject to proceed at this point. I'll do some more digging... Thanks, Jeff

            L 1 Reply Last reply
            0
            • J Jeffrey Webster

              Hi, Unfortunately I just can't find enough documentation on this subject to proceed at this point. I'll do some more digging... Thanks, Jeff

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              what is wrong with myControl.Paint+=myPaintHandler; except for the fact that you want C++ syntax, mine is C#? :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              J 1 Reply Last reply
              0
              • L Luc Pattyn

                what is wrong with myControl.Paint+=myPaintHandler; except for the fact that you want C++ syntax, mine is C#? :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                J Offline
                J Offline
                Jeffrey Webster
                wrote on last edited by
                #7

                Hi, To me - and this may just reveal my ignorance - whether or not this is handled as a Paint event or simply placed within the timer sub doesn't really affect the central question which is how to get the brushes/pens to draw on the pictureBox and not the Form. To get some idea I simply used the Designer code generator to create MouseDown and MouseUp events on the pictureBox. This is what the code looked like:

                this->pictureBox1->MouseUp += new System::Windows::Forms::MouseEventHandler(this, pictureBox1_MouseUp);
                this->pictureBox1->MouseDown += new System::Windows::Forms::MouseEventHandler(this, pictureBox1_MouseDown);

                then for the drawing I used this

                private: System::Void pictureBox1_MouseDown(System::Object * sender, System::Windows::Forms::MouseEventArgs * e)
                {
                p1.X = e->X;
                p1.Y = e->Y;
                }

                private: System::Void pictureBox1_MouseUp(System::Object * sender, System::Windows::Forms::MouseEventArgs * e)
                {
                p2.X = e->X;
                p2.Y = e->Y;
                Graphics* gr= CreateGraphics();
                Pen* pen1 = new Pen(Color::Black);
                gr->DrawLine(pen1, p1.X,p1.Y,p2.X,p2.Y);
                gr->Dispose();
                }

                But this is still drawing on the Form, not the pictureBox. Puzzling. Jeff

                L 1 Reply Last reply
                0
                • J Jeffrey Webster

                  Hi, To me - and this may just reveal my ignorance - whether or not this is handled as a Paint event or simply placed within the timer sub doesn't really affect the central question which is how to get the brushes/pens to draw on the pictureBox and not the Form. To get some idea I simply used the Designer code generator to create MouseDown and MouseUp events on the pictureBox. This is what the code looked like:

                  this->pictureBox1->MouseUp += new System::Windows::Forms::MouseEventHandler(this, pictureBox1_MouseUp);
                  this->pictureBox1->MouseDown += new System::Windows::Forms::MouseEventHandler(this, pictureBox1_MouseDown);

                  then for the drawing I used this

                  private: System::Void pictureBox1_MouseDown(System::Object * sender, System::Windows::Forms::MouseEventArgs * e)
                  {
                  p1.X = e->X;
                  p1.Y = e->Y;
                  }

                  private: System::Void pictureBox1_MouseUp(System::Object * sender, System::Windows::Forms::MouseEventArgs * e)
                  {
                  p2.X = e->X;
                  p2.Y = e->Y;
                  Graphics* gr= CreateGraphics();
                  Pen* pen1 = new Pen(Color::Black);
                  gr->DrawLine(pen1, p1.X,p1.Y,p2.X,p2.Y);
                  gr->Dispose();
                  }

                  But this is still drawing on the Form, not the pictureBox. Puzzling. Jeff

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Jeffrey Webster wrote:

                  Graphics* gr= CreateGraphics();

                  is completely wrong: 1. you are getting a Graphics associated with your Form, not your PictureBox (how is the Graphics to know you want to paint on the PictureBox??) 2. you don't need to create a Graphics at all, you get one for free in any Paint handler. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                  J 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Jeffrey Webster wrote:

                    Graphics* gr= CreateGraphics();

                    is completely wrong: 1. you are getting a Graphics associated with your Form, not your PictureBox (how is the Graphics to know you want to paint on the PictureBox??) 2. you don't need to create a Graphics at all, you get one for free in any Paint handler. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                    J Offline
                    J Offline
                    Jeffrey Webster
                    wrote on last edited by
                    #9

                    Okay then send me some code, an example, something I can work with. I think by now we know that simply admonishing me for not using a Paint handler is not helping me. Thanks, Jeff

                    L 1 Reply Last reply
                    0
                    • J Jeffrey Webster

                      Okay then send me some code, an example, something I can work with. I think by now we know that simply admonishing me for not using a Paint handler is not helping me. Thanks, Jeff

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      C# example (not tested):

                      private Panel panel;
                      private bool paintRectFlag=true;
                      private Rectangle rect=new Rectangle(20, 20, 300, 200);
                      private Pen rectPen=Pens.Black;
                      …
                      public Form1() {
                      InitializeComponents();
                      panel=new Panel();
                      panel.Paint+=panelPaintHandler;
                      Controls.Add(panel);
                      }
                      …
                      protected void panelPaintHandler(object sender, PaintEventArgs e) {
                      Graphics g=e.Graphics;
                      if (paintRectFlag) g.DrawRectangle(rectPen, rect);
                      }

                      protected void buttonClickHandler(object sender, EventArgs e) {
                      paintRectFlag=!paintRectFlag; // toggle visible flag
                      panel.Invalidate();
                      }

                      :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                      J 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        C# example (not tested):

                        private Panel panel;
                        private bool paintRectFlag=true;
                        private Rectangle rect=new Rectangle(20, 20, 300, 200);
                        private Pen rectPen=Pens.Black;
                        …
                        public Form1() {
                        InitializeComponents();
                        panel=new Panel();
                        panel.Paint+=panelPaintHandler;
                        Controls.Add(panel);
                        }
                        …
                        protected void panelPaintHandler(object sender, PaintEventArgs e) {
                        Graphics g=e.Graphics;
                        if (paintRectFlag) g.DrawRectangle(rectPen, rect);
                        }

                        protected void buttonClickHandler(object sender, EventArgs e) {
                        paintRectFlag=!paintRectFlag; // toggle visible flag
                        panel.Invalidate();
                        }

                        :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                        J Offline
                        J Offline
                        Jeffrey Webster
                        wrote on last edited by
                        #11

                        Thanks for the code. As I understand it, the actual drawing occurs here:

                        protected void panelPaintHandler(object sender, PaintEventArgs e) {
                        Graphics g=e.Graphics;
                        if (paintRectFlag) g.DrawRectangle(rectPen, rect);

                        Which draws to a panel, which I'm assuming is a sort of generalized/abstract screen object. Presumably this would be rendered according to the needs of some Form (or other) object when invoked? So if you (were to) run this, would it draw a rectangle? Or do you need additional code to make it "work"? Thanks, Jeff

                        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