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

📄 printdirect.vb

📁 Programming the .NET Compact Framework with vb 源代码
💻 VB
📖 第 1 页 / 共 3 页
字号:
               Me.sglSize = .ReadSingle()
               Me.intStyle = .ReadInt32()
               .Close()
            End With
         End Sub

      End Structure

      Private Sub SaveSettingsToFile()

         '  Create the directory and the file, if necessary.
         If Not Directory.Exists(strDirName) Then _
                           Directory.CreateDirectory(strDirName)
         Directory.SetCurrentDirectory(strDirName)
         If Not File.Exists(strFileName) Then _
                           File.Create(strFileName).Close()

         '  Open a writer for the file
         Dim bwrtFile As New BinaryWriter _
            ( _
               New FileStream(strFileName, _
                              FileMode.OpenOrCreate, _
                              FileAccess.Write, _
                              FileShare.None), _
               Me.encodeFile _
            )

         '  Write the data to the file.
         With bwrtFile
            .Write(tboxInput.Font.Name)      '  String.
            .Write(tboxInput.Font.Size)      '  Single.
            .Write(tboxInput.Font.Style)     '  Integer.
            .Close()
         End With

         '  Create an ourFontInfo structure, load it from
         '     tboxInput's font, have the structure save
         '     its contents to the file.
         'Dim fiFont As ourFontInfo
         'With tboxInput.Font
         '   fiFont.strName = .Name
         '   fiFont.sglSize = .Size
         '   fiFont.intStyle = .Style
         'End With
         'fiFont.WriteToFile(strDirName, _
         '                   strFileName, _
         '                   Encoding.Default)
      End Sub

      Private Sub ReadSettingsFromFile()

         '  Make the directory the current directory.
         Directory.SetCurrentDirectory(strDirName)

         '  Open a reader for the file.
         Dim brdrFile As New BinaryReader _
            ( _
               New FileStream(strFileName, _
                              FileMode.OpenOrCreate, _
                              FileAccess.Read, _
                              FileShare.None), _
               Me.encodeFile _
            )

         '  Read each field from the file, then 
         '     close the file.
         With brdrFile
            Dim strName As String = .ReadString()
            Dim sglSize As Single = .ReadSingle()
            Dim intStyle As Integer = .ReadInt32()
            tboxInput.Font = _
               New Font(strName, sglSize, intStyle)
            .Close()
         End With

      End Sub

#End Region

#Region "Save/Restore to Registry"

      '  Utility object for registry access.
      Dim urNotepad As New UtilRegistry

      '  Field names for the settings in the registry.
      Dim strNotepadCE As String = "NotepadCe", _
          strFont As String = "Font", _
          strName As String = "Name", _
          strSize As String = "Size", _
          strStyle As String = "Style", _
          strBold As String = "Bold", _
          strItalic As String = "Italic", _
          strUnderline As String = "Underline", _
          strOptions As String = "Options", _
          strMenu As String = "Menu", _
          strToolBar As String = "ToolBar", _
          strScrollBars As String = "ScrollBars", _
          strTextAlign As String = "TextAlign", _
          strWordWrap As String = "WordWrap"

      Private Sub mitemSettingsSave_Click( _
                           ByVal sender As System.Object, _
                           ByVal e As System.EventArgs _
                           ) _
                           Handles mitemSettingsSave.Click

         With tboxInput.Font
            urNotepad.SetValue(strNotepadCE + "\" + strFont, _
                               strName, _
                               .Name)
            urNotepad.SetValue(strNotepadCE + "\" + strFont, _
                               strSize, _
                               Convert.ToInt16(.Size))
            urNotepad.SetValue(strNotepadCE + "\" + strFont, _
                               strStyle, _
                               Convert.ToInt16(.Style))

            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strMenu, _
                               Me.Menu Is Me.menuMain)
            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strToolBar, _
                               Me.Controls.Contains(tbarCommands))
            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strScrollBars, _
                               tboxInput.ScrollBars)
            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strTextAlign, _
                               tboxInput.TextAlign)
            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strWordWrap, _
                               tboxInput.WordWrap)
         End With
      End Sub

      Private Sub mitemSettingsRestore_Click( _
                           ByVal sender As System.Object, _
                           ByVal e As System.EventArgs _
                           ) _
                           Handles mitemSettingsRestore.Click

         '  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
         With urNotepad
            Dim strFontName As String
            Dim intFontSize As Integer
            Dim intFontStyle As Integer

            strFontName = "Tahoma"
            .GetValue(strNotepadCE + "\" + strFont, _
                      strName, _
                      strFontName)

            intFontSize = 9
            .GetValue(strNotepadCE + "\" + strFont, _
                      strSize, _
                      intFontSize)

            intFontStyle = FontStyle.Regular
            .GetValue(strNotepadCE + "\" + strFont, _
                      strStyle, _
                      intFontStyle)

            tboxInput.Font = _
               New Font(strFontName, intFontSize, intFontStyle)
         End With

         '  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
         Dim boolTemp As Boolean
         Dim intTemp As Integer
         With urNotepad

            '  .Menu is either menuMain or Nothing
            boolTemp = True
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strMenu, _
                      boolTemp)
            Me.Menu = IIf(boolTemp, Me.menuMain, Nothing)

            '  .Controls either contains 
            '     tbarCommands or it doesn't
            boolTemp = True
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strToolBar, _
                      boolTemp)
            If boolTemp Then
               If Not Me.Controls.Contains(Me.tbarCommands) Then
                  Me.Controls.Add(Me.tbarCommands)
               End If
            Else
               If Me.Controls.Contains(Me.tbarCommands) Then
                  Me.Controls.Remove(Me.tbarCommands)
               End If
            End If

            '  .ScrollBars
            intTemp = ScrollBars.Both
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strScrollBars, _
                      intTemp)
            tboxInput.ScrollBars = intTemp

            '  .TextAlign
            intTemp = HorizontalAlignment.Left
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strTextAlign, _
                      intTemp)
            tboxInput.TextAlign = intTemp

            '  .WordWrap
            boolTemp = True
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strWordWrap, _
                      boolTemp)
            tboxInput.WordWrap = boolTemp
         End With
      End Sub

      Private Sub mitemSettingsInit_Click( _
                           ByVal sender As System.Object, _
                           ByVal e As System.EventArgs _
                           ) _
                           Handles mitemSettingsInit.Click

         urNotepad.DeleteKey(strNotepadCE)
         mitemSettingsRestore_Click(Me, EventArgs.Empty)
      End Sub

