⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdichildform.cs

📁 中文名:Windows Forms 程序设计 英文名:Windows Forms Programming in c# 作者: Chris Sells 翻译: 荣耀 蒋贤哲 出版社:人民
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;

using MruControlLibrary;

namespace DocApp {
  public partial class MDIChildForm : Form {
  
    private MruMenuManager mruMenuManager;
    private bool isPrintPreview;
    private int rowCount;
    
    public MDIChildForm() {
      InitializeComponent();      
    }

    public FileDocumentControlLibrary.FileDocument FileDocument {
      get { return this.fileDocument; }
    }
        
    public string FileName {
      get { return this.fileDocument.FileName; }
    }
    
    // For opening document from command line arguments
    public bool OpenDocument(string filename) {
    
      // Attempt to open file
      bool fileOpened = this.fileDocument.Open(filename);
      
      if( fileOpened) {
        // Add to MRU menu
        this.mruMenuManager.Add(filename);
      }
      
      return fileOpened;      
    }
    private void fileDocument_NewDocument(object sender, EventArgs e) {
      this.fileDocument.Data = this.textBox.Text;
    }
    private void fileDocument_ReadDocument(object sender, FileDocumentControlLibrary.SerializeDocumentEventArgs e) {

      // Deserialize object using the FileDocument's formatter
      //IFormatter formatter = this.fileDocument.Formatter;
      this.textBox.Text = (string)this.fileDocument.Formatter.Deserialize(e.Stream);

      // Let file document take care of loading
      e.Handled = true;
    }
    private void fileDocument_WriteDocument(object sender, FileDocumentControlLibrary.SerializeDocumentEventArgs e) {
    
      // Pass data to file document for saving
      this.fileDocument.Data = this.textBox.Text;
      
      // Add to MRU menu
      this.mruMenuManager.Add(e.Filename);

      // Let file document take care of saving
      e.Handled = false;
    }

    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
      this.Close();
    }
    
    private void printToolStripMenuItem_Click(object sender, EventArgs e) {
      this.Print();
    }
    private void printToolStripButton_Click(object sender, EventArgs e) {
      // Print direct
      this.Print();
    }
    private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e) {
      // Don't print preview of not printers installed
      if( PrinterSettings.InstalledPrinters.Count == 0) {
        MessageBox.Show("No printers are currently installed.", Application.ProductName);
        return;
      }
      
      this.printPreviewDialog.ShowDialog();
    }
    private void printSetupToolStripMenuItem_Click(object sender, EventArgs e) {
      // Don't allow print setup of not printers installed
      if( PrinterSettings.InstalledPrinters.Count == 0 ) {
        MessageBox.Show("No printers are currently installed.", Application.ProductName);
        return;
      }

      if( this.pageSetupDialog.ShowDialog() == DialogResult.OK ) {
        this.printDocument.PrinterSettings = this.pageSetupDialog.PrinterSettings;
      }
    }
    private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
      // Don't print if nothing to print
      if( this.textBox.Text == "" ) e.Cancel = true;

      // Pre-printing configuration
      this.isPrintPreview = (e.PrintAction == PrintAction.PrintToPreview);
      this.rowCount = 0;
    }
    private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {

      Graphics g = e.Graphics;

      // Print page text
      Rectangle printableArea = this.GetRealMarginBounds(e, this.isPrintPreview);

      while( this.rowCount < this.textBox.Lines.Length ) {

        string line = this.textBox.Lines[this.rowCount];

        // Get size for wordwrap
        SizeF printSize = g.MeasureString(line, this.textBox.Font, printableArea.Width);

        if( printableArea.Height > printSize.Height ) {

          // Print line
          g.DrawString(line, this.textBox.Font, Brushes.Black, printableArea);

          // Calculate and reduce remaining printable area
          printableArea.Y += (int)printSize.Height;
          printableArea.Height -= (int)printSize.Height;

          this.rowCount++;
        }
        else break;
      }
      // Keep printing until file ends
      e.HasMorePages = (this.rowCount < this.textBox.Lines.Length);
    }
    private void Print() {

      // Configure PrintDialog page printing range configurations
      int pageCount = this.pageCountPrintController.PageCount();
      int maxPages = (pageCount > 0 ? pageCount : 1);
      this.printDialog.PrinterSettings.FromPage = 1;
      this.printDialog.PrinterSettings.ToPage = maxPages;
      this.printDialog.PrinterSettings.MaximumPage = 1;
      this.printDialog.PrinterSettings.MaximumPage = maxPages;

      if( this.printDialog.ShowDialog() == DialogResult.OK ) {
        this.printDocument.DocumentName = this.fileDocument.FileName;
        this.printDocument.Print();
      }
    }
    private Rectangle GetRealMarginBounds(PrintPageEventArgs e, bool preview) {

      if( preview ) return e.MarginBounds;

      // Get printer抯 offsets
      float cx = e.PageSettings.HardMarginX;
      float cy = e.PageSettings.HardMarginY;

      // Create the real margin bounds by scaling the offset
      // by the printer resolution and then rescaling it
      // back to 1/100th of an inch
      Rectangle marginBounds = e.MarginBounds;
      float dpiX = e.Graphics.DpiX;
      float dpiY = e.Graphics.DpiY;
      marginBounds.Offset((int)(-cx * 100 / dpiX), (int)(-cy * 100 / dpiY));
      return marginBounds;
    }

    private void undoToolStripMenuItem_Click(object sender, EventArgs e) {
      this.textBox.Undo();
    }
    private void cutToolStripMenuItem_Click(object sender, EventArgs e) {
      this.textBox.Cut();
    }
    private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
      this.textBox.Copy();
    }
    private void pasteToolStripMenuItem_Click(object sender, EventArgs e) {
      this.textBox.Paste();
    }

    private void textBox_TextChanged(object sender, EventArgs e) {
      // Update the dirty bit
      this.fileDocument.Dirty = true;
      this.ConfigureEditMenu();
    }
    private void ConfigureEditMenu() {
      bool canEdit = !string.IsNullOrEmpty(this.textBox.Text);
      this.editToolStripMenuItem.Enabled = canEdit;
      this.cutToolStripButton.Enabled = canEdit;
      this.copyToolStripButton.Enabled = canEdit;
      this.pasteToolStripButton.Enabled = canEdit;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -