2014年5月15日 星期四

視窗程式設計5/15

練習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();
			 }
};
}

2014年5月8日 星期四

視窗程式設計5/8

資料庫 PhoneBook.mdb
Visual Studio 2008 CLR主控台應用程式
// DB1.cpp: 主要專案檔。

#include "stdafx.h"

#using <System.Dll>
#using <System.Data.Dll>

using namespace System;
using namespace System::Data;
using namespace System::Data::OleDb;

int main(array<System::String ^> ^args)
{
        // 與指定的Access 資料庫建立連接
        String^ connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=D:\\PhoneBook.mdb;";      
        OleDbConnection^ myConn = gcnew OleDbConnection(connectionString);
        //OdbcCommand^ cmd;
         try
         {
   myConn->Open();
               //  設定取樣標準
              OleDbCommand^ myCommand = myConn->CreateCommand();
              myCommand->CommandText ="SELECT 姓名, 電話, 地址, 郵區 FROM Phone ";
              //  讀取資料
             OleDbDataReader^ myReader = myCommand->ExecuteReader();
            while (myReader->Read())
             {
                 Console::WriteLine("姓名 = " + myReader["姓名"]);
                 Console::WriteLine("電話 = " + myReader["電話"]);
                 Console::WriteLine("地址 = " + myReader["地址"]);
                 Console::WriteLine("郵區 = " + myReader["郵區"]);
   Console::WriteLine();
             }
             myConn->Close();
    myConn->Open();
              myCommand->CommandText ="INSERT INTO Phone VALUES  ('李九九', '02-11112222', '台北市星光大道100號' , '00011') " ;
                 // 新增資料
                 myCommand->ExecuteNonQuery();
                 Console::WriteLine("新增完成!");
              myCommand->CommandText ="SELECT 姓名, 電話, 地址, 郵區 FROM Phone ";
              //  讀取資料
              OleDbDataReader^ myReader2 = myCommand->ExecuteReader();
             while (myReader2->Read())
             {
                 Console::WriteLine("姓名 = " + myReader2["姓名"]);
                 Console::WriteLine("電話 = " + myReader2["電話"]);
                 Console::WriteLine("地址 = " + myReader2["地址"]);
                 Console::WriteLine("郵區 = " + myReader2["郵區"]);
   Console::WriteLine();
             }

        }
        catch (Exception^ e)
        {
            Console::WriteLine("Error: " + e);
        }

        catch (OleDbException^ ex)
        {
             Console::WriteLine(ex->ToString());
        } 
        finally
        {
             myConn->Close();
        }
}
資料庫為stdRec.mdb
A9803001 林品潔         33 96 66 75 Average=67
A9803004 王曉華         60 75 75 77 Average=71
A9803005 王智明         44 59 83 74 Average=65
A9803007 李斌           34 56 58 89 Average=59*
A9803009 陳大川         43 31 80 94 Average=62
A9803010 楊智源         38 65 49 62 Average=53*
A9803012 黃美鳳         39 99 82 51 Average=67
A9803015 鍾智勝         92 44 67 46 Average=62
A9803021 陳大川         75 88 65 64 Average=73
A9803022 朱彰文         54 79 96 80 Average=77
A9803025 黃莉茉         64 61 83 99 Average=76
A9803029 曾山國         47 78 31 37 Average=48*
A9803030 李雄山         43 58 67 99 Average=66
A9803033 林莉貞         41 38 50 34 Average=40*
A9803039 陳均安         82 55 87 62 Average=71
A9803042 李素惠         31 90 72 49 Average=60
A9803043 王小寶         62 49 94 74 Average=69
A9803048 李明白         50 30 35 32 Average=36*
A9803050 曾美蓮         99 92 78 91 Average=90
A9803052 魏一林         77 52 99 39 Average=66
請按任意鍵繼續 . . .
-------------------------------------------------------------

// DB1.cpp: 主要專案檔。

#include "stdafx.h"

#using <System.Dll>
#using <System.Data.Dll>

using namespace System;
using namespace System::Data;
using namespace System::Data::OleDb;