#End Region

#Region "Clipboard Support"
      ' Clipboard messages
      Public Const WM_CUT = &H300
      Public Const WM_COPY = &H301
      Public Const WM_PASTE = &H302
      Public Const WM_CLEAR = &H303
      Public Const WM_UNDO = &H304

      ' Supports clipboard messages to text box.
      Private m_msg As Microsoft.WindowsCE.Forms.Message

      Private Sub mitemClipboard_Click( _
      ByVal sender As Object, _
      ByVal e As System.EventArgs) _
         Handles mitemCut.Click, mitemCopy.Click, _
         mitemPaste.Click, mitemUndo.Click, mitemClear.Click

         If sender.Equals(mitemCut) Then
            m_msg.Msg = WM_CUT
            ' Cut does not work, so do equivalent...
            m_msg.Msg = WM_COPY
            MessageWindow.SendMessage(m_msg)
            m_msg.Msg = WM_CLEAR
         ElseIf sender.Equals(mitemCopy) Then
            m_msg.Msg = WM_COPY
         ElseIf sender.Equals(mitemPaste) Then
            m_msg.Msg = WM_PASTE
         ElseIf sender.Equals(mitemClear) Then
            m_msg.Msg = WM_CLEAR
         ElseIf sender.Equals(mitemUndo) Then
            m_msg.Msg = WM_UNDO
         End If

         MessageWindow.SendMessage(m_msg)
      End Sub

#End Region

#Region "Printing"

      Dim hwndForm As IntPtr

      Private Sub mitemFilePrint_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles mitemFilePrint.Click
         ' Init print setup dialog data.
         Dim psd As PAGESETUPDLGSTRUCT
         psd = New PAGESETUPDLGSTRUCT
         PrintSetupDlg.InitDlgStruct(psd, hwndForm)

         Try
            ' Display print setup dialog box 
            Dim iErr As Integer = PrintSetupDlg.ShowDialog(psd)
            If iErr <> 0 Then
               ' Fetch port name from print setup data.
               Dim strPort As String
               strPort = PrintSetupDlg.QueryOutputPort(psd)

               ' Check whether port is an IP address.
               If PrintJob_Socket.IsIPAddress(strPort) Then
                  PrintJob_Socket.PrintText(tboxInput, strPort)
               Else
                  ' Send text to selected port.
                  PrintJob_Direct.PrintText(tboxInput, strPort)
               End If
            Else
               Dim strErr As String = PrintSetupDlg.GetErrorString()
               If strErr <> "Ok" Then
                  MessageBox.Show(strErr, "PrintDirect")
               End If
            End If
         Catch
            MessageBox.Show("Error printing.", "PrintDirect")
         Finally
            ' Clean up resources associated with print dialog.
            PrintSetupDlg.Close(psd)
         End Try

      End Sub

      Private Sub FormMain_GotFocus( _
      ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles MyBase.GotFocus
         hwndForm = WinFocus.GetFocus()
      End Sub
#End Region

   End Class
End Namespace

⌨️ 快捷键说明

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