Переименованы кнопки по стандарту языка, добавлен метод для изменения размеров формы

This commit is contained in:
Nikita Potapov 2022-10-15 14:07:57 +04:00
parent 6023d417fb
commit 3eabfb5fd0
3 changed files with 166 additions and 121 deletions

View File

@ -16,7 +16,7 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
/// <summary> /// <summary>
/// Класс-сущность /// Класс-сущность
/// </summary> /// </summary>
public EntityBoat Boat { protected set; get; } public EntityBoat Boat { private set; get; }
/// <summary> /// <summary>
/// Левая координата отрисовки лодки /// Левая координата отрисовки лодки
/// </summary> /// </summary>
@ -140,5 +140,29 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
g.DrawEllipse(Pens.Black, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8, g.DrawEllipse(Pens.Black, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8,
_boatWidth / 2, _boatHeight - _boatHeight / 4); _boatWidth / 2, _boatHeight - _boatHeight / 4);
} }
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _boatWidth || _pictureHeight <= _boatHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _boatWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _boatWidth;
}
if (_startPosY + _boatHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _boatHeight;
}
}
} }
} }

177
FormBoat.Designer.cs generated
View File

@ -29,30 +29,31 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.btn_new = new System.Windows.Forms.Button(); this.ButtonCreate = new System.Windows.Forms.Button();
this.statusStrip = new System.Windows.Forms.StatusStrip(); this.statusStrip = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel();
this.groupBox = new System.Windows.Forms.GroupBox(); this.groupBox = new System.Windows.Forms.GroupBox();
this.btn_up = new System.Windows.Forms.Button(); this.ButtonUp = new System.Windows.Forms.Button();
this.btn_down = new System.Windows.Forms.Button(); this.ButtonDown = new System.Windows.Forms.Button();
this.btn_left = new System.Windows.Forms.Button(); this.ButtonLeft = new System.Windows.Forms.Button();
this.btn_right = new System.Windows.Forms.Button(); this.ButtonRight = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox(); this.pictureBoxBoat = new System.Windows.Forms.PictureBox();
this.statusStrip.SuspendLayout(); this.statusStrip.SuspendLayout();
this.groupBox.SuspendLayout(); this.groupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// btn_new // ButtonCreate
// //
this.btn_new.Location = new System.Drawing.Point(12, 344); this.ButtonCreate.Location = new System.Drawing.Point(10, 277);
this.btn_new.Name = "btn_new"; this.ButtonCreate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.btn_new.Size = new System.Drawing.Size(75, 63); this.ButtonCreate.Name = "ButtonCreate";
this.btn_new.TabIndex = 10; this.ButtonCreate.Size = new System.Drawing.Size(118, 28);
this.btn_new.Text = "Создать"; this.ButtonCreate.TabIndex = 10;
this.btn_new.Click += new System.EventHandler(this.btn_new_Click); this.ButtonCreate.Text = "Создать";
this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
// //
// statusStrip // statusStrip
// //
@ -61,121 +62,115 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
this.toolStripStatusLabelSpeed, this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight, this.toolStripStatusLabelWeight,
this.toolStripStatusLabelColor}); this.toolStripStatusLabelColor});
this.statusStrip.Location = new System.Drawing.Point(0, 415); this.statusStrip.Location = new System.Drawing.Point(0, 309);
this.statusStrip.Name = "statusStrip"; this.statusStrip.Name = "statusStrip";
this.statusStrip.Size = new System.Drawing.Size(1074, 26); this.statusStrip.Padding = new System.Windows.Forms.Padding(1, 0, 12, 0);
this.statusStrip.Size = new System.Drawing.Size(940, 22);
this.statusStrip.TabIndex = 8; this.statusStrip.TabIndex = 8;
this.statusStrip.Text = "statusStrip"; this.statusStrip.Text = "statusStrip";
// //
// toolStripStatusLabelSpeed // toolStripStatusLabelSpeed
// //
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(80, 20); this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(65, 17);
this.toolStripStatusLabelSpeed.Text = "Скорость: "; this.toolStripStatusLabelSpeed.Text = "Скорость: ";
// //
// toolStripStatusLabelWeight // toolStripStatusLabelWeight
// //
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(40, 20); this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(32, 17);
this.toolStripStatusLabelWeight.Text = "Вес: "; this.toolStripStatusLabelWeight.Text = "Вес: ";
// //
// toolStripStatusLabelColor // toolStripStatusLabelColor
// //
this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor"; this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor";
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(49, 20); this.toolStripStatusLabelColor.Size = new System.Drawing.Size(39, 17);
this.toolStripStatusLabelColor.Text = "Цвет: "; this.toolStripStatusLabelColor.Text = "Цвет: ";
// //
// groupBox // groupBox
// //
this.groupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.groupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.groupBox.Controls.Add(this.btn_up); this.groupBox.Controls.Add(this.ButtonUp);
this.groupBox.Controls.Add(this.btn_down); this.groupBox.Controls.Add(this.ButtonDown);
this.groupBox.Controls.Add(this.btn_left); this.groupBox.Controls.Add(this.ButtonLeft);
this.groupBox.Controls.Add(this.btn_right); this.groupBox.Controls.Add(this.ButtonRight);
this.groupBox.Location = new System.Drawing.Point(923, 325); this.groupBox.Location = new System.Drawing.Point(808, 244);
this.groupBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.groupBox.Name = "groupBox"; this.groupBox.Name = "groupBox";
this.groupBox.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); this.groupBox.Size = new System.Drawing.Size(122, 66);
this.groupBox.Size = new System.Drawing.Size(139, 88);
this.groupBox.TabIndex = 9; this.groupBox.TabIndex = 9;
this.groupBox.TabStop = false; this.groupBox.TabStop = false;
// //
// btn_up // ButtonUp
// //
this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.ButtonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_up.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_up1; this.ButtonUp.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_up1;
this.btn_up.Location = new System.Drawing.Point(51, 0); this.ButtonUp.Location = new System.Drawing.Point(45, 0);
this.btn_up.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonUp.Name = "ButtonUp";
this.btn_up.Name = "btn_up"; this.ButtonUp.Size = new System.Drawing.Size(35, 30);
this.btn_up.Size = new System.Drawing.Size(40, 40); this.ButtonUp.TabIndex = 1;
this.btn_up.TabIndex = 1; this.ButtonUp.UseVisualStyleBackColor = true;
this.btn_up.UseVisualStyleBackColor = true; this.ButtonUp.Click += new System.EventHandler(this.ButtonMove_Click);
this.btn_up.Click += new System.EventHandler(this.btn_move_Click);
// //
// btn_down // ButtonDown
// //
this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.ButtonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_down.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_down1; this.ButtonDown.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_down1;
this.btn_down.Location = new System.Drawing.Point(51, 42); this.ButtonDown.Location = new System.Drawing.Point(45, 32);
this.btn_down.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonDown.Name = "ButtonDown";
this.btn_down.Name = "btn_down"; this.ButtonDown.Size = new System.Drawing.Size(35, 30);
this.btn_down.Size = new System.Drawing.Size(40, 40); this.ButtonDown.TabIndex = 2;
this.btn_down.TabIndex = 2; this.ButtonDown.Text = " ";
this.btn_down.Text = " "; this.ButtonDown.UseVisualStyleBackColor = true;
this.btn_down.UseVisualStyleBackColor = true; this.ButtonDown.Click += new System.EventHandler(this.ButtonMove_Click);
this.btn_down.Click += new System.EventHandler(this.btn_move_Click);
// //
// btn_left // ButtonLeft
// //
this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.ButtonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_left.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_left1; this.ButtonLeft.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_left1;
this.btn_left.Location = new System.Drawing.Point(5, 42); this.ButtonLeft.Location = new System.Drawing.Point(4, 32);
this.btn_left.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonLeft.Name = "ButtonLeft";
this.btn_left.Name = "btn_left"; this.ButtonLeft.Size = new System.Drawing.Size(35, 30);
this.btn_left.Size = new System.Drawing.Size(40, 40); this.ButtonLeft.TabIndex = 3;
this.btn_left.TabIndex = 3; this.ButtonLeft.UseVisualStyleBackColor = true;
this.btn_left.UseVisualStyleBackColor = true; this.ButtonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
this.btn_left.Click += new System.EventHandler(this.btn_move_Click);
// //
// btn_right // ButtonRight
// //
this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.ButtonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btn_right.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_right1; this.ButtonRight.Image = global::PIbd_21_Potapov_N.S._Catamaran_Base.Properties.Resources.arrow_right1;
this.btn_right.Location = new System.Drawing.Point(97, 44); this.ButtonRight.Location = new System.Drawing.Point(85, 33);
this.btn_right.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.ButtonRight.Name = "ButtonRight";
this.btn_right.Name = "btn_right"; this.ButtonRight.Size = new System.Drawing.Size(35, 30);
this.btn_right.Size = new System.Drawing.Size(40, 40); this.ButtonRight.TabIndex = 4;
this.btn_right.TabIndex = 4; this.ButtonRight.UseVisualStyleBackColor = true;
this.btn_right.UseVisualStyleBackColor = true; this.ButtonRight.Click += new System.EventHandler(this.ButtonMove_Click);
this.btn_right.Click += new System.EventHandler(this.btn_move_Click);
// //
// pictureBox // pictureBoxBoat
// //
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxBoat.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBoxBoat.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.pictureBoxBoat.Name = "pictureBoxBoat";
this.pictureBox.Name = "pictureBox"; this.pictureBoxBoat.Size = new System.Drawing.Size(940, 331);
this.pictureBox.Size = new System.Drawing.Size(1074, 441); this.pictureBoxBoat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxBoat.TabIndex = 0;
this.pictureBox.TabIndex = 0; this.pictureBoxBoat.TabStop = false;
this.pictureBox.TabStop = false; this.pictureBoxBoat.Resize += new System.EventHandler(this.PictureBoxBoat_Resize);
// //
// FormCatamaran // FormBoat
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1074, 441); this.ClientSize = new System.Drawing.Size(940, 331);
this.Controls.Add(this.groupBox); this.Controls.Add(this.groupBox);
this.Controls.Add(this.btn_new); this.Controls.Add(this.ButtonCreate);
this.Controls.Add(this.statusStrip); this.Controls.Add(this.statusStrip);
this.Controls.Add(this.pictureBox); this.Controls.Add(this.pictureBoxBoat);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormBoat";
this.Name = "FormCatamaran";
this.Text = "Лодка"; this.Text = "Лодка";
this.statusStrip.ResumeLayout(false); this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout(); this.statusStrip.PerformLayout();
this.groupBox.ResumeLayout(false); this.groupBox.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -183,12 +178,12 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
#endregion #endregion
private System.Windows.Forms.PictureBox pictureBox; private System.Windows.Forms.PictureBox pictureBoxBoat;
private System.Windows.Forms.Button btn_up; private System.Windows.Forms.Button ButtonUp;
private System.Windows.Forms.Button btn_down; private System.Windows.Forms.Button ButtonDown;
private System.Windows.Forms.Button btn_left; private System.Windows.Forms.Button ButtonLeft;
private System.Windows.Forms.Button btn_right; private System.Windows.Forms.Button ButtonRight;
private System.Windows.Forms.Button btn_new; private System.Windows.Forms.Button ButtonCreate;
private System.Windows.Forms.StatusStrip statusStrip; private System.Windows.Forms.StatusStrip statusStrip;
private System.Windows.Forms.GroupBox groupBox; private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelSpeed; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelSpeed;

