form1.h

来自「《VC++2005编程实例》第一章的源代码」· C头文件 代码 · 共 175 行

H
175
字号
#pragma once


namespace Example {

	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 文件关联的托管资源编译器工具的
	///          “资源文件名”属性。否则,
	///          设计器将不能与此窗体的关联
	///          本地化资源正确交互。
	/// </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::Label^  label1;
	protected: 
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::Panel^  panel1;

	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要
		/// 使用代码编辑器修改此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(13, 24);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(185, 12);
			this->label1->TabIndex = 0;
			this->label1->Text = L"请在右侧的组合框中选择颜色值:";
			// 
			// comboBox1
			// 
			this->comboBox1->DrawMode = System::Windows::Forms::DrawMode::OwnerDrawFixed;
			this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Location = System::Drawing::Point(204, 18);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(174, 22);
			this->comboBox1->TabIndex = 1;
			this->comboBox1->DrawItem += gcnew System::Windows::Forms::DrawItemEventHandler(this, &Form1::comboBox1_DrawItem);
			this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
			this->comboBox1->MeasureItem += gcnew System::Windows::Forms::MeasureItemEventHandler(this, &Form1::comboBox1_MeasureItem);
			// 
			// panel1
			// 
			this->panel1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
			this->panel1->Location = System::Drawing::Point(15, 54);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(363, 139);
			this->panel1->TabIndex = 2;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(390, 209);
			this->Controls->Add(this->panel1);
			this->Controls->Add(this->comboBox1);
			this->Controls->Add(this->label1);
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"演示创建图形和文字组合框";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
//显示选择的颜色
private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
	if(this->comboBox1->SelectedIndex>=0)
	{
		SolidBrush^ MyBrush=(SolidBrush^)(this->comboBox1->SelectedItem);
		this->panel1->BackColor=MyBrush->Color;
	}
}
//绘制组合框列表项
private: System::Void comboBox1_DrawItem(System::Object^  sender, System::Windows::Forms::DrawItemEventArgs^  e) {
 	ComboBox^ MyCombox=(ComboBox^)sender;
	if(e->Index==-1)
		return;
	if(sender==nullptr)
		return;
	SolidBrush^ MyBrush=(SolidBrush^)MyCombox->Items[e->Index];
	Graphics^ g=e->Graphics;
	//如果已经进行选择,则绘制正确的背景颜色和聚集框
	e->DrawBackground();
	e->DrawFocusRectangle();
	//绘制颜色的预览框
	Rectangle MyRect=e->Bounds;
	MyRect.Offset(2,2);
	MyRect.Width=50;
	MyRect.Height-=4;
	g->DrawRectangle(gcnew Pen(e->ForeColor),MyRect);
	//获取选定颜色的相应画刷对象,并填充预览框
	MyRect.Offset(1,1);
	MyRect.Width-=2;
	MyRect.Height-=2;
	g->FillRectangle(MyBrush,MyRect);
	//绘制选定颜色的名称
	g->DrawString(MyBrush->Color.Name->ToString(),Font,gcnew SolidBrush(e->ForeColor),e->Bounds.X+60,e->Bounds.Y+1);

			 }
 //设置组合框列表项宽
private: System::Void comboBox1_MeasureItem(System::Object^  sender, System::Windows::Forms::MeasureItemEventArgs^  e) {
 e->ItemHeight=this->comboBox1->ItemHeight-2;
}
//在组合框中增加列表项
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
    this->comboBox1->Items->Add(Brushes::Cyan);
	this->comboBox1->Items->Add(Brushes::DarkSalmon);
	this->comboBox1->Items->Add(Brushes::Gray);
	this->comboBox1->Items->Add(Brushes::Green);
	this->comboBox1->Items->Add(Brushes::AliceBlue);
	this->comboBox1->Items->Add(Brushes::Black);
	this->comboBox1->Items->Add(Brushes::Blue);
	this->comboBox1->Items->Add(Brushes::Chocolate);
	this->comboBox1->Items->Add(Brushes::Pink);
	this->comboBox1->Items->Add(Brushes::Red);
	this->comboBox1->Items->Add(Brushes::LightBlue);
	this->comboBox1->Items->Add(Brushes::Brown);
	this->comboBox1->Items->Add(Brushes::DodgerBlue);
	this->comboBox1->Items->Add(Brushes::MediumPurple);
	this->comboBox1->Items->Add(Brushes::White);
	this->comboBox1->Items->Add(Brushes::Yellow);
}
};
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?