上海海洋大学windows编程技术期末整理

更新时间:2024-01-26 13:29:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验一: VS 环境认识和程序设计初步 1.开发一个控制台应用程序,输出你的姓名和学号 namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

Console.WriteLine(\:zhonghua hong\ Console.WriteLine(\

Console.Read(); } } }

2. 开发一个WINDOWS应用程序,实现加法计算和清除功能

namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

textBox3.Text = (int.Parse(textBox1.Text) + int.Parse(textBox2.Text)).ToString(); }

private void button2_Click(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ }

第 1 页 共 1 页

} }

3.新建一个form窗体作为启动窗体,并在窗体上显示“学号,姓名”

实验二: C#编程语言基础_上机

1. 用控制台应用程序编写一个简单的学生成绩统计系统

成绩:90,80,67,87,92,57,77,79,95,99;级别:优、良、中、差;条件:》=90 80~89 60 ~79 《=59;

用数组存储成绩:循环判断(if、switch、for);输出优、良、中、差所占成绩的百分比(WriteLine);输出成绩排序(从高到低排序) namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

int[] score;

score=new int[10]{90,80,67,87,92,57,77,79,95,99}; int numA=0,numB=0,numC=0,numD=0,num=0; /*for(int i=0;i<10;i++) {

Console.WriteLine (\ }*/

for (int i = 0; i < score.Length;i++ ) {

if(score[i]>=90) numA++; else if(score[i]>=80) numB++; else if(score[i]>=60) numC++; else numD++; }

for (int i=0;i

switch(score[i]/10) {

case 10:

case 9:numA++;break; case 8:numB++;break; case 7:

case 6:numC++;break; default:numD++;break;

第 2 页 共 2 页

} }

