目录
1.实验需求分析
1.1实验目的
- 熟悉GUI 编程方法
- 掌握常用控件的使用方法
- 掌握控件的属性、事件编程机制
1.2实验要求
1. 编写一个多文档界面交互程序。编写一个矩阵运算类,实现矩阵的加、减、乘、求逆运算。
2. 设计1个主窗体、4个子窗体、登录对话框、关于对话框。4个子窗体用于测绘计算。
3. 要求有登录对话框,输入用户名和密码进行验证,验证成功后显示主窗体。
4. 主窗体有系统(打开、退出功能菜单项)、窗体(子窗体的平铺等操作菜单项)和帮助(关于、帮助菜单项)菜单。
2.实验原理
通过两点距离及方位角计算原理,实验二转换和交会原理计算,以及矩阵的加减乘除等原理编写。
3.实验代码
3.1.登录界面
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 图形用户界面交互
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Delay(double delayTime)
{
DateTime now = DateTime.Now;
double s;
do
{
TimeSpan spand = DateTime.Now - now;
s = spand.Milliseconds; //Milliseconds是指以毫秒计数
Application.DoEvents();
}
while (s < delayTime);
}
public class admin
{
public static int count = 7;
public static string[] zh = new string[8] { "1,2" };
public static string[] mm = new string[8] { "1,2" };
}
private void button1_Click(object sender, EventArgs e)
{
string zhanghao, mima;
zhanghao = 账号.Text;
zhanghao = zhanghao.Trim();
mima = 密码.Text;
mima = mima.Trim();
for (int i=0;i<admin.count+2;i++)
{
if (i < admin.count + 1)
{
if (zhanghao == admin.zh[i] && mima == admin.mm[i])
{
正在登陆 b = new 正在登陆();
b.Show();
Delay(500);
b.Close();
主界面 f = new 主界面();
f.Show();
this.Hide();
break;
}
}
if(i==admin.count+1)
MessageBox.Show("账号或密码有误");
}
}
private void button2_Click(object sender, EventArgs e)
{
账号.Text = "";
密码.Text = "";
账号.Focus();
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = 账号;
}
}
}
3.2正在登录界面
namespace 图形用户界面交互
{
public partial class 正在登陆 : Form
{
public 正在登陆()
{
InitializeComponent();
}
}
}
3.3主界面
namespace 图形用户界面交互
{
public partial class 主界面 : Form
{
private void SetBtnStyle(Button btn)
{
btn.FlatStyle = FlatStyle.Flat;//样式
btn.BackColor = Color.Transparent;//去背景
btn.FlatAppearance.BorderSize = 0;//去边线
btn.FlatAppearance.MouseOverBackColor = Color.Transparent;//鼠标经过
btn.FlatAppearance.MouseDownBackColor = Color.Transparent;//鼠标按下
}
public 主界面()
{
InitializeComponent();
SetBtnStyle(button1);
SetBtnStyle(button2);
SetBtnStyle(button3);
SetBtnStyle(button4);
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
确认更换账户 f = new 确认更换账户();
f.Show();
this.Hide();
}
private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 帮助ToolStripMenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("有问题找QQ3");
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}
private void 更换账户ToolStripMenuItem_Click(object sender, EventArgs e)
{
确认更换账户 f = new 确认更换账户();
f.Show();
this.Hide();
}
private void 退出应用ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void 帮助ToolStripMenuItem3_Click(object sender, EventArgs e)
{
MessageBox.Show("有问题找1");
}
private void 关于ToolStripMenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}
private void button1_Click(object sender, EventArgs e)
{
坐标基本计算 f = new 坐标基本计算();
f.Show();
this.Hide();
}
private void 坐标基本计算ToolStripMenuItem2_Click(object sender, EventArgs e)
{
坐标基本计算 f = new 坐标基本计算();
f.Show();
this.Hide();
}
private void 坐标基本计算ToolStripMenuItem3_Click(object sender, EventArgs e)
{
坐标基本计算 f = new 坐标基本计算();
f.Show();
this.Hide();
}
private void button1_MouseHover(object sender, EventArgs e)
{
Button button_1 = sender as Button;
button1.FlatAppearance.BorderSize = 1;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
Button button_1 = sender as Button;
button1.FlatAppearance.BorderSize = 0;
}
private void button2_MouseHover(object sender, EventArgs e)
{
Button button_2 = sender as Button;
button2.FlatAppearance.BorderSize = 1;
}
private void button2_MouseLeave(object sender, EventArgs e)
{
Button button_2 = sender as Button;
button2.FlatAppearance.BorderSize = 0;
}
private void button3_MouseHover(object sender, EventArgs e)
{
Button button_3 = sender as Button;
button3.FlatAppearance.BorderSize = 1;
}
private void button3_MouseLeave(object sender, EventArgs e)
{
Button button_3 = sender as Button;
button3.FlatAppearance.BorderSize = 0;
}
private void button4_MouseHover(object sender, EventArgs e)
{
Button button_4 = sender as Button;
button4.FlatAppearance.BorderSize = 1;
}
private void button4_MouseLeave(object sender, EventArgs e)
{
Button button_4 = sender as Button;
button4.FlatAppearance.BorderSize = 0;
}
private void button2_Click(object sender, EventArgs e)
{
交会运算主界面 f = new 交会运算主界面();
f.Show();
this.Hide();
}
private void button3_Click(object sender, EventArgs e)
{
角度转换 f = new 角度转换();
f.Show();
this.Hide();
}
private void button4_Click(object sender, EventArgs e)
{
矩阵运算主界面 f = new 矩阵运算主界面();
f.Show();
this.Close();
}
private void 交会计算ToolStripMenuItem3_Click(object sender, EventArgs e)
{
交会运算主界面 f = new 交会运算主界面();
f.Show();
this.Hide();
}
private void 角度转换ToolStripMenuItem3_Click(object sender, EventArgs e)
{
角度转换 f = new 角度转换();
f.Show();
this.Hide();
}
private void 矩阵运算ToolStripMenuItem3_Click(object sender, EventArgs e)
{
矩阵运算主界面 f = new 矩阵运算主界面();
f.Show();
this.Close();
}
private void 交会计算ToolStripMenuItem2_Click(object sender, EventArgs e)
{
交会运算主界面 f = new 交会运算主界面();
f.Show();
this.Hide();
}
private void 角度转换ToolStripMenuItem2_Click(object sender, EventArgs e)
{
角度转换 f = new 角度转换();
f.Show();
this.Hide();
}
private void 矩阵运算ToolStripMenuItem2_Click(object sender, EventArgs e)
{
矩阵运算主界面 f = new 矩阵运算主界面();
f.Show();
this.Close();
}
}
3.4坐标计算
namespace 图形用户界面交互
{
public partial class 坐标基本计算 : Form
{
public 坐标基本计算()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
主界面 f = new 主界面();
f.Show();
this.Close();
}
public static double DuD(double x)//度转度分秒
{
int a;
double b, c, d;
a = (int)x;
b = (int)((x - a) * 60);
d = ((x - a) * 60);
c = (d - b) * 60;
x = a + b / 100.0 + c / 10000;
return x;
}
public static double FWJ(double x1, double y1, double x2, double y2)//方位角计算
{
double A, R;
double azimuth = 0;
double deltax = x2 - x1;
double deltay = y2 - y1;
if (deltax == 0 && deltay == 0)
{
A = -1;
return A;
}
else if (deltax == 0)
{
if (deltay > 0)
{
azimuth = Math.PI / 2.0;
}
else
azimuth = 1.5 * Math.PI;
}
else
{
R = Math.Atan(deltay / deltax);
if (deltax <= 0)
azimuth = R + Math.PI;
else if (deltay <= 0)
azimuth = R + 2 * Math.PI;
else
azimuth = R;
}
A = azimuth * (180 / Math.PI);//弧度转度
A = DuD(A);//度转度分秒
return A;
}
private void button1_Click(object sender, EventArgs e)
{
double x1, y1, x2, y2,S,A;
if (Y1.Text == "" || Y2.Text == "" || X1.Text == "" || X2.Text == "")
MessageBox.Show("请检查是否将数据输入完整");
else
{
x1 = Convert.ToDouble(X1.Text);
y1 = Convert.ToDouble(Y1.Text);
x2 = Convert.ToDouble(X2.Text);
y2 = Convert.ToDouble(Y2.Text);
S = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
A = FWJ(x1, y1, x2, y2);
string S1 = S.ToString("f3");
string A1 = A.ToString("f5");
MessageBox.Show("距离=" + S1 + "\n方位角=" + A1 + "(DD.MMSSS)");
}
}
}
}