int main(array<System::String ^> ^args)
{
        // 與指定的Access 資料庫建立連接
  int sc;
  String^ tmp;

        String^ connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=d:\\stdRec.mdb;";      
        OleDbConnection^ myConn = gcnew OleDbConnection(connectionString);
        //OdbcCommand^ cmd;
         try
         {
   myConn->Open();
               //  設定取樣標準
              OleDbCommand^ myCommand = myConn->CreateCommand();
              //myCommand->CommandText ="SELECT 課程A.學號,課程A.分數, 課程B.分數 FROM 課程A, 課程B WHERE 課程A.學號=課程B.學號";
     myCommand->CommandText ="SELECT 學生資料表.學號, 姓名, 課程A.分數, 課程B.分數, 課程C.分數, 課程D.分數 from 學生資料表, 課程A, 課程B, 課程C, 課程D WHERE 學生資料表.學號=課程A.學號 and 學生資料表.學號=課程B.學號 and 學生資料表.學號=課程C.學號 and 學生資料表.學號=課程D.學號";
              
     //  讀取資料
             OleDbDataReader^ myReader = myCommand->ExecuteReader();
            while (myReader->Read())
             { 
     String^ tmp;
     int sc=0;
                  Console::Write(myReader["學號"]);
      Console::Write(" " + myReader["姓名"]);
                  Console::Write("    \t" + myReader["課程A.分數"]);
                  Console::Write(" " + myReader["課程B.分數"]);
      Console::Write(" " + myReader["課程C.分數"]);
      Console::Write(" " + myReader["課程D.分數"]);
                  //Console::WriteLine("電話號碼 = " + myReader["電話號碼"]);
                  //Console::WriteLine("聯絡地址 = " + myReader["聯絡地址"]);
      tmp=myReader["課程A.分數"]->ToString();
      sc+=System::Int32::Parse(tmp);
      tmp=myReader["課程B.分數"]->ToString();
      sc+=System::Int32::Parse(tmp);
      tmp=myReader["課程C.分數"]->ToString();
      sc+=System::Int32::Parse(tmp);
      tmp=myReader["課程D.分數"]->ToString();
      sc+=System::Int32::Parse(tmp);
      sc/=4.0;

      Console::Write(" Average=" + sc);
      if(sc >= 60) 
       Console::WriteLine( " ");
      else 
       Console::WriteLine( "*");
                  // Console::WriteLine("郵區 = " + myReader["郵區"]);
      //Console::WriteLine();
             }
        }
        catch (Exception^ e)
        {
            Console::WriteLine("Error: " + e);
        }

        catch (OleDbException^ ex)
        {
             Console::WriteLine(ex->ToString());
        } 
        finally
        {
             myConn->Close();
        }
}
Visual Studio 2008 Windows form 圖片移動  計畫名稱要一致
#pragma once


namespace KeyBoard {

 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: System::Windows::Forms::PictureBox^  pictureBox1;
 protected: 

 private:
  /// <summary>
  /// 設計工具所需的變數。
  String^ state;
  /// </summary>
  System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
  /// <summary>
  /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
     // String^ state;
  /// </summary>
 //System::ComponentModel::Container ^components;
  /// </summary>
  void InitializeComponent(void)
  {
   System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
   this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
   (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
   this->SuspendLayout();
   // 
   // pictureBox1
   // 
   this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureBox1.Image")));
   this->pictureBox1->Location = System::Drawing::Point(110, 53);
   this->pictureBox1->Name = L"pictureBox1";
   this->pictureBox1->Size = System::Drawing::Size(61, 56);
   this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
   this->pictureBox1->TabIndex = 0;
   this->pictureBox1->TabStop = false;
   // 
   // Form1
   // 
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->BackColor = System::Drawing::Color::White;
   this->ClientSize = System::Drawing::Size(290, 189);
   this->Controls->Add(this->pictureBox1);
   this->Name = L"Form1";
   this->Text = L"鍵盤按鍵";
   this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
   (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
   this->ResumeLayout(false);

  }
#pragma endregion

 private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
     this->KeyUp += gcnew KeyEventHandler(this, &KeyBoard::Form1::OnKeyPress);
      state="Up";
    }

 public: void OnKeyPress(Object^ sender, KeyEventArgs^ e)
    {
  String^ skey = e->KeyCode.ToString();   //  取得按鍵名稱
  if (skey=="Up") {
    // 同向未碰到頂邊則向上移動
    if ((pictureBox1->Top>=10) && state=="Up") {
                        pictureBox1->Top -= 10;
    }
                else
                {
                        state = "Up";
                        pictureBox1->ImageLocation = "D:\\\ArrowU.gif";
                }
  }
  if (skey=="Down") {
    // 同向未碰到底邊則向下移動
                if ((pictureBox1->Top <=this->Height-40-10) && state == "Down")
                        pictureBox1->Top += 10;
                else
                {
                        state = "Down";
      pictureBox1->ImageLocation = "D:\\ArrowD.gif";
                }
  }
  if (skey=="Left") {
    // 同向未碰到左邊則向左移動
    if ((pictureBox1->Left >= 10) && state == "Left")
                        pictureBox1->Left -= 10;
                else
                {
                        state = "Left";
                        pictureBox1->ImageLocation = "D:\\ArrowL.gif";
                }
  }
  if (skey=="Right") {
    // 同向未碰到右邊則向右移動
                if ((pictureBox1->Left <= this->Width - 40 - 10) && state == "Right")
                        pictureBox1->Left += 10;
                else
                {
                        state = "Right";
                        pictureBox1->ImageLocation = "D:\\ArrowR.gif";
                }
  }
      }


 };
}


Visual Studio 2008 Windows form 畫矩形 計畫名稱要一致
#pragma once


namespace mouse {

 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;
   }
  }
 bool mouseOn;
 int x0,y0,x1,y1,w,h;
 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(421, 374);
   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) {
     this->MouseDown +=gcnew MouseEventHandler(this, &mouse::Form1::OnMouseDown);
     this->MouseUp +=gcnew MouseEventHandler(this, &mouse::Form1::OnMouseUp);
     this->MouseMove +=gcnew MouseEventHandler(this, &mouse::Form1::OnMouseMove);
     mouseOn=false;
    }
 private:  System::Void OnMouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^ e) {
     x0=e->X;
     y0=e->Y;
     x1=e->X;
     y1=e->Y;
     mouseOn=true;
    }
 private:  System::Void OnMouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^ e) {
     w=e->X-x1;
     h=e->Y-y1;
     if(w<0){
      w=w*-1;
      x0=e->X;
     }

     if(h<0){
      h=h*-1;
      y0=e->Y;
     }     
     Refresh();
     mouseOn=false;
    }
 private:  System::Void OnMouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^ e) {
     if( mouseOn){
      w=e->X-x1;
      h=e->Y-y1 ;
     if(w<0){
      w=w*-1;
      x0=e->X;
     }

     if(h<0){
       h=h*-1;
      y0=e->Y;
     }     
     Refresh();
     }
    }
 protected: virtual void OnPaint(PaintEventArgs^  e) override{
     Pen^ blackPen = gcnew Pen(Color::Black, 3);
     e->Graphics->DrawRectangle(blackPen,x0,y0,w,h);
    }
 };
}


2014年5月1日 星期四

視窗程式設計5/1

egles老鷹圖片在網路學園
#pragma once


namespace eagle {

 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;
   }
  }
  int times;
 private: System::Windows::Forms::Button^  button1;
 protected: 
 private: System::Windows::Forms::PictureBox^  pictureBox1;
 private: System::Windows::Forms::Button^  button2;
 private: System::Windows::Forms::Timer^  timer1;
 private: System::Windows::Forms::Button^  button3;
 private: System::ComponentModel::IContainer^  components;

 private:
  /// <summary>
  /// 設計工具所需的變數。
  /// </summary>