View File

@ -12,68 +12,94 @@ namespace PIbd_21_Potapov_N.S._Catamaran_Base
{ {
public partial class FormBoat : System.Windows.Forms.Form public partial class FormBoat : System.Windows.Forms.Form
{ {
DrawingBoat boatDrawObj; DrawingBoat _boat;
public FormBoat() public FormBoat()
{ {
InitializeComponent(); InitializeComponent();
pictureBox.Image = new Bitmap(pictureBox.Width, pictureBox.Height);
} }
/// <summary>
private void btn_new_Click(object sender, EventArgs e) /// Обработчик нажатия кнопки "Создать"
/// </summary>
private void ButtonCreate_Click(object sender, EventArgs e)
{ {
Random rnd = new Random(); Random rnd = new Random();
boatDrawObj = new DrawingBoat(); _boat = new DrawingBoat();
boatDrawObj.Init(rnd.Next(50, 100), rnd.Next(50, 100), Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))); _boat.Init(
boatDrawObj.SetPosition(rnd.Next(10, 50), rnd.Next(10, 50), pictureBox.Width, pictureBox.Height - statusStrip.Height); rnd.Next(50, 100),
toolStripStatusLabelSpeed.Text = $"Скорость: {boatDrawObj.Boat.Speed}"; rnd.Next(50, 100),
toolStripStatusLabelWeight.Text = $"Вес: {boatDrawObj.Boat.Weight}"; Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))
toolStripStatusLabelColor.Text = $"Цвет: {boatDrawObj.Boat.BodyColor.Name}"; );
RedrawCatamaran(); _boat.SetPosition(
rnd.Next(10, 50),
rnd.Next(10, 50),
pictureBoxBoat.Width,
pictureBoxBoat.Height - statusStrip.Height
);
toolStripStatusLabelSpeed.Text = $"Скорость: {_boat.Boat.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {_boat.Boat.Weight}";
toolStripStatusLabelColor.Text = $"Цвет: {_boat.Boat.BodyColor.Name}";
Draw();
} }
/// <summary>
private void btn_move_Click(object sender, EventArgs e) /// Обработчик нажатия кнопок передвижения
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (boatDrawObj == null) if (_boat == null)
return; return;
string btnName = ((Button)sender).Name; string btnName = ((Button)sender).Name;
switch (btnName) switch (btnName)
{ {
case "btn_up": case "ButtonUp":
{ {
boatDrawObj.MoveTransport(Direction.Up); _boat.MoveTransport(Direction.Up);
} }
break; break;
case "btn_down": case "ButtonDown":
{ {
boatDrawObj.MoveTransport(Direction.Down); _boat.MoveTransport(Direction.Down);
} }
break; break;
case "btn_left": case "ButtonLeft":
{ {
boatDrawObj.MoveTransport(Direction.Left); _boat.MoveTransport(Direction.Left);
} }
break; break;
case "btn_right": case "ButtonRight":
{ {
boatDrawObj.MoveTransport(Direction.Right); _boat.MoveTransport(Direction.Right);
} }
break; break;
default: default:
break; break;
} }
RedrawCatamaran(); Draw();
} }
/// <summary>
private void RedrawCatamaran() /// Метод прорисовки лодки
/// </summary>
private void Draw()
{ {
Graphics g = Graphics.FromImage(pictureBox.Image); Bitmap bmp = new(pictureBoxBoat.Width, pictureBoxBoat.Height);
g.Clear(Color.White); Graphics g = Graphics.FromImage(bmp);
boatDrawObj.DrawTransport(g); _boat?.DrawTransport(g);
pictureBox.Invalidate(); pictureBoxBoat.Image = bmp;
}
/// <summary>
/// Изменение размеров формы
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PictureBoxBoat_Resize(object sender, EventArgs e)
{
_boat?.ChangeBorders(pictureBoxBoat.Width, pictureBoxBoat.Height - statusStrip.Height);
Draw();
} }
} }
} }