Ir para conteúdo
Fórum Script Brasil
  • 0

Desenhando em janela (windows form)


diegobill

Pergunta

Estou tentando fazer um programa para desenhar com o mouse (dando só um clique) em uma janela windows form usando o visual c++, mas não está funcionando quando clico não desenha...

ARQUIVO Form1.h:

-----------------------------------------------------

#pragma once

#include "elementos.h"

namespace path {

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

using namespace CLRSketcher;

enum class ElementType{robo, obstaculo, objetivo};

/// <summary>

/// Summary for Form1

///

/// WARNING: If you change the name of this class, you will need to change the

/// 'Resource File Name' property for the managed resource compiler tool

/// associated with all .resx files this class depends on. Otherwise,

/// the designers will not be able to interact properly with localized

/// resources associated with this form.

/// </summary>

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void) : drawing(false)

{

InitializeComponent();

//

//TODO: Add the constructor code here

//

}

protected:

/// <summary>

/// Clean up any resources being used.

/// </summary>

~Form1()

{

if (components)

{

delete components;

}

}

private: System::Windows::Forms::RadioButton^ radioButton1;

protected:

private: System::Windows::Forms::RadioButton^ radioButton2;

private: System::Windows::Forms::RadioButton^ radioButton3;

private:

/// <summary>

/// Required designer variable.

/// </summary>

System::ComponentModel::Container ^components;

// Current element type

ElementType elementType;

// Current drawing color

//Color color;

// Records when drawing an element is in progress

bool drawing;

// Temporary store for the element being drawn

Element^ tempRobo;

Element^ tempObjetivo;

private: System::Windows::Forms::Button^ button1;

Element^ tempObstaculo;

#pragma region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

void InitializeComponent(void)

{

this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());

this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());

this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());

this->button1 = (gcnew System::Windows::Forms::Button());

this->SuspendLayout();

//

// radioButton1

//

this->radioButton1->AutoSize = true;

this->radioButton1->Location = System::Drawing::Point(12, 237);

this->radioButton1->Name = L"radioButton1";

this->radioButton1->Size = System::Drawing::Size(46, 17);

this->radioButton1->TabIndex = 0;

this->radioButton1->TabStop = true;

this->radioButton1->Text = L"robo";

this->radioButton1->UseVisualStyleBackColor = true;

this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton1_CheckedChanged);

//

// radioButton2

//

this->radioButton2->AutoSize = true;

this->radioButton2->Location = System::Drawing::Point(64, 237);

this->radioButton2->Name = L"radioButton2";

this->radioButton2->Size = System::Drawing::Size(71, 17);

this->radioButton2->TabIndex = 1;

this->radioButton2->TabStop = true;

this->radioButton2->Text = L"obstaculo";

this->radioButton2->UseVisualStyleBackColor = true;

this->radioButton2->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton2_CheckedChanged);

//

// radioButton3

//

this->radioButton3->AutoSize = true;

this->radioButton3->Location = System::Drawing::Point(141, 237);

this->radioButton3->Name = L"radioButton3";

this->radioButton3->Size = System::Drawing::Size(62, 17);

this->radioButton3->TabIndex = 2;

this->radioButton3->TabStop = true;

this->radioButton3->Text = L"objetivo";

this->radioButton3->UseVisualStyleBackColor = true;

this->radioButton3->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton3_CheckedChanged);

//

// button1

//

this->button1->Location = System::Drawing::Point(210, 237);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(75, 23);

this->button1->TabIndex = 3;

this->button1->Text = L"trajetoria";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click_1);

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->ClientSize = System::Drawing::Size(746, 266);

this->Controls->Add(this->button1);

this->Controls->Add(this->radioButton3);

this->Controls->Add(this->radioButton2);

this->Controls->Add(this->radioButton1);

this->Name = L"Form1";

this->Text = L"trajetoria";

this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);

this->ResumeLayout(false);

this->PerformLayout();

}

#pragma endregion

private: System::Void radioButton1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {

elementType = ElementType::robo;

}

private: System::Void radioButton2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {

elementType = ElementType::obstaculo;

}

private: System::Void radioButton3_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {

elementType = ElementType::objetivo;

}

private: System::Void button1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {

Graphics^ g = e->Graphics;

if(tempRobo)

tempRobo->Draw(g);

if(tempObjetivo)

tempObjetivo->Draw(g);

if(tempObstaculo)

tempObstaculo->Draw(g);

}