#pragma region Windows Form Designer generated code
  /// <summary>
  /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
  ///
  /// </summary>
  void InitializeComponent(void)
  {
   this->components = (gcnew System::ComponentModel::Container());
   this->button1 = (gcnew System::Windows::Forms::Button());
   this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
   this->button2 = (gcnew System::Windows::Forms::Button());
   this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
   this->button3 = (gcnew System::Windows::Forms::Button());
   (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
   this->SuspendLayout();
   // 
   // button1
   // 
   this->button1->Location = System::Drawing::Point(12, 22);
   this->button1->Name = L"button1";
   this->button1->Size = System::Drawing::Size(75, 23);
   this->button1->TabIndex = 0;
   this->button1->Text = L"Stop";
   this->button1->UseVisualStyleBackColor = true;
   this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
   // 
   // pictureBox1
   // 
   this->pictureBox1->Location = System::Drawing::Point(22, 65);
   this->pictureBox1->Name = L"pictureBox1";
   this->pictureBox1->Size = System::Drawing::Size(173, 150);
   this->pictureBox1->TabIndex = 1;
   this->pictureBox1->TabStop = false;
   this->pictureBox1->Click += gcnew System::EventHandler(this, &Form1::pictureBox1_Click);
   // 
   // button2
   // 
   this->button2->Location = System::Drawing::Point(174, 22);
   this->button2->Name = L"button2";
   this->button2->Size = System::Drawing::Size(75, 23);
   this->button2->TabIndex = 2;
   this->button2->Text = L"Exit";
   this->button2->UseVisualStyleBackColor = true;
   this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
   // 
   // timer1
   // 
   this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
   // 
   // button3
   // 
   this->button3->Location = System::Drawing::Point(93, 22);
   this->button3->Name = L"button3";
   this->button3->Size = System::Drawing::Size(75, 23);
   this->button3->TabIndex = 3;
   this->button3->Text = L"restart";
   this->button3->UseVisualStyleBackColor = true;
   this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
   // 
   // Form1
   // 
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)), 
    static_cast<System::Int32>(static_cast<System::Byte>(255)));
   this->ClientSize = System::Drawing::Size(284, 262);
   this->Controls->Add(this->button3);
   this->Controls->Add(this->button2);
   this->Controls->Add(this->pictureBox1);
   this->Controls->Add(this->button1);
   this->Name = L"Form1";
   this->Text = L"Form1";
   this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
   (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
   this->ResumeLayout(false);

  }
#pragma endregion
 private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
     switch(times){
      case 0:
       pictureBox1->Image=Image::FromFile("D:\\Eagle2.gif");
       times=1;
       break;
      case 1:
       pictureBox1->Image=Image::FromFile("D:\\Eagle3.gif");
       times=2;
       break;
      case 2:
       pictureBox1->Image=Image::FromFile("D:\\Eagle1.gif");
       times=0;
       break;
    }
       pictureBox1->Left +=14;
       pictureBox1->Top +=4;
       pictureBox1->Height +=5;
       pictureBox1->Width +=6;
    }
 private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
     timer1->Enabled=true;
     timer1->Interval=100;
     times=0;
     Form1::Height=1024;
     Form1::Width=1024;
    }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    timer1->Enabled=!timer1->Enabled;
    if(timer1->Enabled==true)
     button1->Text="Start";
    else
     button1->Text="Retart";
   }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    Application::Exit();
   }
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
    timer1->Enabled=true;
    System::Media::SoundPlayer^ so = gcnew System::Media::SoundPlayer("D:\\");
    so->Play();
   }
};
}


視窗程式設計期中考

midTest1
#include <iostream>

using namespace std;

void sort(int *dd,int num){   // Descending
 int tmp;

 for(int i=0;i<num-1;i++){
  for(int j=i+1;j<num;j++){
   if(dd[i]<dd[j]){
                    tmp=dd[i];
                    dd[i]=dd[j];
                    dd[j]=tmp;
   }
  }
 }
}
int bsearch(int* dd, int low, int high, int tk){
    int idx;

    if(low > high) return -1;
    idx=(low+high)/2;
    if(tk==dd[idx]) return idx;
    else if(tk>dd[idx]) bsearch(dd,low,idx-1,tk);
    else bsearch(dd,idx+1,high,tk);
}
int main(){
    int num, rk, tk;
    int data[]={59,83,66,54,79,63,44,99,50,88,73,47,80,75,60,39,92,95,87,69,57};

 num=sizeof(data)/4;       // number of array element
 sort(data,num);
    rk=bsearch(data,0,20,63);
    if(rk < 0) cout << "score 63 not found!!!" << endl;
    else cout << "rank of 63 is " << bsearch(data,0,20,63)+1 << endl;
    rk=bsearch(data,0,20,53);
    if(rk < 0) cout << "score 53 not found!!!" << endl;
    else cout << "rank of 53 is " << bsearch(data,0,20,53)+1 << endl;
    rk=bsearch(data,0,20,92);
    if(rk < 0) cout << "score 92 not found!!!" << endl;
    else cout << "rank of 92 is " << bsearch(data,0,20,92)+1 << endl;
    }

