English(beta)
hfyb的Blog 页面正在加载中 .....

Windows中的弹出窗体

         Windows中的弹出窗体有很多,最简单的Msgbox就不说了,其他还有“保存”、“打开”等等,今天就一一说一下:
         首先在窗体中添加toolStrip控件,打开控件编译器,插入标准项,双击打开文件按钮,在打开OToolStripButton_Click事件中添加代码如下:
            private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            OpenFileDialog openfileDialog1 = new OpenFileDialog();
            openfileDialog1.InitialDirectory = "c:\\";
            openfileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            openfileDialog1.FilterIndex = 2;
            openfileDialog1.RestoreDirectory = true;
            openfileDialog1.ShowReadOnly = true;
            openfileDialog1.ShowHelp = true;
            if (openfileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((openfileDialog1.OpenFile().ToString()) != null)
                {
                    openfileDialog1.OpenFile().Close();
                }
                if (openfileDialog1.ReadOnlyChecked == true)
                { openfileDialog1.OpenFile(); }
                else { string path = openfileDialog1.FileName;
                //return new (path,System.IO.FileMode.Open,System.IO.FileAccess.ReadWrite);
                }
            }
        }
        运行 就可以看到打开文件夹的弹出窗体。
        其他的弹出窗体代码与此类同:
         private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog savefileDialog1 = new SaveFileDialog();
            savefileDialog1.InitialDirectory = "c:\\";
            savefileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*|*.gif|*.gif";
            savefileDialog1.FilterIndex = 2;
            savefileDialog1.RestoreDirectory = true;
            savefileDialog1.ShowHelp = true;
            if (savefileDialog1.ShowDialog()==DialogResult.OK )
            {
            //if(savefileDialog1.OpenFile()!=null)
            //{ savefileDialog1.OpenFile().Close(); }
            }
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog1 = new FontDialog();
            fontDialog1.ShowColor = true;
            fontDialog1.Font = textBox1.Font;
            fontDialog1.Color = textBox1.ForeColor;
            if(fontDialog1.ShowDialog()==DialogResult.OK )
            {
                textBox1.Font = fontDialog1.Font;
                textBox1.ForeColor = fontDialog1.Color;
            }
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog();
            colorDialog1.AllowFullOpen = true;
            colorDialog1.Color = textBox1.ForeColor;
            colorDialog1.ShowDialog();
           
            if(colorDialog1.ShowDialog()==DialogResult.OK )
            {
                textBox1.BackColor = colorDialog1.Color;
            }
        }

        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //PrintDocumengt printDocumengt1 = new PrintDocument();
            PrintDialog printDialog1 = new PrintDialog();
           
            printDialog1 = new System.Windows.Forms.PrintDialog();
            printDialog1.AllowCurrentPage=true;
            printDialog1.AllowSelection=true;
            //printDialog1.Document = printDocument1;
            if(printDialog1.ShowDialog()!=DialogResult.Cancel )
            {
                //printDocument1.print();
            }
        }

        private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            printPreviewDialog1.Document = new System.Drawing.Printing.PrintDocument();
           
            printPreviewDialog1.ShowDialog();
        }
        好了,这样就基本上有了所有的弹出窗体了!代码有什么不对的地方请大家指点,博主也是刚开始写,谢谢!

posted on 2006-11-27 23:15  hfyb  阅读(849)  评论(2编辑  收藏  举报

导航