private: System::Void button1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {

if(e->Button == System::Windows::Forms::MouseButtons::Left) drawing = true;

if(drawing)

{

switch(elementType)

{

case ElementType::robo:

tempRobo = gcnew CLRSketcher::Rectangle(Color::Red, e->Location);

break;

case ElementType::obstaculo:

tempObstaculo = gcnew CLRSketcher::Rectangle(Color::Green, e->Location);

break;

case ElementType::objetivo:

tempObjetivo = gcnew CLRSketcher::Rectangle(Color::Blue, e->Location);

break;

}

Invalidate();

}

}

private: System::Void button1_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {

drawing = false;

}

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

}

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

}

private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) {

}

};

}

ARQUIVO elementos.h:

---------------------------------------------------------------------

// elementos.h

// Defines element types

#pragma once

using namespace System;

using namespace System::Drawing;

namespace CLRSketcher

{

// The base class for all elements

public ref class Element abstract

{

protected:

Point position;

Color color;

System::Drawing::Rectangle boundRect;

Pen^ pen;

public:

virtual void Draw(Graphics^ g) abstract;

};

// The class defining a line element

public ref class Line : Element

{

protected:

Point end;

public:

// Constructor

Line(Color color, Point start, Point end)

{

pen = gcnew Pen(color);

this->color = color;

position = start;

this->end = end;

boundRect = System::Drawing::Rectangle(Math::Min(position.X, end.X),

Math::Min(position.Y, end.Y),

Math::Abs(position.X - end.X), Math::Abs(position.Y - end.Y));

// Provide for lines that are horizontal or vertical

if(boundRect.Width < 2) boundRect.Width = 2;

if(boundRect.Height < 2) boundRect.Height = 2;

}

// Function to draw a line

virtual void Draw(Graphics^ g) override

{

g->DrawLine(pen, position, end);

}

};

// The class defining a rectangle element

public ref class Rectangle : Element

{

protected:

public:

Rectangle(Color color, Point p1)

{

pen = gcnew Pen(color);

this->color = color;

position = p1;

boundRect = System::Drawing::Rectangle(position, Size(20, 20));

}

virtual void Draw(Graphics^ g) override

{

g->DrawRectangle(pen, position.X-10, position.Y-10, 20, 20);

}

};

// The class defining a circle element

public ref class Circle : Element

{

protected:

int width;

int height;

public:

Circle(Color color, Point center, Point circum)

{

pen = gcnew Pen(color);

this->color = color;

int radius = safe_cast<int>(Math::Sqrt(

(center.X-circum.X)*(center.X-circum.X) +

(center.Y-circum.Y)*(center.Y-circum.Y)));

position = Point(center.X - radius, center.Y - radius);

width = height = 2*radius;

boundRect = System::Drawing::Rectangle(position, Size(width, height));

}

virtual void Draw(Graphics^ g) override

{

g->DrawEllipse(pen, position.X, position.Y, width,height);

}

};

}

Link para o comentário
Compartilhar em outros sites

3 respostass a esta questão

Posts Recomendados

  • 0

CÓDIGO DENTRO DE

[b]ARQUIVO Form1.h:[/b]
-----------------------------------------------------
[code]
#pragma once

#include "elementos.h"
namespace path {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace CLRSketcher;
    enum class ElementType{robo, obstaculo, objetivo};
    /// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          'Resource File Name' property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void) : drawing(false)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::RadioButton^  radioButton1;
    protected: 
    private: System::Windows::Forms::RadioButton^  radioButton2;
    private: System::Windows::Forms::RadioButton^  radioButton3;


    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;
         // Current element type
         ElementType elementType;
         // Current drawing color
         //Color color;
         // Records when drawing an element is in progress
         bool drawing;

             // Temporary store for the element being drawn
         Element^ tempRobo;
         Element^ tempObjetivo;
    private: System::Windows::Forms::Button^  button1;


             Element^ tempObstaculo;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
            this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
            this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // radioButton1
            // 
            this->radioButton1->AutoSize = true;
            this->radioButton1->Location = System::Drawing::Point(12, 237);
            this->radioButton1->Name = L"radioButton1";
            this->radioButton1->Size = System::Drawing::Size(46, 17);
            this->radioButton1->TabIndex = 0;
            this->radioButton1->TabStop = true;
            this->radioButton1->Text = L"robo";
            this->radioButton1->UseVisualStyleBackColor = true;
            this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton1_CheckedChanged);
            // 
            // radioButton2
            // 
            this->radioButton2->AutoSize = true;
            this->radioButton2->Location = System::Drawing::Point(64, 237);
            this->radioButton2->Name = L"radioButton2";
            this->radioButton2->Size = System::Drawing::Size(71, 17);
            this->radioButton2->TabIndex = 1;
            this->radioButton2->TabStop = true;
            this->radioButton2->Text = L"obstaculo";
            this->radioButton2->UseVisualStyleBackColor = true;
            this->radioButton2->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton2_CheckedChanged);
            // 
            // radioButton3
            // 
            this->radioButton3->AutoSize = true;
            this->radioButton3->Location = System::Drawing::Point(141, 237);
            this->radioButton3->Name = L"radioButton3";
            this->radioButton3->Size = System::Drawing::Size(62, 17);
            this->radioButton3->TabIndex = 2;
            this->radioButton3->TabStop = true;
            this->radioButton3->Text = L"objetivo";
            this->radioButton3->UseVisualStyleBackColor = true;
            this->radioButton3->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton3_CheckedChanged);
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(210, 237);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 3;
            this->button1->Text = L"trajetoria";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click_1);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(746, 266);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->radioButton3);
            this->Controls->Add(this->radioButton2);
            this->Controls->Add(this->radioButton1);
            this->Name = L"Form1";
            this->Text = L"trajetoria";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void radioButton1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
                 elementType = ElementType::robo;
             }
