練習A
#pragma once
namespace graph3 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 的摘要
///
/// 警告: 如果您變更這個類別的名稱,就必須變更與這個類別所依據之所有 .resx 檔案關聯的
/// Managed 資源編譯器工具的 'Resource File Name' 屬性。
/// 否則,這些設計工具
/// 將無法與這個表單關聯的當地語系化資源
/// 正確互動。
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此加入建構函式程式碼
//
}
protected:
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// 設計工具所需的變數。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
///
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 15);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(673, 613);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
protected: virtual void OnPaint(PaintEventArgs^ e) override{
System::Drawing::Font ^myFont=gcnew System::Drawing::Font("Verdana",18);
System::Drawing::SolidBrush ^redBrush=gcnew System::Drawing::SolidBrush(Color::Red);
System::Drawing::SolidBrush ^blueBrush=gcnew System::Drawing::SolidBrush(Color::Blue);
/*for(int i=0;i<20;i+=2){
if(i%4)
e->Graphics->DrawString("Hellow USC!", myFont, redBrush, 40+i*10, 40+i*10);
else
e->Graphics->DrawString("Hellow USC!", myFont, blueBrush, 400-i*10, 60+i*10);
}*/
Graphics^ g = e->Graphics;
Pen^ myPen = gcnew Pen(Color::Black, 3);
/*for(int i=0;i<20;i++){
for(int j=0;j<7000000;j++) ;
g->DrawEllipse(myPen, 30+i*15, 20+i*10, 220, 90);
}*/
/*
int x1 = 300,y1=200,x2=150,y2=150;
g->FillEllipse(redBrush2,x1,y1,x2,y2);
*/
Pen^ myPen2 = gcnew Pen(Color::White, 3);
SolidBrush^ redBrush2 = gcnew SolidBrush(Color::HotPink);
int r=200,x,y;
double deg=Math::PI/72;
for(int i=0;i<144;i++){
for(int j=0;j<70000000;j++) ;
g->DrawLine(myPen2, 200, 200, 200+x, 200+y);
x=r*Math::Sin(deg*i);
y=r*Math::Cos(deg*i);
g->DrawLine(myPen, 200, 200, 200+x, 200+y);
}
}
};
}
老師範例
#pragma once
namespace graph3 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 的摘要
///
/// 警告: 如果您變更這個類別的名稱,就必須變更與這個類別所依據之所有 .resx 檔案關聯的
/// Managed 資源編譯器工具的 'Resource File Name' 屬性。
/// 否則,這些設計工具
/// 將無法與這個表單關聯的當地語系化資源
/// 正確互動。
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此加入建構函式程式碼
//
}
protected:
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// 設計工具所需的變數。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
///
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 15);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(673, 613);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
protected: virtual void OnPaint(PaintEventArgs^ e) override{
System::Drawing::Font ^myFont=gcnew System::Drawing::Font("Verdana",18);
System::Drawing::SolidBrush ^redBrush=gcnew System::Drawing::SolidBrush(Color::Red);
e->Graphics->DrawString("Hellow USC!", myFont, redBrush, 40, 40);
//=========================================================================
Graphics^ g = e->Graphics;
Pen^ myPen = gcnew Pen(Color::Black, 3);
g->DrawEllipse(myPen, 30, 20, 220, 90);
// Graphics^ g = e->Graphics;
//==========================================================================
Pen^ goldPen = gcnew Pen(Color::Gold, 5);
array<PointF>^ curvePoints =gcnew array<PointF>
{PointF(230,260), PointF(230,235), PointF(370,215), PointF(410,260), PointF(430,310)};
g->DrawClosedCurve(goldPen, curvePoints);
//=====================================================================================
Pen^ blackPen = gcnew Pen(Color::Black, 3);
int x =35; // 方框左上點橫座標(方框為?圓之外框)
int y =15; // 方框左上點縱座標
int width = 200; // 方框寬
int height = 100; // 方框高
int startAngle = 40; // 開始角度
int sweepAngle = 230; // 掃過角度
g->DrawPie(blackPen, x, y, width, height, startAngle, sweepAngle);
//=======================================================================================
SolidBrush^ redBrush2 = gcnew SolidBrush(Color::LightBlue);
int x1 = 300;
int y1 = 20;
int x2 = 380;
int y2 = 70;
g->FillEllipse(redBrush2 , x1, y1, x2, y2);
//=======================================================================================
// 先產生fillRegion區域
System::Drawing::Region^ fillRegion =
gcnew System::Drawing::Region(Rectangle(230, 230, 330, 330));
// 將fillRegion區域與Rectangle 聯集
fillRegion->Complement(Rectangle(260, 260, 330, 330));
SolidBrush^ br1=gcnew SolidBrush(Color::Red);
// 畫實心區域圖
g->FillRegion(br1,fillRegion);
}
};
}
練習B
#pragma once
namespace graph3 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 的摘要
///
/// 警告: 如果您變更這個類別的名稱,就必須變更與這個類別所依據之所有 .resx 檔案關聯的
/// Managed 資源編譯器工具的 'Resource File Name' 屬性。
/// 否則,這些設計工具
/// 將無法與這個表單關聯的當地語系化資源
/// 正確互動。
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此加入建構函式程式碼
//
}
Graphics^ g1;
Pen ^p1, ^p2;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button3;
public:
private: System::Windows::Forms::Button^ button2;
protected:
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// 設計工具所需的變數。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
///
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(93, 440);
this->button1->Margin = System::Windows::Forms::Padding(5);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(122, 35);
this->button1->TabIndex = 0;
this->button1->Text = L"Circle";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(272, 440);
this->button2->Margin = System::Windows::Forms::Padding(5);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(122, 35);
this->button2->TabIndex = 1;
this->button2->Text = L"Retcangle";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(450, 440);
this->button3->Margin = System::Windows::Forms::Padding(5);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(122, 35);
this->button3->TabIndex = 2;
this->button3->Text = L"Clear";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(13, 23);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(668, 522);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Font = (gcnew System::Drawing::Font(L"新細明體", 13.8F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(136)));
this->Margin = System::Windows::Forms::Padding(5);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
g1=this->CreateGraphics();
p1=gcnew Pen(Color::Red, 2);
p2=gcnew Pen(Color::Blue, 4);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
g1->DrawEllipse(p2,200,200,200,200);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
g1->DrawRectangle(p1,150,70,250,80);
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
Refresh();
}
};
}
練習作業
#pragma once
namespace graph3 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 的摘要
///
/// 警告: 如果您變更這個類別的名稱,就必須變更與這個類別所依據之所有 .resx 檔案關聯的
/// Managed 資源編譯器工具的 'Resource File Name' 屬性。
/// 否則,這些設計工具
/// 將無法與這個表單關聯的當地語系化資源
/// 正確互動。
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此加入建構函式程式碼
//
}
Graphics^ g1;
Pen ^p1, ^p2;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button3;
public:
private: System::Windows::Forms::Button^ button2;
protected:
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// 設計工具所需的變數。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
///
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(93, 440);
this->button1->Margin = System::Windows::Forms::Padding(5);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(122, 35);
this->button1->TabIndex = 0;
this->button1->Text = L"Circle";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(272, 440);
this->button2->Margin = System::Windows::Forms::Padding(5);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(122, 35);
this->button2->TabIndex = 1;
this->button2->Text = L"Retcangle";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(450, 440);
this->button3->Margin = System::Windows::Forms::Padding(5);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(122, 35);
this->button3->TabIndex = 2;
this->button3->Text = L"Clear";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(13, 23);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(668, 522);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Font = (gcnew System::Drawing::Font(L"新細明體", 13.8F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(136)));
this->Margin = System::Windows::Forms::Padding(5);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
g1=this->CreateGraphics();
p1=gcnew Pen(Color::Red, 2);
p2=gcnew Pen(Color::Blue, 4);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Pen^ myPen2 = gcnew Pen(Color::White, 3);
SolidBrush^ redBrush2 = gcnew SolidBrush(Color::HotPink);
int r=200,x,y;
double deg=Math::PI/72;
for(int i=0;i<144;i++){
for(int j=0;j<70000000;j++) ;
x=r*Math::Sin(deg*i);
y=r*Math::Cos(deg*i);
g1->DrawEllipse(p2,240+x,150+y,150,150);
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Pen^ myPen2 = gcnew Pen(Color::White, 3);
SolidBrush^ redBrush2 = gcnew SolidBrush(Color::HotPink);
int r=200,x,y;
double deg=Math::PI/72;
for(int i=0;i<144;i++){
for(int j=0;j<70000000;j++) ;
x=r*Math::Sin(deg*i);
y=r*Math::Cos(deg*i);
g1->DrawRectangle(p1,240+x,150+y,150,70);
}
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
Refresh();
}
};
}