C# 编程是基于 C 和 C++ 编程语言的,是一门面向对象的编程语言
1.什么是对象
在使用面向对象的计算机语言的程序员眼里世间万物皆是对象,例如大到地球、宇宙星空,小到细胞、尘埃等都视之为对象。
对象是由属性、方法、事件组成。
(1)属性:用来描述对象的形态外貌特征
(2)方法:指对象的主动行为,天生具有的一些能力
(3)事件:指对象的被动行为,天生并不具备这种能力,而是受其他因素影响产生的能力
2.15种属性类型
注意:有无符号表示是否可以取负值
(1) 整数(8种):byte(8位有符号),sbyte(8位有符号);
Int(32位) uint(无符号)
Long(64) ulong
Short(16) ushort
(2)小数: Double(双精度) float(单精度) decimal(精度)
(3)Char 字符型 string 字符串型 bool 布尔型 object 对象型(可以囊括所有类型)
eg:Char a=’ ’ string=” ” bool c=true/false object c= this;
注意:(1)字符和字符串的区别:字符型使用单引号,单引号中有且只有一个字符(空字符不是一个字符)
字符串型使用双引号,双引号中是多个字符的组合;
(2).object虽然可以包含所有属性类型,但它的存储空间非常大,浪费资源,所以需要分情况使用,划分其他的属性类型,方便有针对性的进行赋值,合理分配存储空间
3.属性赋值:步骤
判断属性类型:第一步:查看属性类型:将鼠标停在属性上,会出现自动提示,如果是15种预定义属性类型则直接赋值;
第二步:如果不是15种类型,试着在等号右边空格 若出现智能提示则加一个点后以分号结束
第三步:遇到特殊属性类型,等号后直接使用属性类型然后加点并选择属性类型以分号结,如(this.BackColor=Color.blue;)
第四步:若敲空格仍未出现提示,则在等号右边输入new+空格,出现智能提示后,加上一对小括号外加分号,再删掉前半个括号后又添上,然后针对括号中的属性继续根据情况使用这四种方法赋值
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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//属性赋值
//1.首先查看属性类型,如果属于15种预定义数据类型,则直接进行赋值
//(1)15种数据类型:整数(8种)
//sbyte(8位 有符号) byte(无符号)
//int(32位 有符号) uint(无符号)
//short(16位 有符号) ushort
//long(64) ulong
//小数:float(单) double(双) decimal(精度)
//字符串:string
//布尔:bool
//字符:char
//对象:object
//2.如果不是十五种属性类型,在等号后敲空格,出现提示加点以分号结束
//3.遇到特殊类型,直接使用属性,加点以分号结束
//4.等号后敲空格没有出现智能提示时,在等号后敲new+空格,出现提示后加一对小括号,以分号结束.然后再删掉前半个括号,针对括号内的属性继续使用前四种方法
this.BackColor=Color.black;
this.Font = new Font("",20);
this.Font = new Font("",15,FontStyle.Bold);
this.ImeMode = ImeMode.Alpha;
this.AccessibleRole = AccessibleRole.Alert;
this.ActiveControl = new Control();
this.ActiveControl = new Control("",20,20,20,20);
}
}
}
窗口抖动案例1
.项目分析
1.首先添加窗口加载(load)事件,设置窗口的大小,初始位置,
2.在视图的工具箱中给窗口添加按钮(button),给button对象设置单击(click)事件;
3.在click事件中改变窗口的位置(设置线程);
4.利用for循环使其重复抖动;
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;
//全部是命名空间
using System.Threading;
namespace _02窗口抖动案例
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//1.设置窗口的大小
this.Width = 300;
this.Height = 300;
//2.设置窗口的初始位置
this.Left = 500;
this.Top = 300;
//3.设置button1的文本是“抖动”
button1.Text = "抖动";
}
private void button1_Click(object sender, EventArgs e)
{
//i++= i= i+1
for (int i=0;i<5 ;i++ )
{
//事件里面的代码严格按照从上往下的顺序一行一行执行
this.Left = 503;
//线程:代码执行的路线进程
//方法:小括号分号结束,给括号中填参数
Thread.Sleep(10);
this.Top = 303;
Thread.Sleep(10);
this.Left = 500;
Thread.Sleep(10);
this.Left = 497;
Thread.Sleep(10);
this.Top = 300;
Thread.Sleep(10);
this.Top = 297;
Thread.Sleep(10);
this.Left = 500;
Thread.Sleep(10);
this.Left = 503;
Thread.Sleep(10);
this.Top = 300;
Thread.Sleep(10);
this.Left = 500;
}
}
}
}
滚动字幕案例2:步骤:
设置form1窗口大小(使用size属性)
设置form1窗口位置居中
设置窗口的背景颜色
在视图设计界面中添加textbox,用来存储文本,并修改其相关属性,如(将multiline属性设置为true)
设置textbox的背景色,前背景色(即文本文字颜色),文本居中对齐,只读属性,字体样式,大小,光标,等
设置文本框的位置在窗体的最下面,用来实现自下而上滚动效果
设置命名空间,添加导入音乐工具
添加if判断,是歌词循环滚动
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 System.Media;
namespace gundongzimu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//设置大小
this.Size = new Size(400,500);
//设置位置居中
this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width/2;
this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
//修改文本名称
this.Text = "滚动字幕";
//设置窗口的背景颜色
this.BackColor = Color.Black;
//设置文本字体大小
textBox1.Width = this.Width;
textBox1.Font = new Font("幼圆", 14);
//设置文本文字居中
textBox1.TextAlign = HorizontalAlignment.Center;
//设置文本框的位置在(0,0)点
textBox1.Location = new Point(0, 0);
//设置文本框的背景颜色为黑色
textBox1.BackColor = Color.Black;
//设置文本的前景色(文本色)
textBox1.ForeColor = Color.LightBlue;
//设置文本光标在所有文字的最后
textBox1.SelectionStart = textBox1.TextLength;
//设置文本框的位置在窗口最下面(自下而上滚动)
textBox1.Top = this.Height;
//导入音乐
SoundPlayer sound = new SoundPlayer();
sound.SoundLocation = "shengpizi.wav";
//sound.SoundLocation = "../../shengpizi.wav";
sound.Play();
}
private void timer1_Tick(object sender, EventArgs e)
{
//设置文本框的top,每隔0.01秒减少2
textBox1.Top = textBox1.Top -2;
if(textBox1.Top <= -textBox1.Height)
{
textBox1.Top = this.Height;
}
}
}
}
4.位图:top、left width、height