欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Winform常用的一些功能收集(持续更新)

程序员文章站 2022-07-02 18:42:35
...
#region progressBar实时显示进度
        private void button1_Click(object sender, EventArgs e)
        {
            int i = 10000;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = i;
            for (int k = 0; k < i; k++)
            {
                progressBar1.Value += 1;
                Application.DoEvents();  //label1实时显示,而非处理完成后再显示
                label1.Text = progressBar1.Value.ToString() ;
            }
        }
        #endregion

        #region 打开文件选择对话框
        private void menuFileLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Open Photo";
            dlg.Filter = "jpg files(*.jpg)|*.jpg|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    pbxPhoto.Image = new Bitmap(dlg.OpenFile());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
            dlg.Dispose();
        }
        #endregion

        #region 菜单选中或可选
        private void menuImage_DropDownOpening(object sender, EventArgs e)
        {
            ProcessImageOpening(sender as ToolStripDropDownItem);
        }

        private void ProcessImageOpening(ToolStripDropDownItem parent)
        {
            if (parent != null)
            {
                string enumval = pbxPhoto.SizeMode.ToString();
                foreach (ToolStripMenuItem item in parent.DropDownItems)
                {
                    item.Enabled = (pbxPhoto.Image != null);
                    item.Checked = item.Tag.Equals(enumval);
                }
            }
        }
        #endregion
#region Timer控件定时操作
        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 1000;//1秒1次
            timer1.Tick += new EventHandler(tmr_Tick);
            timer1.Start();
        }

        private void tmr_Tick(object sender, EventArgs e)
        {
            //到达设定时间后的事件
            label1.Text = DateTime.Now.ToString() + "执行了";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
        #endregion
//ListView新增列
            listView1.Columns.Add("年龄", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("班级", 80, HorizontalAlignment.Center);
            listView1.Columns.Add("性别", 40, HorizontalAlignment.Center);
            listView1.Columns.Add("职业", 100, HorizontalAlignment.Center);
            button5.Enabled = false;

            //ListView新增行 批量更新防闪烁
            listView1.BeginUpdate();
            listView1.Items.Add("row1", "张三", 0);
            listView1.Items["row1"].SubItems.Add("21");
            listView1.Items["row1"].SubItems.Add("98001");
            listView1.Items["row1"].SubItems.Add("男");
            listView1.Items["row1"].SubItems.Add("学生");

            listView1.Items.Add("row2", "李四", 0);
            listView1.Items["row2"].SubItems.Add("22");
            listView1.Items["row2"].SubItems.Add("98002");
            listView1.Items["row2"].SubItems.Add("女");
            listView1.Items["row2"].SubItems.Add("教师");

            listView1.Items.Add("row3", "王五", 0);
            listView1.Items["row3"].SubItems.Add("23");
            listView1.Items["row3"].SubItems.Add("98003");
            listView1.Items["row3"].SubItems.Add("男");
            listView1.Items["row3"].SubItems.Add("工程师");

            listView1.Items.Add("row4", "赵六", 0);
            listView1.Items["row4"].SubItems.Add("24");
            listView1.Items["row4"].SubItems.Add("98004");
            listView1.Items["row4"].SubItems.Add("女");
            listView1.Items["row4"].SubItems.Add("公务员");
            listView1.EndUpdate();

            //设置ListView属性
            listView1.GridLines = true;
            listView1.View = View.Details;
            listView1.LabelEdit = false;
            listView1.HeaderStyle = ColumnHeaderStyle.Clickable;

            //更改行颜色
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (i % 2==0)
                {
                    listView1.Items[i].BackColor = Color.Gray;
                }
            }

            listView1.Items.Clear(); //清除列表内容 不包含表头

            listView1.Clear(); //清除列表包含表头
//画直线
            Graphics g = CreateGraphics();
            Pen p = new Pen(Color.Gray);
            g.DrawLine(p, 0, this.Height / 2, this.Width, this.Height / 2);            

            //画矩形
            g.DrawRectangle(p, 50, 50, 200, 100);

            //画圆形
            g.DrawEllipse(p, 100, 100, 100, 100);

            //画圆柱体
            int height = this.ClientSize.Height - 200;
            int width = this.ClientSize.Width - 50;
            int vHeight = 200;
            int vWidth = 100;
            g.Clear(Color.White);
            SolidBrush brush = new SolidBrush(Color.Gainsboro);
            for (int i = height / 2; i > 0; i--)
            {
                g.DrawEllipse(p, width / 2, i, vHeight, vWidth);
            }
            g.FillEllipse(brush, width / 2, 0, vHeight, vWidth);
            
            //释放资源
            p.Dispose();
            g.Dispose();
//读取图片
        //Bitmap bmap = new System.Drawing.Bitmap(@"c:\1.jpg");
        Bitmap bmap = (Bitmap)System.Drawing.Image.FromFile(@"c:\1.jpg");
        //加载图片
        Graphics g = Graphics.FromImage(bmap);
        //定义字体
        Font drawFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Millimeter);
        //定义画笔
        SolidBrush drawBrush = new SolidBrush(Color.Red);
        //定义坐标
        int xPos = bmap.Height - (bmap.Height - 25);
        int yPos = 3;
        //绘制文字
        g.DrawString("清纯美女", drawFont, drawBrush, xPos, yPos);
        //保存图片
        bmap.Save(@"c:\2.jpg");
        //释放资源
        bmap.Dispose();
        g.Dispose();
private void btnExport_Click(object sender, EventArgs e)
        {
            //获取用户选择路径  导出Execl
            SaveFileDialog SaveFile = new SaveFileDialog();
            SaveFile.Filter = "Miscrosoft Office Excel 97-2003 工作表|*.xls|所有文件(*.*)|*.*";
            SaveFile.RestoreDirectory = true;
            if (SaveFile.ShowDialog() == DialogResult.OK)
            {
                string targetPath = SaveFile.FileName;
                if (File.Exists(targetPath)) { File.Delete(targetPath); } File.Copy(fileSource, SaveFile.FileName);
                DialogResult r = MessageBox.Show ("导出数据成功!" , "系统信息" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
                if (r == DialogResult.OK)
                {
                    System.Diagnostics.Process.Start(SaveFile.FileName);
                }
            }
        }

转载于:https://my.oschina.net/smartsmile/blog/814854