Using MouseMove in a PictureBox to enter i.e a signature on a touch screen.

Is there an example program available which catches the MouseMove event in a PictureBox to draw a line following the movement and allowing this to be saved as a bitmap. I've seen examples in C# but my knowledge of a possible translation to COBOL is inadequate.

Can somebody help me on this?

Regards

Rainer

  • Verified Answer

    Here is an example in a Windows Forms application:

           class-id savesignature.Form1 is partial
                     inherits type System.Windows.Forms.Form.
           
           working-storage section.
           01 prevLocation type Point value null.
           method-id NEW.
           procedure division.
               invoke self::InitializeComponent
               goback.
           end method.
    
           method-id pictureBox1_MouseDown final private.
           procedure division using by value sender as object e as type System.Windows.Forms.MouseEventArgs.
              set prevLocation to e::Location
              invoke self::pictureBox1_MouseMove(sender, e)
              
           end method.
    
           method-id pictureBox1_MouseMove final private.
           01 bmp type Bitmap.
           procedure division using by value sender as object e as type System.Windows.Forms.MouseEventArgs.
           
               if (PrevLocation not = null)
                  if (pictureBox1::Image = null)
                     set bmp to new Bitmap(pictureBox1::Width, pictureBox1::Height)
                        perform using g as type Graphics = type Graphics::FromImage(bmp)
                           invoke g::Clear(type Color::White)
                        end-perform
                     set pictureBox1::Image to bmp
                   end-if
                   perform using g as type Graphics = type Graphics::FromImage(pictureBox1::Image)
                      invoke g::DrawLine(type Pens::Black, prevLocation::X, prevLocation::Y, e::X, e::Y)
                   end-perform
                   invoke pictureBox1::Invalidate
                   set PrevLocation to e::Location
                end-if
    
           end method.
    
           method-id pictureBox1_MouseUp final private.
           procedure division using by value sender as object e as type System.Windows.Forms.MouseEventArgs.
               set prevLocation to null.
           end method.
    
           method-id button1_Click final private.
           01 bmp type Bitmap.
           procedure division using by value sender as object e as type System.EventArgs.
           
               set bmp to new type Bitmap(PictureBox1::Width, PictureBox1::Height)
               invoke PictureBox1::DrawToBitmap(bmp, PictureBox1::ClientRectangle)
               invoke bmp::Save("c:\\temp\mysignature.jpg", type System.Drawing.Imaging.ImageFormat::Jpeg)
        
           end method.
          
           end class.
    
  • Seems I've answered the question myself. Should anyone need this feature, here is a piece of code using Windows Forms.

    1st Create a pictureBox

    2nd set the mouse properties Up, Down Move

         $set ilusing "System.Drawing".

         $set ilusing "System.Drawing.Graphics".

         $set ilusing "System.IO.Stream".

    working-storage

          01 Pen              type Pen.

          01 bmp              type Bitmap.

          01 g                type Graphics.

          01 _Previous        type Point.

          01 mouseval         pic 9.

           88 mousedown       value 1 false 0.

    Methods:

          method-id pictureBox1_Paint final private.

          procedure division using by value sender as object e as type System.Windows.Forms.PaintEventArgs.

              set Pen to new Pen(type Color::Black, 3)

          end method.

          method-id pictureBox1_MouseMove final private.

          procedure division using by value sender as object e as type System.Windows.Forms.MouseEventArgs.

              if mousedown

                  if pictureBox1::Image = Null

                      set bmp to new Bitmap(PictureBox1::Width, PictureBox1::Height)

                      set g to type Graphics::FromImage(bmp)

                      invoke g::Clear(type Color::White)

                      set pictureBox1::Image to bmp

                  else

                      invoke g::DrawLine(Pen, _Previous::X, _Previous::Y, e::X, e::Y)

                  end-if

                  invoke pictureBox1::Invalidate()

                  set _Previous::X to e::X

                  set _Previous::Y to e::Y

              end-if

          end method.

          method-id pictureBox1_MouseDown final private.

          procedure division using by value sender as object e as type System.Windows.Forms.MouseEventArgs.

              set _Previous to new Point(e::X, e::Y)

              set mousedown to true

          end method.

          method-id pictureBox1_MouseUp final private.

          procedure division using by value sender as object e as type System.Windows.Forms.MouseEventArgs.

              set mousedown to false

              set _Previous to Null

          end method.

          method-id button2_Click final private.

          procedure division using by value sender as object e as type System.EventArgs.      

              invoke bmp::Save("Signature.bmp", type System.Drawing.Imaging.ImageFormat::Bmp)

              set pictureBox1::Image to Null

          end method.

  • Sorry Chris,

    didn't notice you had already answered my question. On the other hand - shows I'm getting better.

  • Yes Karl, you did quite well on the code conversion.

    I am very impressed!