自定义控件界面及控件如下
控件列表
using Cognex.VisionPro;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro.ToolGroup;
using System.Security.RightsManagement;
using System.Windows.Forms.VisualStyles;
namespace VMPro2024._08.UserControl8
{
public partial class DisplayControl : UserControl
{
#region 申明变量
public DisplayControl displayControl;
public TitleForm titleForm;
#endregion
DisplayControl _displayControl;public DisplayControl(){InitializeComponent();this.displayControl = _displayControl; }public void IniDisplay(){this.cogDisplayStatusBarV21.Display = this.cogRecordDisplay1;this.cogDisplayToolbarV21.Display = this.cogRecordDisplay1;this.cogRecordDisplay1.AutoFit = true;}//toolstrippublic CogRecordDisplay cogRecordDisplay{set { this.cogRecordDisplay1 = value; }get { return this.cogRecordDisplay1; }}#region 实时采集图像/// <summary>/// 第一个窗体中的toolstrip事件/// </summary>public ToolStripStatusLabel toolStripStatusLabel1{get{return this.相机实时_toolStripLabel;}}/// <summary>/// 第二个窗体中的toolstrip事件/// </summary>public ToolStripStatusLabel toolStripStatusLabel2{get{return this.相机实时_toolStripLabel;}}/// <summary>/// 第三个窗体中的toolstrip事件/// </summary>public ToolStripStatusLabel toolStripStatusLabel3{get{return this.相机实时_toolStripLabel;}}/// <summary>/// 第四个窗体中的toolstrip事件/// </summary>public ToolStripStatusLabel toolStripStatusLabel4{get{return this.相机实时_toolStripLabel;}}/// <summary>/// 第五个窗体中的toolstrip事件/// </summary>public ToolStripStatusLabel toolStripStatusLabel5{get{return this.相机实时_toolStripLabel;}}/// <summary>/// 第六个窗体中的toolstrip事件/// </summary>public ToolStripStatusLabel toolStripStatusLabel6{get{return this.相机实时_toolStripLabel;}}#endregion#region 相机单次采图/// <summary>/// 第一个子窗体手动拍照按钮/// </summary>public ToolStripStatusLabel toolStripStatusLabel拍照1{get{return this.相机拍照_toolStatusLabel;}}/// <summary>/// 第二个子窗体手动拍照按钮/// </summary>public ToolStripStatusLabel toolStripStatusLabel拍照2{get{return this.相机拍照_toolStatusLabel;}}/// <summary>/// 第三个子窗体手动拍照按钮/// </summary>public ToolStripStatusLabel toolStripStatusLabel拍照3{get{return this.相机拍照_toolStatusLabel;}}/// <summary>/// 第四个子窗体手动拍照按钮/// </summary>public ToolStripStatusLabel toolStripStatusLabel拍照4{get{return this.相机拍照_toolStatusLabel;}}/// <summary>/// 第五个子窗体手动拍照按钮/// </summary>public ToolStripStatusLabel toolStripStatusLabel拍照5{get{return this.相机拍照_toolStatusLabel;}}/// <summary>/// 第六个子窗体手动拍照按钮/// </summary>public ToolStripStatusLabel toolStripStatusLabel拍照6{get{return this.相机拍照_toolStatusLabel;}}#endregion#region 放大缩小/// <summary>/// 放大/// </summary>public ToolStripDropDownButton 放大Button1{get{return this.放大Button;}}public ToolStripDropDownButton 缩小Button1{get{return this.缩小Button;}}#endregion}
}
窗体设置按钮调用另一个子窗体截图及代码,本窗体是3行2列!
控件:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VMPro2024._08.WinForm
{
public partial class WindFormSetting : Form
{
private int _row;
public int cameraCount = 0;public int camerrow = 0;//public int cameraCount = 0; public int Row{get { return _row; }set { _row = value; }}private int _column;public int Column{get { return _column; }set { _column = value; }}public WindFormSetting(int row, int column, Form parent){this.Row = row;this.Column = column;InitializeComponent();this.StartPosition = FormStartPosition.Manual;this.Location = new Point(parent.Location.X + (parent.Size.Width / 2) - (this.Size.Width / 2),parent.Location.Y + (parent.Size.Height / 2) - (this.Size.Height));this.btnSetting.Click += new EventHandler(btnSetting_Click);//numRow = new NumericUpDown();//numColumn = new NumericUpDown();//mCamerCount(cameraCount);}public WindFormSetting(){}private void WindFormSetting_Load(object sender, EventArgs e){this.numRow.Value = this.Row;this.numColumn.Value = this.Column;}private void btnSetting_Click(object sender, EventArgs e){this.Row = Convert.ToInt32(this.numRow.Value);this.Column = Convert.ToInt32(this.numColumn.Value);this.Close();}/// <summary>/// 计算相机个数/// </summary>/// <param name="count"></param>//public int mCamerCount(int count)//{//用于计算相机个数//camerrow = Convert.ToInt32(this.numRow.Value);//camercolumn = Convert.ToInt32(this.numColumn.Value);//cameraCount = camerrow * camercolumn;//计算相机个数//return cameraCount;//}//设置行private void numRow_MouseDown(object sender, MouseEventArgs e){if (this.numRow.Value <= 3){this.numRow.Value++;}}private void numRow_MouseUp(object sender, MouseEventArgs e){if (this.numRow.Value >= 2){this.numRow.Value--;}}//设置列private void numColumn_MouseDown(object sender, MouseEventArgs e){if (this.numColumn.Value <= 4){this.numColumn.Value++;}}private void numColumn_MouseUp(object sender, MouseEventArgs e){if (this.numColumn.Value >= 2){this.numColumn.Value--;}}
}
}
主窗体
主窗体代码:
#region 窗体个数变量
public DisplayControl[] displayControls = new DisplayControl[10];//显示控件
bool bEnlarge = false;//判断是否双击激活 cogRecordDisplay_DoubleClick事件
#endregion
//设置窗体个数
private void 窗体设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
WindFormSetting windFormSetting = new WindFormSetting(WindowConfig.gOnly.displayRow, WindowConfig.gOnly.displayColumn, this);
windFormSetting.ShowDialog();
DialogResult result = MessageBox.Show(“是否重设显示个数”, “选择按钮”, MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
WindowConfig.gOnly.displayRow = windFormSetting.Row;
WindowConfig.gOnly.displayColumn = windFormSetting.Column;
WindowConfig.SaveConfig();
//需要添加窗体修改Log
IniDisplayCount(windFormSetting.Row, windFormSetting.Column, true);LoadConfigXml(cameraCount);//计算相机个数WindowConfig.gOnly.cameraCount = Convert.ToInt32(cameraCount);//存储相机个数MessageBox.Show(cameraCount.ToString());
}
windFormSetting.Dispose();//窗体销毁
}
//初始化窗口 设置tableLayoutPanel的相关属性
private void IniDisplayCount(int RowCount, int ColumnCount, bool bChange)
{
int displayCount = 0;
tableLayoutPanel2.RowStyles.Clear();
tableLayoutPanel2.ColumnStyles.Clear();
tableLayoutPanel2.RowCount = RowCount;
tableLayoutPanel2.ColumnCount = ColumnCount;
for (int i = 0; i < tableLayoutPanel2.RowCount; i++)
{tableLayoutPanel2.RowStyles.Add(new ColumnStyle(SizeType.Percent, 50));
}for (int i = 0; i < tableLayoutPanel2.ColumnCount; i++)
{tableLayoutPanel2.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
}if (bChange)
{for (int i = 0; i < displayControls.Length; i++){if (displayControls[i] != null){if (i == 0)displayControls[i].cogRecordDisplay.DoubleClick -= cogRecordDisplay1_DoubleClick;if (i == 1)displayControls[i].cogRecordDisplay.DoubleClick -= cogRecordDisplay2_DoubleClick;if (i == 2)displayControls[i].cogRecordDisplay.DoubleClick -= cogRecordDisplay3_DoubleClick;if (i == 3)displayControls[i].cogRecordDisplay.DoubleClick -= cogRecordDisplay4_DoubleClick;if (i == 4)displayControls[i].cogRecordDisplay.DoubleClick -= cogRecordDisplay5_DoubleClick;if (i == 5)displayControls[i].cogRecordDisplay.DoubleClick -= cogRecordDisplay6_DoubleClick;displayControls[i].Dispose();displayControls[i] = null;}}
}
for (int i = 0; i < RowCount; i++)
{for (int j = 0; j < ColumnCount; j++){displayControls[displayCount] = new DisplayControl();displayControls[displayCount].IniDisplay();displayControls[displayCount].Dock = DockStyle.Fill;tableLayoutPanel2.Controls.Add(displayControls[displayCount], j, i);if (i == 0 && j == 0)displayControls[displayCount].cogRecordDisplay.DoubleClick += cogRecordDisplay1_DoubleClick;if (i == 0 && j == 1)displayControls[displayCount].cogRecordDisplay.DoubleClick += cogRecordDisplay2_DoubleClick;if (i == 1 && j == 0)displayControls[displayCount].cogRecordDisplay.DoubleClick += cogRecordDisplay3_DoubleClick;if (i == 1 && j == 1)displayControls[displayCount].cogRecordDisplay.DoubleClick += cogRecordDisplay4_DoubleClick;if (i == 2 && j == 0)displayControls[displayCount].cogRecordDisplay.DoubleClick += cogRecordDisplay5_DoubleClick;if (i == 2 && j == 1)displayControls[displayCount].cogRecordDisplay.DoubleClick += cogRecordDisplay6_DoubleClick;displayCount++;}
}
}
#region 双击全窗口显示 恢复
private void cogRecordDisplay1_DoubleClick(object sender, EventArgs e)
{
Cognex.VisionPro.CogRecordDisplay recordDisplay = sender as Cognex.VisionPro.CogRecordDisplay;
if (bEnlarge)
{
//再次双击鼠标恢复
bEnlarge = false;
tableLayoutPanel2.Controls.Add(displayControls[0]);displayControls[0].SendToBack();displayControls[0].Parent = tableLayoutPanel2;displayControls[0].Dock = DockStyle.Fill;tableLayoutPanel2.Show();
}
else
{//双击鼠标放大bEnlarge = true;tableLayoutPanel2.Controls.Remove(displayControls[0]);displayControls[0].Parent = this;displayControls[0].BringToFront();displayControls[0].Dock = DockStyle.Fill;tableLayoutPanel2.Hide();
}
}
private void cogRecordDisplay2_DoubleClick(object sender, EventArgs e)
{
Cognex.VisionPro.CogRecordDisplay recordDisplay = sender as Cognex.VisionPro.CogRecordDisplay;
if (bEnlarge)
{
//再次双击鼠标恢复
bEnlarge = false;
tableLayoutPanel2.Controls.Add(displayControls[1]);displayControls[1].SendToBack();displayControls[1].Parent = tableLayoutPanel2;displayControls[1].Dock = DockStyle.Fill;tableLayoutPanel2.Show();
}
else
{//双击鼠标放大bEnlarge = true;tableLayoutPanel2.Controls.Remove(displayControls[1]);displayControls[1].Parent = this;displayControls[1].BringToFront();displayControls[1].Dock = DockStyle.Fill;tableLayoutPanel2.Hide();
}
}
private void cogRecordDisplay3_DoubleClick(object sender, EventArgs e)
{
Cognex.VisionPro.CogRecordDisplay recordDisplay = sender as Cognex.VisionPro.CogRecordDisplay;
if (bEnlarge)
{
//再次双击鼠标恢复
bEnlarge = false;
tableLayoutPanel2.Controls.Add(displayControls[2]);displayControls[2].SendToBack();displayControls[2].Parent = tableLayoutPanel2;displayControls[2].Dock = DockStyle.Fill;tableLayoutPanel2.Show();
}
else
{//双击鼠标放大bEnlarge = true;tableLayoutPanel2.Controls.Remove(displayControls[2]);displayControls[2].Parent = this;displayControls[2].BringToFront();displayControls[2].Dock = DockStyle.Fill;tableLayoutPanel2.Hide();
}
}
private void cogRecordDisplay4_DoubleClick(object sender, EventArgs e)
{
Cognex.VisionPro.CogRecordDisplay recordDisplay = sender as Cognex.VisionPro.CogRecordDisplay;
if (bEnlarge)
{
//再次双击鼠标恢复
bEnlarge = false;
tableLayoutPanel2.Controls.Add(displayControls[3]);displayControls[3].SendToBack();displayControls[3].Parent = tableLayoutPanel2;displayControls[3].Dock = DockStyle.Fill;tableLayoutPanel2.Show();
}
else
{//双击鼠标放大bEnlarge = true;tableLayoutPanel2.Controls.Remove(displayControls[3]);displayControls[3].Parent = this;displayControls[3].BringToFront();displayControls[3].Dock = DockStyle.Fill;tableLayoutPanel2.Hide();
}
}
private void cogRecordDisplay5_DoubleClick(object sender, EventArgs e)
{
Cognex.VisionPro.CogRecordDisplay recordDisplay = sender as Cognex.VisionPro.CogRecordDisplay;
if (bEnlarge)
{
//再次双击鼠标恢复
bEnlarge = false;
tableLayoutPanel2.Controls.Add(displayControls[4]);displayControls[4].SendToBack();displayControls[4].Parent = tableLayoutPanel2;displayControls[4].Dock = DockStyle.Fill;tableLayoutPanel2.Show();
}
else
{//双击鼠标放大bEnlarge = true;tableLayoutPanel2.Controls.Remove(displayControls[4]);displayControls[4].Parent = this;displayControls[4].BringToFront();displayControls[4].Dock = DockStyle.Fill;tableLayoutPanel2.Hide();
}
}
private void cogRecordDisplay6_DoubleClick(object sender, EventArgs e)
{
Cognex.VisionPro.CogRecordDisplay recordDisplay = sender as Cognex.VisionPro.CogRecordDisplay;
if (bEnlarge)
{
//再次双击鼠标恢复
bEnlarge = false;
tableLayoutPanel2.Controls.Add(displayControls[5]);displayControls[5].SendToBack();displayControls[5].Parent = tableLayoutPanel2;displayControls[5].Dock = DockStyle.Fill;tableLayoutPanel2.Show();
}
else
{//双击鼠标放大bEnlarge = true;tableLayoutPanel2.Controls.Remove(displayControls[5]);displayControls[5].Parent = this;displayControls[5].BringToFront();displayControls[5].Dock = DockStyle.Fill;tableLayoutPanel2.Hide();
}
}
#endregion