private: System::Void radioButton2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
             elementType = ElementType::obstaculo;
         }
private: System::Void radioButton3_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
             elementType = ElementType::objetivo;
         }
private: System::Void button1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
  Graphics^ g = e->Graphics;
  if(tempRobo)
  tempRobo->Draw(g);
  if(tempObjetivo)
  tempObjetivo->Draw(g);
  if(tempObstaculo)
  tempObstaculo->Draw(g);
         }
private: System::Void button1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
  if(e->Button == System::Windows::Forms::MouseButtons::Left) drawing = true;
  if(drawing)
  {
    switch(elementType)
    {
    case ElementType::robo:
        tempRobo = gcnew CLRSketcher::Rectangle(Color::Red, e->Location);
        break;
    case ElementType::obstaculo:
        tempObstaculo = gcnew CLRSketcher::Rectangle(Color::Green, e->Location);
        break;
    case ElementType::objetivo:
        tempObjetivo = gcnew CLRSketcher::Rectangle(Color::Blue, e->Location);
        break;
    }
    Invalidate();
  }
         }
private: System::Void button1_MouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
  drawing = false;
         }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e) {
         }
};
}
ARQUIVO elementos.h: ---------------------------------------------------------------------
// elementos.h
// Defines element types
#pragma once

using namespace System;
using namespace System::Drawing;

namespace CLRSketcher 
{
  // The base class for all elements
  public ref class Element abstract
  {
  protected:
    Point position;
    Color color;
    System::Drawing::Rectangle boundRect;
    Pen^ pen;

  public:
    virtual void Draw(Graphics^ g) abstract;
  };

  // The class defining a line element
  public ref class Line : Element
  {
    protected:
      Point end;

    public:
      // Constructor
      Line(Color color, Point start, Point end)
      {
        pen = gcnew Pen(color);
        this->color = color;
        position = start;
        this->end = end;
        boundRect = System::Drawing::Rectangle(Math::Min(position.X, end.X),
                                               Math::Min(position.Y, end.Y),
                       Math::Abs(position.X - end.X), Math::Abs(position.Y - end.Y));

        // Provide for lines that are horizontal or vertical      
        if(boundRect.Width < 2) boundRect.Width = 2;
        if(boundRect.Height < 2) boundRect.Height = 2;
      }

      // Function to draw a line
      virtual void Draw(Graphics^ g) override
      {
        g->DrawLine(pen, position, end);
      }
    };

  // The class defining a rectangle element
  public ref class Rectangle : Element
  {
  protected:

  public:
    Rectangle(Color color, Point p1)
    {
      pen = gcnew Pen(color);
      this->color = color;
      position = p1;
      boundRect = System::Drawing::Rectangle(position, Size(20, 20));
    }

        virtual void Draw(Graphics^ g) override
    {
        g->DrawRectangle(pen, position.X-10, position.Y-10, 20, 20);
    }
  };

  // The class defining a circle element
  public ref class Circle : Element
  {
  protected:
    int width;
    int height;

  public:
    Circle(Color color, Point center, Point circum)
    {
      pen = gcnew Pen(color);
      this->color = color;
      int radius = safe_cast<int>(Math::Sqrt(
                  (center.X-circum.X)*(center.X-circum.X) +
                                 (center.Y-circum.Y)*(center.Y-circum.Y)));
      position = Point(center.X - radius, center.Y - radius);
      width = height = 2*radius;
      boundRect = System::Drawing::Rectangle(position, Size(width, height));
   }

    virtual void Draw(Graphics^ g) override
    {
      g->DrawEllipse(pen, position.X, position.Y, width,height);
    }
  };

}

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,3k
    • Posts
      652,3k
×
×
  • Criar Novo...