前言:
C# 的使用,似乎在机器视觉的集成项目里面变得越来越多了,笔者无奈的熟悉起来这个环境,不过,据说,如果做界面的设计,C#的使用是异常方便的,有的不行了,做几个实例看看?
今天的实例,就是我要从以前的应用里面加一个新的检测的项目,那么,肯定不能全面瘫倒主菜单去选吧,所以,应该是从一个下拉菜单里面去选择,于是就有了本文的标题的一个小定义:
项目环境:
Windwos10 64位
Microsoft Visual Studio Community 2019
版本 16.11.22
1 先要加一个新的窗口:
在菜单里面选取【项目】,然后下拉菜单里面有 【添加,窗体、控件、类等等各种添加的选项】
我们这里添加一下新的窗体,自然,选择,【添加窗体】
这里好像选项很多,笔者选择【窗体】,然后,下面有一个名称设定,这里可以改一下,点击【添加】就有如下:
编译器这时候不仅仅编译好了窗口,还会自动生成一些相关的代码和类:
namespace VisionSystem
{
partial class frm3Ddetect
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// frm3Ddetect
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Name = "frm3Ddetect";
this.Text = "frm3Ddetect";
this.Load += new System.EventHandler(this.frm3Ddetect_Load);
this.ResumeLayout(false);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace VisionSystem
{
public partial class frm3Ddetect : Form
{
public frm3Ddetect()
{
InitializeComponent();
}
private void frm3Ddetect_Load(object sender, EventArgs e)
{
}
}
}
2 添加菜单选择入口:
双击原来的主窗口:
这时候,自动进入窗口的编辑,如下:
这里直接在原来的主菜单里面,我们在【请在此处键入】这里【单击】就可以输入,新的菜单选项。【案,这里确实C#确实比较方便】
【笔者,在这里添加(3D检测),然后鼠标下移,输入(打开检测窗口)】这些都非常方便。
3 将新建菜单操作绑定到新建的窗口:
咱们现在,刚才编辑的菜单里面,双击:
这时候会自动进入编辑窗口:
【案,笔者这里要编辑如下代码,然后,在点击菜单的时候,就能够自动装载我们设计的窗体了】
private void 打开检测窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
// Create a new form
frm3Ddetect frm_3Ddetect = new frm3Ddetect();
// Show the new form
frm_3Ddetect.Show();
}
4 编译测试:
实验完成如上: