線を引くには? Part2

解決


z  2009-02-07 11:32:33  No: 69611

度々すみません。

Graphicsのインスタンスを取得したんですが、
g->DrawLineでブレイクを掛けても止まりません。

何が原因なのでしょうか?
以下、ソースです。

#pragma once

namespace test000000 {

  using namespace System;
  using namespace System::ComponentModel;
  using namespace System::Collections;
  using namespace System::Windows::Forms;
  using namespace System::Data;
  using namespace System::Drawing;

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

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

    public:
      Form1(void)
      {
        InitializeComponent();
      }

    protected:
      ~Form1()
      {
        if (components)
        {
          delete components;
        }
      }
    private: System::Windows::Forms::Button^  button1;
    protected: 

    private:
      System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
      void InitializeComponent(void)
      {
        this->button1 = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // button1
        this->button1->Location = System::Drawing::Point(98, 100);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"グラフ表示";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
        // Form1
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(292, 266);
        this->Controls->Add(this->button1);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->ResumeLayout(false);

      }
#pragma endregion
  
    //グラフ表示ボタン押下
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
      System::Windows::Forms::Form^ Form2 = gcnew Form;
      
      ButtonGraph = gcnew System::Windows::Forms::Button();
      ButtonGraph->Parent = Form2;
      ButtonGraph->Width = 50;
      ButtonGraph->Location = System::Drawing::Point(230, 54);
      ButtonGraph->Name = L"ButtonGraph";
      ButtonGraph->Size = System::Drawing::Size(50, 23);
      ButtonGraph->Text = L"グラフ表示";
      ButtonGraph->UseVisualStyleBackColor = true;
      ButtonGraph->Click += gcnew System::EventHandler(this, &Form1::ButtonGraph_Click);

      Form2->ShowIcon = false;
      Form2->MinimizeBox = false;
      Form2->MaximizeBox = false;

      Form2->Text = L"GrapthShow";
      Form2->Width = 200;
      Form2->Height = 200;
      Form2->MinimumSize = System::Drawing::Size(200,200);
      Form2->MaximumSize = System::Drawing::Size(200,200);

      Form2->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
      Form2->Activate();
      Form2->Show();
      Form2->Refresh();
    }

    private: System::Void ButtonGraph_Click(System::Object^  sender, System::EventArgs^  e) {
      System::Windows::Forms::PictureBox^  pictureBox1;

      pictureBox1 = gcnew PictureBox;

      pictureBox1->Dock = DockStyle::Fill;
      pictureBox1->BackColor = Color::White;

      pictureBox1->Paint += gcnew System::Windows::Forms::PaintEventHandler
        ( this, &Form1::pictureBox1_Paint );

      this->Controls->Add( pictureBox1 );
    }

    void pictureBox1_Paint( Object^ , System::Windows::Forms::PaintEventArgs^ e )
    {
      Graphics^ g = e->Graphics;

      g->DrawLine( System::Drawing::Pens::Red, 10,10,100,100 );
    }
  };
}


επιστημη  URL  2009-02-07 12:13:59  No: 69612

button1 と ButtonGraph とは何が違うんですか?
どっちも"グラフ表示"ですが、なぜ二つあるんですか?


z  2009-02-07 22:58:40  No: 69613

失礼しました。

グラフ表示がふたつありましたね。

一応、線を引くことができたのですが、Form1上に線を引いてしまいます。
Form2上に線はどのようにして引けばよいのでしょうか?

#pragma once

namespace test000000 {

  using namespace System;
  using namespace System::ComponentModel;
  using namespace System::Collections;
  using namespace System::Windows::Forms;
  using namespace System::Data;
  using namespace System::Drawing;

  public ref class Form1 : public System::Windows::Forms::Form
  {
    public:
      Form1(void)
      {
        InitializeComponent();
      }

    protected:
      ~Form1()
      {
        if (components)
        {
          delete components;
        }
      }
    private: System::Windows::Forms::Button^  button1;
    protected: 

    private:
      System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
      void InitializeComponent(void)
      {
        this->button1 = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // button1
        this->button1->Location = System::Drawing::Point(98, 100);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"グラフ表示";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
        // Form1
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(292, 266);
        this->Controls->Add(this->button1);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->ResumeLayout(false);

      }
#pragma endregion
  
    //グラフ表示ボタン押下
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
      System::Windows::Forms::Form^ Form2 = gcnew Form;
      
      System::Windows::Forms::PictureBox^  pictureBox1;

      pictureBox1 = gcnew PictureBox;

      pictureBox1->Dock = DockStyle::Fill;
      pictureBox1->BackColor = Color::White;

      pictureBox1->Paint += gcnew System::Windows::Forms::PaintEventHandler
        ( this, &Form1::pictureBox1_Paint );

      this->Controls->Add( pictureBox1 );

      Form2->ShowIcon = false;
      Form2->MinimizeBox = false;
      Form2->MaximizeBox = false;

      Form2->Text = L"GrapthShow";
      Form2->Width = 300;
      Form2->Height = 300;
      Form2->MinimumSize = System::Drawing::Size(300,300);
      Form2->MaximumSize = System::Drawing::Size(300,300);

      Form2->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
      Form2->Activate();
      Form2->Show();
      Form2->Refresh();
    }

    void pictureBox1_Paint( Object^ , System::Windows::Forms::PaintEventArgs^ e )
    {
      Graphics^ g = e->Graphics;

      g->DrawLine( System::Drawing::Pens::Red, 10,10,100,100 );
    }
  };
}


επιστημη  URL  2009-02-07 23:51:00  No: 69614

> 一応、線を引くことができたのですが、Form1上に線を引いてしまいます。

なぜそうなるかがわかっていれば対処法は明らかですよね。
PictureBoxをForm1,Form2のどちらに貼っていますか?

# いきあたりばったりに書きなぐってデバッグを人任せにしてない?


επιστημη  URL  2009-02-08 00:01:36  No: 69615

てかそんなの現象から明らかやん。


z  2009-02-08 00:15:49  No: 69616

pictureBox1->Paint += gcnew System::Windows::Forms::PaintEventHandler
        ( this, &Form1::pictureBox1_Paint );

↑では、Form1にしていますが、ここはForm2にはできないですよね?

すみません。初心者ですので、
PictureBoxをForm2に貼るというのは、どの命令のことをいっているのでしょうか?


επιστημη  URL  2009-02-08 00:23:05  No: 69617

>↑では、Form1にしていますが、ここはForm2にはできないですよね?

ごめん、何言いたいのかわからんです。

> PictureBoxをForm2に貼るというのは、どの命令のことをいっているのでしょうか?

↓こんなのがForm1のメソッド中にありますよ? 自分で書いたんちゃうの?
this->Controls->Add( pictureBox1 );


z  2009-02-08 01:17:41  No: 69618

ありがとうございます。

Form2->Controls->Add( pictureBox1 );

で解決できました。m(__)m


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加