midTest2
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
int main(void)
{
   char txt[80],ch;
   string record;

   ifstream ifile("D:\\stdrec5.txt",ios::in);        // for input
   ofstream ofileA("D:\\reportA.txt",ios::out);       // for output1
   ofstream ofileB("D:\\reportB.txt",ios::out);       // for output2
   char porf=' ';
   string cls, id, name,sc[5],fnum;

   int isc[5],sum;
   while(!ifile.eof())
   {
       sum=0;
       fnum="";
       ifile.getline(txt,80,'\n');
       cout << txt << endl;
       record=txt;
       cls=record.substr(0,6);          // class
       id=record.substr(6,8);           // student identifier
       name=record.substr(14,10);         // student name
       for(int i=0;i<5;i++){              // get 5 course score & transfer to integer
            sc[i]=record.substr(24+i*3,3);
            isc[i]=atoi(sc[i].c_str());
            sum+=isc[i];
            if(isc[i]<60) fnum+="*";
      }
      if(cls.compare("DITM3A")==0){
            ofileA<<cls<<" "<<id<<" "<<name<<" "<<sc[0]<<" "<<sc[1]<<" "<<sc[2]<<" "<<sc[3]<<" "<<sc[4]<<"  Total="<<sum<<" Average="<<round(sum*10/5.0)/10.0<<"("<<fnum<<")"<<endl;
      }
      else{
            ofileB<<cls<<" "<<id<<" "<<name<<" "<<sc[0]<<" "<<sc[1]<<" "<<sc[2]<<" "<<sc[3]<<" "<<sc[4]<<"  Total="<<sum<<" Average="<<round(sum*10/5.0)/10.0<<"("<<fnum<<")"<<endl;
      }
   }
   ifile.close();
   ofileA.close();
   ofileB.close();

   return 0;
}

midTest3
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

class person{
private:
    double pound,inch,kg,m;
public:
    person(double pd, double in){
        pound=pd;
        inch=in;
        toKg();
        toM();
    }
private:
    void toKg(){
        kg=pound*0.454;
    }
    void toM(){
        m=inch*0.0254;
    }
public:
    double getKg(){
        return kg;
    }
    double getM(){
        return m;
    }
};

class BMI:person{

    public:
    BMI(double pd, double in):person(pd, in){};
    public:
        short ckBMI(){
            double bmi=getBMI();
            if(bmi<18.5) return -1;
            else if(bmi<24.0) return 0;
            else if(bmi<27.0) return 1;
            else if(bmi<30.0) return 2;
            else if(bmi<35.0) return 3;
            else return 4;
        }
        double getBMI(){
            double kg,m, bmi;
            kg=person::getKg();
            m=person::getM();
            return bmi=kg/(m*m);
        }
        char* toMessage(short f){
            switch(f){
                case -1:
                    return "體重過輕";
                    break;
                case 0:
                    return "正常";
                    break;
                case 1:
                    return "過重";
                    break;
                case 2:
                    return "輕度肥胖";
                    break;
                case 3:
                    return "中度肥胖";
                    break;
                case 4:
                    return "重度肥胖";
            }
        }
};


int main(void) {
        BMI John(200, 71);
        BMI Mary(170, 69);
        BMI Ronny(192, 65);

        cout << "John BMI is " << John.getBMI()<< " Status:" << John.toMessage(John.ckBMI())<< endl;
        cout << "Mary BMI is " << Mary.getBMI()<< " Status:" << Mary.toMessage(Mary.ckBMI())<< endl;
        cout << "Ronny BMI is " << Ronny.getBMI()<< " Status:" << Ronny.toMessage(Ronny.ckBMI())<< endl;
    }