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

📄 printgdi.cs

📁 windows mobile 开发实例wi ndows mobile 开发实例
💻 CS
📖 第 1 页 / 共 3 页
字号:
               FileShare.None), 
               encodeFile);

            //  Write the three fields, each of a 
            //     different data type.  
            //  Close the file.
            bwrtFile.Write(this.strName);
            bwrtFile.Write(this.sglSize);
            bwrtFile.Write((int)this.fsStyle);
            bwrtFile.Close();
         }

         public void ReadFromFile(string  strDirName,  
            string  strFileName,  
            Encoding encodeFile) 
         {
            //  Set the current directory
            Directory.SetCurrentDirectory(strDirName);

            //  Create a BinaryReader (wrapped around a 
            //     FileStream wrapped around the file)
            //     for input using the user specified 
            //     encoding.
            BinaryReader brdrFile = 
               new BinaryReader(
               new FileStream(
               strFileName, 
               FileMode.OpenOrCreate,
               FileAccess.Read,
               FileShare.None), 
               encodeFile);
                
            //  Read the three fields.
            //  Close the file.
            try
            {
               this.strName = brdrFile.ReadString();
               this.sglSize = brdrFile.ReadSingle();
               this.fsStyle = (FontStyle)brdrFile.ReadInt32();
            }
            catch
            {
               this.strName = "Tahoma";
               this.sglSize = 9;
               this.fsStyle = FontStyle.Regular;
            }
            finally
            {
               brdrFile.Close();
            }
         }
      }

      #endregion

      #region Save/Restore to Registry

      //  Variables for registry access.
      UtilRegistry  urNotepad = new UtilRegistry();

      string  strNotepadCE = "NotepadCe";
      string  strFont = "Font";
      string  strName = "Name";
      string  strSize = "Size";
      string  strStyle = "Style";
      string  strOptions = "Options";
      string  strMenu = "Menu";
      string  strToolBar = "ToolBar";
      string  strScrollBars = "ScrollBars";
      string  strTextAlign = "TextAlign";
      string  strWordWrap = "WordWrap";

      private void mitemSettingsSave_Click(object sender, 
         EventArgs e)
      {
         //    Use the UtilRegistry object to save the
         //       font settings.
         //    UtilRegistry has three overloads for 
         //       SetValue; string, integer, and boolean.
         //    Font Size and Style are data types that
         //       derive from integer; they have to 
         //       be converted to integer for this call.  
         //    Font.Style data type, because it has 
         //       the [Flags] attribute, requires an 
         //       unchecked conversion.
         urNotepad.SetValue(
            strNotepadCE + @"\" + strFont, 
            strName, 
            tboxInput.Font.Name);
         urNotepad.SetValue(
            strNotepadCE + @"\" + strFont, 
            strSize, 
            Convert.ToInt32(tboxInput.Font.Size));
         urNotepad.SetValue(
            strNotepadCE + @"\" + strFont, 
            strStyle, 
            unchecked((System.Int32)tboxInput.Font.Style));

         //    Use the UtilRegistry object to save the
         //       Textbox settings; three of which are
         //       boolean, two of which are [Flags] 
         //       attributed unsigned integers.
         urNotepad.SetValue(
            strNotepadCE + @"\" + strOptions, 
            strMenu, 
            this.Menu == this.menuMain);
         urNotepad.SetValue(
            strNotepadCE + @"\" + strOptions, 
            strToolBar, 
            this.Controls.Contains(tbarCommands));
         urNotepad.SetValue(
            strNotepadCE + @"\" + strOptions, 
            strScrollBars, 
            unchecked((System.Int32)tboxInput.ScrollBars));
         urNotepad.SetValue(strNotepadCE + @"\" + strOptions, 
            strTextAlign, 
            unchecked((System.Int32)tboxInput.TextAlign));
         urNotepad.SetValue(strNotepadCE + @"\" + strOptions, 
            strWordWrap, 
            tboxInput.WordWrap);
      }

      // Version that uses the ourFontInfo structure
      //      private void mitemSettingsSave_Click(object sender, 
      //         EventArgs e)
      //      {
      //         string  strDirName = @"My Documents\NotepadCE";
      //         string  strFileName = "Settings.dat";
      //
      //         //  Create an ourFontInfo structure, load it from
      //         //     tboxInput's font, have the structure save
      //         //     its contents to the file.
      //      
      //         ourFontInfo fiFont= new ourFontInfo();
      //         fiFont.strName = tboxInput.Font.Name;
      //         fiFont.sglSize = tboxInput.Font.Size;
      //         fiFont.fsStyle = tboxInput.Font.Style;
      //         fiFont.WriteToFile(
      //            strDirName, strFileName, Encoding.Default);
      //      }


      private void mitemSettingsRestore_Click(object sender, 
         EventArgs e)
      {
         //  Read Font info, if any, from the registry, 
         //  Create a font from that info, or use default
         //     values if no info is in the registry.
         //  Set tboxInput.Font = that font.
         string  strFontName;
         int intFontSize;
         int intFontStyle;

         strFontName = "Tahoma";
         urNotepad.GetValue(strNotepadCE + @"\" + strFont, 
            strName, 
            ref strFontName);

         intFontSize = 9;
         urNotepad.GetValue(strNotepadCE + @"\" + strFont, 
            strSize, 
            ref intFontSize);

         intFontStyle = (int)FontStyle.Regular;
         urNotepad.GetValue(strNotepadCE + @"\" + strFont, 
            strStyle, 
            ref intFontStyle);
         tboxInput.Font = 
            new Font(strFontName, 
            intFontSize, 
            (FontStyle)intFontStyle);

         //  Read Option info, if any, from the registry.
         //  Set the properties from that info, or use default
         //     values if no info is in the registry.
         bool boolTemp;
         int intTemp;

         //  .Menu is either menuMain or null
         boolTemp = true;
         urNotepad.GetValue(strNotepadCE + @"\" + strOptions, 
            strMenu, 
            ref boolTemp);
         this.Menu = boolTemp ? this.menuMain : null;

         //  .Controls either contains 
         //     tbarCommands or it doesn't
         boolTemp = true;
         urNotepad.GetValue(strNotepadCE + @"\" + strOptions, 
            strToolBar, 
            ref boolTemp);
         if ( boolTemp ) 
         {
            if (! this.Controls.Contains(this.tbarCommands) ) 
            {
               this.Controls.Add(this.tbarCommands);
            }
         } 
         else 
         {
            if ( this.Controls.Contains(this.tbarCommands) ) 
            {
               this.Controls.Remove(this.tbarCommands);
            }
         }

         //  .ScrollBars
         intTemp = (int)ScrollBars.Both;
         urNotepad.GetValue(strNotepadCE + @"\" + strOptions, 
            strScrollBars, 
            ref intTemp);
         tboxInput.ScrollBars = (ScrollBars)intTemp;

         //  .TextAlign
         intTemp = (int)HorizontalAlignment.Left;
         urNotepad.GetValue(strNotepadCE + @"\" + strOptions, 
            strTextAlign, 
            ref intTemp);
         tboxInput.TextAlign = (HorizontalAlignment)intTemp;

         //  .WordWrap
         boolTemp = true;
         urNotepad.GetValue(strNotepadCE + @"\" + strOptions, 
            strWordWrap, 
            ref boolTemp);
         tboxInput.WordWrap = boolTemp;
      }


      // Version that uses the ourFontInfo structure
      //      private void mitemSettingsRestore_Click(object sender, 
      //                                              EventArgs e)
      //      {
      //         string  strDirName = @"My Documents\NotepadCE";
      //         string  strFileName = "Settings.dat";
      //
      //         //  Create an ourFontInfo structure, have the 
      //         //     structure load itself from the file, 
      //         //     create the font from the loaded info.
      //         ourFontInfo fiFont = new ourFontInfo();
      //         fiFont.ReadFromFile(
      //            strDirName, strFileName, Encoding.Default);
      //         tboxInput.Font = 
      //            new Font(
      //            fiFont.strName, fiFont.sglSize, fiFont.fsStyle);
      //      }


      private void mitemSettingsInit_Click(object sender, 
         EventArgs e)
      {
         //    Initialize the settings by deleting the
         //       registry key where they are stored and
         //       then restoring from the non existant
         //       key to reinstate all default values.
         urNotepad.DeleteKey(strNotepadCE);
         mitemSettingsRestore_Click(this, EventArgs.Empty);
      }
      #endregion

      #region TextBox Clipboard Support

      // Clipboard messages
      public const int WM_CUT = 0x0300;
      public const int WM_COPY = 0x0301;
      public const int WM_PASTE = 0x0302;
      public const int WM_CLEAR = 0x0303;
      public const int WM_UNDO = 0x0304;

      // P/Invoke declaration
      [DllImport("coredll.dll", CharSet=CharSet.Unicode)]
      public static extern IntPtr GetFocus();

      private void mitemEditCut_Click(object sender, EventArgs e)
      {
         m_msg.Msg = WM_CUT; // WM_CUT does not work...

         // ...and yet Copy & Clear work, so we do that instead
         m_msg.Msg = WM_COPY;
         MessageWindow.SendMessage(ref m_msg);
         m_msg.Msg = WM_CLEAR;
         MessageWindow.SendMessage(ref m_msg);
      }

      private void mitemEditCopy_Click(object sender, EventArgs e)
      {
         m_msg.Msg = WM_COPY;
         MessageWindow.SendMessage(ref m_msg);
      }

      private void mitemEditPaste_Click(object sender, EventArgs e)
      {
         m_msg.Msg = WM_PASTE;
         MessageWindow.SendMessage(ref m_msg);
      }

      private void mitemEditClear_Click(object sender, EventArgs e)
      {
         m_msg.Msg = WM_CLEAR;
         MessageWindow.SendMessage(ref m_msg);
      }

      private void mitemEditUndo_Click(object sender, EventArgs e)
      {
         m_msg.Msg = WM_UNDO;
         MessageWindow.SendMessage(ref m_msg);
      }

      #endregion

      #region Print Support

      IntPtr hwndForm;
      private void 
      mitemFilePrint_Click(object sender, EventArgs e)
      {
         // Display dialog to select printer & port.
         PAGESETUPDLGSTRUCT psd;
         psd = new PAGESETUPDLGSTRUCT();
         PrintSetupDlg.InitDlgStruct(ref psd, hwndForm);
         int iErr = PrintSetupDlg.ShowDialog(ref psd);
         if (iErr == 0)
         {
            // Either error...
            string strErr = PrintSetupDlg.GetErrorString();
            if (strErr != "Ok")
            {
               MessageBox.Show(strErr, "PrintGdi");
            }
            // ...Or user clicked <Cancel>
            return;
         }

         IntPtr hdcPrinter = IntPtr.Zero;
         IntPtr hfont = IntPtr.Zero;
         IntPtr hfontOld = IntPtr.Zero;
         try
         {
            // Connect to printer by creating a DC.
            hdcPrinter = Printing.CreatePrinterDC(ref psd);
            if (hdcPrinter != IntPtr.Zero)
            {
               // Select font.
               hfont = GdiFont.Create(tboxInput.Font.Name, 
                  (int)tboxInput.Font.Size, 0, hdcPrinter);
               hfontOld = GdiGraphics.SelectObject(hdcPrinter, hfont);

               // Print
               PrintJob_Gdi.PrintText(tboxInput, hdcPrinter);
            }
            else
            {
               throw new System.Exception();
            }
         }
         catch
         {
            MessageBox.Show("Error connecting to printer.", "PrintGdi");
         }
         finally
         {
            // Cleanup
            GdiGraphics.SelectObject(hdcPrinter, hfontOld);
            GdiGraphics.DeleteObject(hfont);
            Printing.DeleteDC(hdcPrinter);

            // Clean up resources associated with print dialog.
            PrintSetupDlg.Close(ref psd);
         }
      }

      private void FormMain_GotFocus(object sender, EventArgs e)
      {
         hwndForm = WinFocus.GetFocus();
      }

#endregion


   } // class
} // namespace

⌨️ 快捷键说明

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