由于有C语言和java的基础,所以直接去看C#开发1200例,从例子中学习,只为熟练C#的控件api
第 一 天:
windows窗体:
1.设置窗体启动位置:
private void Form1_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;//居中显示
}
2.设置窗体位置:
3.从上次关闭位置启动窗体:
private void Frm_Main_Load(object sender, EventArgs e)
{
RegistryKey myReg1, myReg2;//声明注册表对象
myReg1 = Registry.CurrentUser;//获取当前用户注册表项
try
{
myReg2 = myReg1.CreateSubKey("Software\\MySoft");//在注册表项中创建子项
this.Location = new Point(Convert.ToInt16(myReg2.GetValue("1")), Convert.ToInt16(myReg2.GetValue("2")));//设置窗体的显示位置
}
catch { }
}
private void Frm_Main_FormClosed(object sender, FormClosedEventArgs e)
{
RegistryKey myReg1, myReg2;//声明注册表对象
myReg1 = Registry.CurrentUser;//获取当前用户注册表项
myReg2 = myReg1.CreateSubKey("Software\\MySoft");//在注册表项中创建子项
try
{
myReg2.SetValue("1", this.Location.X.ToString());//将窗体关闭位置的x坐标写入注册表
myReg2.SetValue("2", this.Location.Y.ToString());//将窗体关闭位置的y坐标写入注册表
}
catch { }
}
4.始终置于桌面最顶层:
只需将Topmost属性设置为True即可。
5.窗体边框风格解释:
代码设置:
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
6.根据桌面大小调整窗体:
private void Frm_Main_Load(object sender, EventArgs e)
{
int DeskWidth = Screen.PrimaryScreen.WorkingArea.Width;//获取桌面宽度
int DeskHeight = Screen.PrimaryScreen.WorkingArea.Height;//获取桌面高度
this.Width = Convert.ToInt32(DeskWidth * 0.8);//设置窗体宽度
this.Height = Convert.ToInt32(DeskHeight * 0.8);//设置窗体高度
}
7.自定义最大、小化和关闭按钮:
记得要将FromBorderstyle的属性设置为none来屏蔽窗体原有的按钮。然后添加两个panel,一个用于放三个按钮。另一个用于放置内容。
#region 设置窗体的最大化、最小化和关闭按钮的单击事件
/// <summary>
/// 设置窗体的最大化、最小化和关闭按钮的单击事件
/// </summary>
/// <param Frm_Tem="Form">窗体</param>
/// <param n="int">标识</param>
public void FrmClickMeans(Form Frm_Tem, int n)
{
switch (n)//窗体的操作样式
{
case 0://窗体最小化
Frm_Tem.WindowState = FormWindowState.Minimized;//窗体最小化
break;
case 1://窗体最大化和还原的切换
{
if (Frm_Tem.WindowState == FormWindowState.Maximized)//如果窗体当前是最大化
Frm_Tem.WindowState = FormWindowState.Normal;//还原窗体大小
else
Frm_Tem.WindowState = FormWindowState.Maximized;//窗体最大化
break;
}
case 2: //关闭窗体
Frm_Tem.Close();
break;
}
}
#endregion
#region 控制图片的切换状态
/// <summary>
/// 控制图片的切换状态
/// </summary>
/// <param sender =" object ">要改变图片的对象</param>
/// <param n="int">标识</param>
/// <param ns="int">移出移入标识</param>
public static PictureBox Tem_PictB = new PictureBox();//实例化PictureBox控件
public void ImageSwitch(object sender, int n, int ns)
{
Tem_PictB = (PictureBox)sender;
switch (n)//获取标识
{
case 0://当前为最小化按钮
{
Tem_PictB.Image = null;//清空图片
if (ns == 0)//鼠标移入
Tem_PictB.Image = Properties.Resources.最小化变色;
if (ns == 1)//鼠标移出
Tem_PictB.Image = Properties.Resources.最小化按钮;
break;
}
case 1://最大化按钮
{
Tem_PictB.Image = null;
if (ns == 0)
Tem_PictB.Image = Properties.Resources.最大化变色;
if (ns == 1)
Tem_PictB.Image = Properties.Resources.最大化按钮;
break;
}
case 2://关闭按钮
{
Tem_PictB.Image = null;
if (ns == 0)
Tem_PictB.Image = Properties.Resources.关闭变色;
if (ns == 1)
Tem_PictB.Image = Properties.Resources.关闭按钮;
break;
}
}
}
#endregion
private void pictureBox_Close_Click(object sender, EventArgs e)//单击事件
{
FrmClickMeans(this, Convert.ToInt16(((PictureBox)sender).Tag.ToStrin