c# winform 应用编程代码总结 14

50、自画ListBox

        private Brush[] listBoxBrushes ;
        //该数组用来存储绘制listBox1背景的Brush对象
        private int[] listBoxHeights = new int[] {50, 25, 33, 15,20} ;
        //该数组用来存储listBox1各列表项的预定义高度

        private void Form1_Load(object sender, System.EventArgs e)
        {
            Bitmap backgroundImage = new Bitmap("..\\..\\First.bmp");
            Brush backgroundBrush = new TextureBrush(backgroundImage);
            //创建brush,将使用它画ListBox中第一个列表项的背景
            
            Rectangle r = new Rectangle(0, 0, listBox1.Width, 100);
            LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red,Color.Yellow,LinearGradientMode.Horizontal);
            //创建一个渐变画刷,将使用它画第三个列表项的背景

            listBoxBrushes = new Brush[]
                {
                    backgroundBrush,
                    Brushes.LemonChiffon,
                    lb,
                    Brushes.CornflowerBlue,
                    Brushes.PeachPuff
                };
            //创建Brush类的数组listBoxBrushes
            //其中包含了自定义的backgroundBrush、lb
            //系统定义的Brush中的LemonChiffon、CornflowerBlue、PeachPuff
        }

        private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            e.DrawBackground();

            Brush brush = listBoxBrushes[e.Index];
            e.Graphics.FillRectangle(brush, e.Bounds);
            //用指定的画刷填充列表项范围所形成的矩形
            e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds);
            //画列表项的边框

            bool selected = ((e.State & DrawItemState.Selected)
                == DrawItemState.Selected) ? true : false;
            string displayText = "ITEM #" + e.Index;
            displayText = displayText + (selected ? " SELECTED" : "");
            e.Graphics.DrawString(displayText, this.Font, Brushes.Black, e.Bounds);
            //显示列表项上的字符并且如果该列表项处于选中状态
            //则再添加字符SELECTED
            e.DrawFocusRectangle();
            //绘制聚焦框
        }

        private void listBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
        {
            string displayText = "ITEM #" + e.Index;
            SizeF stringSize=e.Graphics.MeasureString(displayText, this.Font);
            stringSize.Height += 6;
            if (listBoxHeights[e.Index] > stringSize.Height)
                e.ItemHeight = listBoxHeights[e.Index];
            else
                e.ItemHeight = (int)stringSize.Height;
        }

         image

51、使用打开文件对话框

        private void button1_Click(object sender, System.EventArgs e)
        {
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Multiselect =false;
            openFileDialog1.Filter = "文件类型(*.rtf)|*.rtf";
            openFileDialog1.Title = "请选择一个RTF格式文件";
            openFileDialog1.InitialDirectory = "C:";
            openFileDialog1.ShowReadOnly =true;
            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowHelp = true;
            if ((this.openFileDialog1.ShowDialog() == DialogResult.OK)
                & (openFileDialog1.FileName.Length > 0))
            {
                try {this.richTextBox1.LoadFile(this.openFileDialog1.FileName);}                
                catch {}
            }
            if (this.openFileDialog1.ReadOnlyChecked == true)
            {this.richTextBox1.ReadOnly = true;}
            else
            {this.richTextBox1.ReadOnly = false;}
            //如果在openFileDialog1中选定文件为只读方式打开
            //那么ReadOnly属性设置为true
            //在richTextBox1中不能修改文本
        }

52、使用保存文件对话框

        private void button1_Click(object sender, System.EventArgs e)
        {
            saveFileDialog1.AddExtension = true;
            saveFileDialog1.DefaultExt = "rtf";
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.Filter = "RTF格式文件(*.rtf)|*.rtf";
            saveFileDialog1.OverwritePrompt = true;
            if ((this.saveFileDialog1.ShowDialog()==DialogResult.OK)
                &(saveFileDialog1.FileName.Length > 0))
            {
                this.richTextBox1.SaveFile(this.saveFileDialog1.FileName); 
            }
        }

作者:syxChina
本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 278250658@qq.com 联系作者本人。

posted @ 2010-12-14 15:34  BuildNewApp  阅读(244)  评论(0编辑  收藏  举报