C# 窗口和控件的操作

来源:互联网 发布:淘宝手机版装修尺寸 编辑:程序博客网 时间:2024/05/16 10:55

一。 窗口和窗口之间传值

1第一种方法用静态变量

在form1里定义 public static string  passvalue;在form2里赋值;

2第二种方法用new的参数

定义form2时加一个参数,新建窗口时把值传进去。

 public partial class Form2 : Form    {        public Form2(string value )//通过new 窗口之间传值        {            InitializeComponent();            value = "";        }

 private void button1_Click(object sender, EventArgs e)        {            Form2 PictureForm = new Form2("789");            PictureForm.ShowDialog();        }

二 。全屏

1)窗口全屏

            this.SetVisibleCore(false);            this.FormBorderStyle = FormBorderStyle.None;            this.WindowState = FormWindowState.Maximized;            this.SetVisibleCore(true);

2)控件全屏

新建一个窗口,设置 FormBorderStyle为None,WindowState为Maximized,TopMost为True。然后具体代码如下

AddEventKeyUp(control);            原来的parent.Controls.Clear();            frmFullscreen  frm = new frmFullscreen();            frm.Controls.Add(control);            frm.ShowDialog();
private void AddEventKeyUp(Control control) {            if (control != null) {                control.KeyUp += new KeyEventHandler(control_KeyUp);                foreach (Control c in control.Controls) {                    AddEventKeyUp(c);                }            }        }        void control_KeyUp(object sender, KeyEventArgs e) {            if (e.KeyCode == Keys.Escape) {                if (control != null) {                   if (frm != null) {                        frm.Controls.Clear();                        原来的parent.Controls.Add(control);// 这里不能和下面的Close顺序错了,要不然会引起错误,因为Close后把控件销毁了。                        frm.Close();                        frm = null;                    }                }            }        }


三。随机数

 Random rd = new Random();
 label3.Text = rd.Next(0,9).ToString() ; 


四。PictureBox 控件

1 )加载图片

       1 pictureBox1.Image = Image.FromFile("F:\\123.JPG");

       2 Application.StartupPath;  //   可以得到程序根目录 
           string picPath=Application.StartupPath+"//1.gif";

      3  网络图片路径

          this.pictureBox2.Image= Image.FromStream(System.Net.WebRequest.Create(http://www.xyhhxx.com/images/logo/logo.gif).GetResponse().GetResponseStream()); 
2)图片上加文字,透明底色

     //将label1隐藏,根据label1的文字重画图片。

      

      label3.Parent = pictureBox1;      pictureBox1.Parent = panel1;  
        private void pictureBox1_Paint(object sender, PaintEventArgs e)        {               PictureBox pb = sender as PictureBox;                  foreach (Control C in pb.Controls)                  {                          if (C is Label)                          {                                  Label L = (Label)C;                                  L.Visible = false;                                  e.Graphics.DrawString(L.Text, L.Font, new SolidBrush(L.ForeColor),                                      new RectangleF(L.Left - pb.Left, L.Top - pb.Top, L.Width, L.Height));                          }                  }           }

3)保存图片

            saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PnG Image|*.png|Wmf  Image|*.wmf";
            saveFileDialog1.FilterIndex = 0;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (pictureBox1.Image != null)
                {
                     pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                }

                else

                {

                       MessageBox.Show("没有预览图片");

                 }
            }

4)













0 0