Console.WriteLine(\ Console.WriteLine(\ Console.WriteLine(\ Console.WriteLine(\ /*Console.WriteLine (\ for (int i = 0; i < 10; i++) {

Console.WriteLine(\ }*/

for (int i = 1; i < score.Length; i++) {

for (int j = 0; j < score.Length - i; j++) {

if (score[j] < score[j + 1]) {

num = score[j]; score[j] = score[j + 1]; score[j + 1] = num; } } }

Console.WriteLine(\ for (int i = 0; i < 10; i++) {

Console.WriteLine(\ }

Console.ReadKey(); } } }

2. 用windows应用程序设计一个存款计算器 (1)利息计算: 利息 =p*(1+i)N - 本金

(2)算法流程描述:(a)从用户输入界面中分别提取存款年限、存款本金、年利率;(b)依据公式(1)计算利息总额;(c)计算总的帐户金额 ;(4)将所有要求输出的数据拼凑成符合格式要求的字符串,并输出。

namespace WindowsFormsApplication1 {

public partial class Form1 : Form

第 3 页 共 3 页

{

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

float bj = float.Parse(textBox1.Text); float cq = float.Parse(textBox2.Text); float nll= float.Parse(textBox3.Text);

double syl = bj*Math.Pow(1 + 0.01 * nll, cq);

label4.Text = \本金:\存期:\年利率\总收益\syl; } } }

实验四 控制程序设计

1.实现在TextBox输入0-9数值,在MessageBox中输出大写汉字

namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

int sum = int.Parse(textBox1.Text); switch (sum) {

case 0:

MessageBox.Show(\零\ break; case 1:

MessageBox.Show(\壹\ break; case 2:

MessageBox.Show(\贰\

第 4 页 共 4 页

break; case 3:

MessageBox.Show(\叁\ break; case 4:

MessageBox.Show(\肆\ break; case 5:

MessageBox.Show(\伍\ break; case 6:

MessageBox.Show(\陆\ break; case 7:

MessageBox.Show(\柒\ break; case 8:

MessageBox.Show(\捌\ break; case 9:

MessageBox.Show(\玖\ break;

} } } }

2.实现登陆界面利用用户名和密码登陆并跳出相应MessageBox,比如可以将用户名设置为admin和shou,密码与用户名相同。 然后跳出相应MessageBox namespace WindowsFormsApplication2 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

if(comboBox1.SelectedIndex == 0)

第 5 页 共 5 页

{

if(textBox2.Text==\

{MessageBox.Show(\您输入的用户名和密码正确\ } else {

MessageBox.Show(\您输入的用户名和密码错误\ } }

else if (comboBox1.SelectedIndex == 1) {

if (textBox2.Text == \ {

MessageBox.Show(\您输入的用户名和密码正确\ } else {

MessageBox.Show(\您输入的用户名和密码错误\ } } }

3.实现通过在TextBox中输入关键词,查询ListBox中是否有该项名单,如果有用MessageBox告知该内容已查询到,并在ListBox中选中该选项。 namespace WindowsFormsApplication3 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

string inStr = textBox1.Text; string[] strItem; strItem = new string[7];

for(int i=0;i

strItem[i]=listBox1.Items[i].ToString();

第 6 页 共 6 页

}

for (int i = 0; i < strItem.Length; i++) {

if (strItem[i].Contains(inStr)) {

MessageBox.Show(\找到了\《\》\ listBox1.SelectedIndex = i; } } } } }

实验五 字符串与数组

1.移除在richTextBox中输入的字符串中的逗号\namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { }

private void button1_MouseClick(object sender, MouseEventArgs e) {

string str = richTextBox1.Text;

第 7 页 共 7 页

int num = 0, j = 0; char[] chfinal;

chfinal = new char[str.Length]; foreach (char ch in str) {

if (ch != ',') {

chfinal[j] = ch; j++; } }

richTextBox1.Text = new string(chfinal); } } }

2.通过OpenFileDialog选择文件,获得该文件的路径、文件名、后缀名 namespace WindowsFormsApplication2 {

public partial class Form1 : Form {

public Form1() {

第 8 页 共 8 页

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

//openFileDialog1.ShowDialog();(打开文件的另一种方法) OpenFileDialog dlg = new OpenFileDialog(); dlg.ShowDialog();

string fileAll = dlg.FileName; label5.Text = fileAll;

label6.Text=fileAll.Substring(0,fileAll.LastIndexOf(\

label7.Text = fileAll.Substring(fileAll.LastIndexOf(\- fileAll.LastIndexOf(\

label8.Text = fileAll.Substring(fileAll.LastIndexOf(\fileAll.LastIndexOf(\ }

private void Form1_Load(object sender, EventArgs e) { } } }

3.替换文本中的指定字符串

namespace WindowsFormsApplication3 {

第 9 页 共 9 页

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

string str = richTextBox1.Text;

richTextBox1.Text = str.Replace(textBox1.Text, textBox2.Text); } } }

实验六:基于WinForms的上机自测系统(按书上的程序做就可以,后半部分没有) 实现书pp69-87的上机自测系统 namespace selfExam {

public partial class PrimaryExmForm : Form {

public PrimaryExmForm() {

InitializeComponent();

第 10 页 共 10 页

}

int ExamSecond=0;//考试用时

private void timer1_Tick(object sender, EventArgs e) {

ExamSecond++;

this.ExamTime.Text = ExamSecond.ToString(); }

private void btnGrade_Click(object sender, EventArgs e) {

int Score = 0;//总得分 //分别评判每题

if (this.Anwser1.Text == \ if (this.Anwser21A.Checked) Score = Score + 10; if (this.Anwser22C.Checked) Score = Score + 10;

if (this.Anwser23.Text==\ if (this.Anwser24.Text == \ if (this.Anwser3A.Checked && this.Anwser3B.Checked && this.Anwser3C.Checked && !this.Anwser3D.Checked) Score = Score + 10; this.timer1.Enabled = false;//计时停止 this.btnGrade.Enabled = false;//评分锁定

this.TotalScore.Text = Score.ToString();//显示总得分 }

private void btnClose_Click(object sender, EventArgs e)

第 11 页 共 11 页

{

this.Close();//调用Close方法关闭窗体PrimaryExmForm } } }

实验七 窗体和控件程序设计

1.设计一个简易秒表,通过一个文本框,以\的形式显示秒(位数随计数值变化)和毫秒,并且。通过“开始”、“停止”按钮启动或停止计时,通过一个复位按钮复位计数值以便重新开始计数

namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

DateTime time = new DateTime(2014, 11, 5, 0, 0, 0); private void button1_Click(object sender, EventArgs e) {

timer1.Start();

//for (int i = 0; i < 10000; i++) //{

第 12 页 共 12 页

//textBox1.Text = (time.Hour).ToString() + \时\分\秒\ //} }

private void timer1_Tick(object sender, EventArgs e) {

int second = 1; //for (int i = 0; ; i++) //{

time= time.AddSeconds(second);

textBox1.Text = (time.Hour).ToString() + \时\分\秒\ //} }

private void button2_Click(object sender, EventArgs e) {

time = new DateTime(2014, 11, 5, 0, 0, 0);

textBox1.Text = (time.Hour).ToString() + \时\分\+ (time.Second).ToString() + \秒\ timer1.Stop(); } } }

第 13 页 共 13 页

(2)自行设计一个包含3个功能窗体和1个启动窗体的简单应用程序,每个窗体的功能可以自行设计,但至少包含两种窗体控件并有相应的程序设计,所有窗体通过菜单打开、工具按钮关闭。 (Form Fist)

namespace WindowsForms {

public partial class FormFirst : Form {

public FormFirst() {

InitializeComponent(); } } }

(Form Load)

namespace WindowsForms {

public partial class FormLoad : Form {

public FormLoad() {

InitializeComponent(); }

第 14 页 共 14 页

FormFirst frm1 = new FormFirst(); FormSecond frm2 = new FormSecond(); FormThird frm3 = new FormThird();

private void 打开窗体1ToolStripMenuItem_Click(object sender, EventArgs e) {

frm1.Show(); }

private void 打开窗体2ToolStripMenuItem_Click(object sender, EventArgs e) {

frm2.Show(); }

private void 打开窗体3ToolStripMenuItem_Click(object sender, EventArgs e) {

frm3.Show(); }

private void toolStripButton1_Click(object sender, EventArgs e) {

frm1.Close(); }

private void toolStripButton2_Click(object sender, EventArgs e) {

frm2.Close();

第 15 页 共 15 页

}

private void toolStripButton3_Click(object sender, EventArgs e) {

frm3.Close(); } } }

(Form Second)

namespace WindowsForms {

public partial class FormSecond : Form {

public FormSecond() {

InitializeComponent(); } } }

(Form Third)

namespace WindowsForms {

public partial class FormThird : Form

第 16 页 共 16 页

{

public FormThird() {

InitializeComponent(); } } }

实验八---文件读写

本题位于书《C#语言windows程序设计》上第324页实验4

(1).创建一个windows应用程序,其中包含两个TextBox控件(TextBox1,TextBox2)和两个Button控件(Button1),单击Button1(写入),可以字符或字符串格式将TextBox1控件中的文本信息写入到一个文本文件中,单击Button2(读取),可将对应的文本文件中的内容显示在TextBox2。

namespace WindowsFormsApplication1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

OpenFileDialog dlg = new OpenFileDialog(); if ((dlg.ShowDialog()) == DialogResult.OK) {

第 17 页 共 17 页

StreamReader reader = new StreamReader(dlg.FileName); //1.直接读

//richTextBox1.Text = reader.ReadToEnd(); //2.判断

/*while(!reader.EndOfStream) {

richTextBox1.Text += reader.ReadLine(); }*/

//3.reader数组,每次读一个字符 char[] chr = new char[1024]; while (!reader.EndOfStream) {

reader.Read(chr, 0, chr.Length); richTextBox1.Text = new String(chr); }

reader.Close(); } }

private void button2_Click(object sender, EventArgs e) {

/*SaveFileDialog.dlg = new SaveFileDialog(); if ((dlg.ShowDialog()) == DialogResult.OK) {

StreamWriter writer = new StreamWriter(dlg.FileName); writer.Write(richTextBox2.Text); writer.Flush();

第 18 页 共 18 页

writer.Close(); }*/ } } }

(2).创建一个windows应用程序,其中包含一个RichTextBox控件(RichTextBox1)和一个Button控件(Button1),单击Button(读取),可以将一个二进制文件的内容(如一幅图片)显示在RichTextBox1中。 namespace WindowsFormsApplication2 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

OpenFileDialog dlg = new OpenFileDialog(); if ((dlg.ShowDialog()) == DialogResult.OK) {

FileStream fs = new FileStream(dlg.FileName, FileMode.Open); Byte[] bt = new Byte[fs.Length]; fs.Read(bt, 0, bt.Length);

Graphics gs = pictureBox2.CreateGraphics(); Image img = Image.FromStream(fs); gs.DrawImage(img, 0, 0);

第 19 页 共 19 页

fs.Flush(); fs.Close(); } }

private void Form1_Load(object sender, EventArgs e) { }

private void pictureBox2_Click(object sender, EventArgs e) { } } }

实验九---GDI+

1.pp:246 基于鼠标轨迹画线的程序设计 namespace MouseDrawline {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); //创建绘图对象

g = this.pictureBox1.CreateGraphics(); }

第 20 页 共 20 页

//定义画线起点X、Y坐标 int LineStartX = 0; int LineStartY = 0;

//移动并“按下鼠标”才可画线 bool blDrawLine = false; //定义绘图对象 Graphics g;

//初置画线起点以便鼠标移动时画线

private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {

if (e.Button == MouseButtons.Left) {

//画线起点 LineStartX = e.X; LineStartY = e.Y; blDrawLine = true; } } //画线

private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {

第 21 页 共 21 页

//判断是否可以画线 if (blDrawLine) {

//创建一个2像素宽的蓝色画笔 Pen p = new Pen(Color.Blue,2); //开始画线

g.DrawLine(p, LineStartX, LineStartY, e.X, e.Y); //重置画线起点 LineStartX = e.X; LineStartY = e.Y; } }

//取消画线

private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {

blDrawLine = false; }

//清除画线

private void button1_Click(object sender, EventArgs e) {

pictureBox1.Refresh(); }

第 22 页 共 22 页

} }

2.pp:251 图像显示、保存、变形和缩放程序设计 namespace BitmapProcess {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

//显示图像

private void btnOpen_Click(object sender, EventArgs e) {

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.Filter = \所有合适文件(*.bmp/*.jpg/*.gif)|*.*|Bitmap文件(*.bmp)|*.bmp|Jpeg文件(*.jpg)|*.jpg\ openFileDialog1.FilterIndex = 2;

openFileDialog1.RestoreDirectory = true;

if (DialogResult.OK == openFileDialog1.ShowDialog()) {

pictureBox1.Image = Bitmap.FromFile(openFileDialog1.FileName, false);

第 23 页 共 23 页

} }

//保存图像

private void btnSave_Click(object sender, EventArgs e) {

Bitmap b2 = new Bitmap(pictureBox1.Image);

SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.ShowDialog(); string str = saveFileDialog1.FileName; b2.Save(str); }

//图像旋转扭曲

private void btnDeform_Click(object sender, EventArgs e) { //旋转扭曲后的左上点、右上点、左下点的坐标点 Point[] destinationPoints = {

new Point(10, 10), // 左上点 new Point(200, 50),// 右上点 new Point(80, 150) // 左下点 }; // 原始图像

Image image = new Bitmap(pictureBox1.Image);

第 24 页 共 24 页

//在pictureBox1中位置绘制原图

Graphics g = pictureBox1.CreateGraphics(); //按照旋转扭曲后的坐标点,绘制变形的图像 g.DrawImage(image, destinationPoints); }

//图像缩放为原图的1/2

private void btnZoom_Click(object sender, EventArgs e) {

Image image = new Bitmap(pictureBox1.Image);

Image pThumbnail = image.GetThumbnailImage(200, 200, null, new IntPtr());

Graphics g = pictureBox1.CreateGraphics();

g.DrawImage(pThumbnail, 10, 10, pictureBox1.Image.Width / 2, pictureBox1.Image.Height/2); } } }

3 pp:254 动画程序设计 namespace PicsAnimate {

public partial class Form1 : Form {

//预加载图像保存

第 25 页 共 25 页

Bitmap[] bitmap; //图像编号

int PicNum=0; public Form1() {

InitializeComponent(); } //预加载图像

private void Form1_Load(object sender, EventArgs e) {

bitmap = new Bitmap[5]; for(int i=1;i<6;i++) {

bitmap[i-1] = new

Bitmap(Application.StartupPath+\ } } //开始动画

private void timer1_Tick(object sender, EventArgs e) {

if (PicNum > 4) {

PicNum = 0;

第 26 页 共 26 页

}

pictureBox1.Image = bitmap[PicNum]; PicNum++; } //速度控制

private void trackBar1_Scroll(object sender, EventArgs e) {

timer1.Interval = trackBar1.Value; } } }

实验十一 --- 进程和多线程程序设计

1.pp:101-103 应用进程调用已有的计算器并打开 namespace CProcess {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

//启动一个Windows计算器

第 27 页 共 27 页

private void btnCalculatorStart_Click(object sender, EventArgs e) {

CalculatorProcess.Start(); }

//关闭全部已启动的Windows计算器

private void btnCalculatorStopAll_Click(object sender, EventArgs e) {

//创建一个Process组件的数组 Process[] CalculatorProcess;

//将所建数组与指定的进程名称(calc)的所有进程资源相关联 CalculatorProcess = Process.GetProcessesByName(\ //遍历当前启动程序中,查找包含指定名称的进程 foreach(Process instance in CalculatorProcess) {

//终止当前进程,关闭应用程序窗体 instance.CloseMainWindow(); } }

//启动一个上机自测系统

private void btnSelfExamStart_Click(object sender, EventArgs e) {

FileInfo fInfo = new FileInfo(@\程序设计\\教材源代码\\chpt5-1\\selfExam\\selfExam\\bin\\Debug\\selfExam.exe\

第 28 页 共 28 页

if (fInfo.Exists) {

//实例一个Process类,启动一个独立进程 Process prcsSelfExam = new Process();

//Process有一个StartInfo属性,这是ProcessStartInfo类,包括一些方法和属性 prcsSelfExam.StartInfo.FileName = fInfo.FullName;//待启动的程序文件 prcsSelfExam.Start(); //启动进程 } else {

MessageBox.Show(\文件:\不存在!\ } }

//关闭所有上机自测系统

private void btnSelfExamStopAll_Click(object sender, EventArgs e) {

//创建一个Process组件的数组 Process[] SelfExamProcess;

//将所建数组与指定的进程名称(selfExam)的所有进程资源相关联 SelfExamProcess = Process.GetProcessesByName(\ //遍历当前启动程序中,查找包含指定名称的进程 foreach (Process instance in SelfExamProcess)

第 29 页 共 29 页

{

//终止当前进程,关闭应用程序窗体 instance.CloseMainWindow(); } } } }

2.pp: 107-108 应用线程技术实现两个独立线程进行0-999累加并显示在TextBox里 namespace MThreadTest1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent();

CheckForIllegalCrossThreadCalls = false;//禁用此异常 }

private Thread thread1 = null;//创建用来计数的线程对象 private Thread thread2 = null;

private void Form1_Load(object sender, EventArgs e) {

thread1 = new Thread(new ThreadStart(counter1));//线程初始化

第 30 页 共 30 页

thread2 = new Thread(new ThreadStart(counter2)); thread1.Start();//启动线程 thread2.Start(); }

//线程1(thread1)的计数方法 private void counter1() {

while (true) { int i;

for (i = 0; i < 1000; i++) {

textBox1.Text = i.ToString(); }

Thread.Sleep(3000);//线程休眠3秒 } }

//线程2(thread2)的计数方法 private void counter2() {

while (true) {

第 31 页 共 31 页

int j;

for (j = 0; j < 1000; j++) {

textBox2.Text = j.ToString(); }

Thread.Sleep(3000); } }

private void button1_Click(object sender, EventArgs e) {

thread1.Abort();//销毁线程 thread2.Abort();

button1.Enabled = false; } } }

3.pp: 111-113 应用多线程lock实现线程互斥,同时往richTextBox里输出大写A和小写a namespace ThreadMutex1 {

public partial class Form1 : Form {

public Form1()

第 32 页 共 32 页

{

InitializeComponent();

CheckForIllegalCrossThreadCalls = false;//禁用此异常 }

//创建显示字符的线程对象 private Thread thread1 = null; private Thread thread2 = null; //显示字符

private void ShowChar(char ch) {

lock (this) {

richTextBox1.Text += ch; } }

//线程thread1调用的方法(显示字符a) private void thread1Show() {

while (true) {

ShowChar('a'); Thread.Sleep(60);

第 33 页 共 33 页

} }

//线程thread2调用的方法(显示字符A) private void thread2Show() {

while (true) {

ShowChar('A'); Thread.Sleep(30); } }

//线程初始化,并启动线程

private void button1_Click(object sender, EventArgs e) {

thread1 = new Thread(new ThreadStart(thread1Show)); thread2 = new Thread(new ThreadStart(thread2Show)); thread1.Start(); thread2.Start();

button1.Enabled = false; button2.Enabled = true; }

//终止线程

第 34 页 共 34 页

private void button2_Click(object sender, EventArgs e) {

thread1.Abort(); thread2.Abort(); button1.Enabled = true; button2.Enabled = false; }

//关闭窗体时终止线程(否则,VS调试程序将仍处于运行状态)

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {

if (thread1 != null) thread1.Abort(); if (thread2 != null) thread2.Abort(); } } }

第 35 页 共 35 页

本文来源:https://www.bwwdw.com/article/mn0w.html

Top