藉由PictureBox元件其實也可以開啟各種圖檔格式
由於TIF格式比較特殊,因為這種格式可以多頁存放,所以多了一項挑戰
執行畫面
程式除了展現開啟多頁TIF格式外,另外還可以透過拖曳到圖示來開啟
如果你覺得Windows本身所附的工具太陽春,而一般市面上的工具太貴或資源吃太兇,想使用自己設計的程式,你可以修改[檔案關聯],將程式設為預設開啓工具
這樣變更後,以後只要在附檔名為TIF的檔案上,雙擊(點2下)滑鼠左鍵,系統就會以此程式來開啟TIF檔案
由於程式元件很多,而大部分用法之前都有提過,沒提過的用法也十分簡單,所以小弟打算只秀程式碼部分,畫面請大家自行設計...
引用:
作者: 程式碼
Imports System.IO
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Single, k As Integer = 0
ToolStripLabel4.Text = "共 1 頁之第 1 頁"
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
ToolStripComboBox1.Items.Clear()
For i = 0 To Printing.PrinterSettings.InstalledPrinters.Count - 1
ToolStripComboBox1.Items.Add(Printing.PrinterSettings.InstalledPrinters.Item(i).ToString)
Next
ToolStripComboBox1.Text = PrintDialog1.PrinterSettings.PrinterName
ToolStripComboBox2.Items.Clear()
For i = 0 To PrintDialog1.PrinterSettings.PaperSources.Count - 1
ToolStripComboBox2.Items.Add(PrintDialog1.PrinterSettings.PaperSizes.Item(i).PaperName)
If Mid(PrintDialog1.PrinterSettings.PaperSizes.Item(i).PaperName, 1, 2) = "A4" Then k = i
Next
ToolStripComboBox2.SelectedIndex = k
If My.Application.CommandLineArgs.Count > 0 Then
PictureBox1.ImageLocation = My.Application.CommandLineArgs(0).ToString
ToolStripStatusLabel1.Text = My.Computer.FileSystem.GetParentPath(PictureBox1.ImageLocation)
ScanFile()
For k = 0 To ListView1.Items.Count - 1
If ListView1.Items.Item(k).ToolTipText = PictureBox1.ImageLocation Then
ListView1.Items.Item(k).Selected = True
ListView1.Items.Item(k).Focused = True
End If
Next
ToolStripButton1.Checked = False
ToolStripButton1_Click(sender, e)
LoadImage()
Else
ToolStripStatusLabel1.Text = My.Computer.FileSystem.CurrentDirectory
ScanFile()
End If
End Sub
Sub LoadImage()
If ListView1.Items.Item(ListView1.FocusedItem.Index).ImageIndex = 1 Then
Dim newImage As Image
Dim sca As Double
newImage = Image.FromFile(ListView1.Items.Item(ListView1.FocusedItem.Index).ToolTipText)
PictureBox1.Image = newImage
PictureBox1.ImageLocation = ListView1.Items.Item(ListView1.FocusedItem.Index).ToolTipText
VScrollBar1.Maximum = newImage.GetFrameCount(FrameDimension.Page) - 1
ToolStripLabel4.Text = "共 " & newImage.GetFrameCount(FrameDimension.Page) & " 頁之第 " & VScrollBar1.Value + 1 & " 頁"
'PrintDocument1.DocumentName = "列印 - " & ListView1.FocusedItem.Text
If newImage.Width < newImage.Height Then
sca = newImage.Height / Panel1.ClientSize.Height
ToolStripButton2.Checked = False
ToolStripLabel1.Text = "直印"
'PrintDocument1.DefaultPageSettings.Landscape = False
Else
sca = newImage.Width / Panel1.ClientSize.Width
ToolStripButton2.Checked = True
ToolStripLabel1.Text = "橫印"
'PrintDocument1.DefaultPageSettings.Landscape = True
End If
PictureBox1.Width = newImage.Width * (Int(100 / sca) / 100)
PictureBox1.Height = newImage.Height * (Int(100 / sca) / 100)
If PictureBox1.Height > Panel1.ClientSize.Height Then PictureBox1.Top = Panel1.AutoScrollPosition.Y Else PictureBox1.Top = Panel1.AutoScrollPosition.Y + Panel1.ClientSize.Height / 2 - PictureBox1.Height / 2
If PictureBox1.Width > Panel1.ClientSize.Width Then PictureBox1.Left = Panel1.AutoScrollPosition.X Else PictureBox1.Left = Panel1.AutoScrollPosition.X + Panel1.ClientSize.Width / 2 - PictureBox1.Width / 2
End If
End Sub
Sub ScanFile()
My.Application.DoEvents()
Dim dir As New DirectoryInfo(ToolStripStatusLabel1.Text)
Dim dirs As DirectoryInfo() = dir.GetDirectories("*.*")
Dim diNext As DirectoryInfo
ListView1.Items.Clear()
For Each diNext In dirs
ListView1.Items.Add(diNext.ToString)
ListView1.Items.Item(ListView1.Items.Count - 1).ImageIndex = 0
Next
For Each f In dir.GetFiles("*.tif")
ListView1.Items.Add(f.Name)
ListView1.Items.Item(ListView1.Items.Count - 1).ImageIndex = 1
ListView1.Items.Item(ListView1.Items.Count - 1).ToolTipText = f.FullName
Next
If ToolStripStatusLabel1.Text.Length > 3 Then
ListView1.Items.Add("..")
ListView1.Items.Item(ListView1.Items.Count - 1).ImageIndex = 2
dir = New DirectoryInfo(My.Computer.FileSystem.GetParentPath(ToolStripStatusLabel1.Text))
dirs = dir.GetDirectories("*.*")
End If
End Sub
Private Sub ListView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.Click
If ListView1.Items.Item(ListView1.FocusedItem.Index).ToolTipText <> "" Then LoadImage()
End Sub
Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
If ListView1.Items.Item(ListView1.FocusedItem.Index).ImageIndex = 0 Then
ToolStripStatusLabel1.Text = My.Computer.FileSystem.CombinePath(ToolStripStatusLabel1.Text, ListView1.FocusedItem.Text)
ElseIf ListView1.Items.Item(ListView1.FocusedItem.Index).ImageIndex = 2 Then
ToolStripStatusLabel1.Text = My.Computer.FileSystem.GetParentPath(ToolStripStatusLabel1.Text)
End If
ScanFile()
End Sub
Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If PictureBox1.Image IsNot Nothing Then LoadImage()
End Sub
Private Sub VScrollBar1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VScrollBar1.ValueChanged
Dim i As Integer = VScrollBar1.Value
Dim newImage As Image
newImage = Image.FromFile(ListView1.Items.Item(ListView1.FocusedItem.Index).ToolTipText)
newImage.SelectActiveFrame(FrameDimension.Page, i)
PictureBox1.Image = newImage
ToolStripLabel4.Text = "共 " & newImage.GetFrameCount(FrameDimension.Page) & " 頁之第 " & VScrollBar1.Value + 1 & " 頁"
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
If ToolStripButton2.Checked Then ToolStripLabel1.Text = "橫印" Else ToolStripLabel1.Text = "直印"
End Sub
Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
Dim i As Integer, k As Integer = 0
PrintDialog1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
ToolStripComboBox2.Items.Clear()
For i = 0 To PrintDialog1.PrinterSettings.PaperSources.Count - 1
ToolStripComboBox2.Items.Add(PrintDialog1.PrinterSettings.PaperSizes.Item(i).PaperName)
If Mid(PrintDialog1.PrinterSettings.PaperSizes.Item(i).PaperName, 1, 2) = "A4" Then k = i
Next
ToolStripComboBox2.SelectedIndex = k
PrintDocument1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
End Sub
Private Sub ToolStripComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox2.SelectedIndexChanged
PrintDocument1.DefaultPageSettings.PaperSize = PrintDialog1.PrinterSettings.PaperSizes.Item(ToolStripComboBox2.SelectedIndex)
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim x As Integer, y As Integer, w As Integer, h As Integer
If Mid(ToolStripComboBox2.Text, 1, 2) = "A4" Then
If ToolStripButton2.Checked Then
x = -25 : y = -15 : w = 0 : h = 0
Else
x = -10 : y = -20 : w = 0 : h = 0
End If
Else
x = -20 : y = -20 : w = 0 : h = 0
End If
Dim destRect As New Rectangle(x, y, e.PageBounds.Width + w, e.PageBounds.Height + h)
Dim newImage As Image
newImage = PictureBox1.Image
newImage.SelectActiveFrame(FrameDimension.Page, VScrollBar1.Value)
e.Graphics.DrawImage(newImage, destRect)
e.HasMorePages = False
End Sub
Private Sub ToolStripSplitButton1_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripSplitButton1.ButtonClick
Dim newImage As Image
newImage = Image.FromFile(ListView1.Items.Item(ListView1.FocusedItem.Index).ToolTipText)
PrintDocument1.DocumentName = "列印 - " & ListView1.Items.Item(ListView1.FocusedItem.Index).Text
PrintDocument1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
PrintDocument1.PrinterSettings.Copies = 1
PrintDocument1.DefaultPageSettings.PaperSource = PrintDialog1.PrinterSettings.PaperSources.Item(ToolStripComboBox2.SelectedIndex)
If ToolStripButton2.Checked Then
PrintDocument1.DefaultPageSettings.Landscape = True
Else
PrintDocument1.DefaultPageSettings.Landscape = False
End If
For i = 0 To newImage.GetFrameCount(FrameDimension.Page) - 1
PrintDocument1.Print()
If VScrollBar1.Value = VScrollBar1.Maximum Then VScrollBar1.Value = 0 Else VScrollBar1.Value += 1
Next
End Sub
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim newImage As Image
newImage = Image.FromFile(ListView1.Items.Item(ListView1.FocusedItem.Index).ToolTipText)
PrintDocument1.DocumentName = "列印 - " & ListView1.Items.Item(ListView1.FocusedItem.Index).Text
PrintDocument1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
PrintDocument1.PrinterSettings.Copies = 1
PrintDocument1.DefaultPageSettings.PaperSource = PrintDialog1.PrinterSettings.PaperSources.Item(ToolStripComboBox2.SelectedIndex)
If ToolStripButton2.Checked Then
PrintDocument1.DefaultPageSettings.Landscape = True
Else
PrintDocument1.DefaultPageSettings.Landscape = False
End If
PrintDocument1.Print()
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
ListView1.Visible = ToolStripButton1.Checked
End Sub
End Class
|
程式透過VScrollBar1屬性Value來選擇顯示的頁面
引用:
Private Sub VScrollBar1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VScrollBar1.ValueChanged
Dim i As Integer = VScrollBar1.Value
Dim newImage As Image
newImage = Image.FromFile(ListView1.Items.Item(ListView1.FocusedItem.Index).ToolTipText)
newImage.SelectActiveFrame(FrameDimension.Page, i)
PictureBox1.Image = newImage
ToolStripLabel4.Text = "共 " & newImage.GetFrameCount(FrameDimension.Page) & " 頁之第 " & VScrollBar1.Value + 1 & " 頁"
End Sub
|
此處判斷執行程式時,是否有附加參數,當使用拖曳時,檔案的路徑就會當成參數傳給程式
引用:
If My.Application.CommandLineArgs.Count > 0 Then
PictureBox1.ImageLocation = My.Application.CommandLineArgs(0).ToString
ToolStripStatusLabel1.Text = My.Computer.FileSystem.GetParentPath(PictureBox1.ImageLocation)
ScanFile()
For k = 0 To ListView1.Items.Count - 1
If ListView1.Items.Item(k).ToolTipText = PictureBox1.ImageLocation Then
ListView1.Items.Item(k).Selected = True
ListView1.Items.Item(k).Focused = True
End If
Next
ToolStripButton1.Checked = False
ToolStripButton1_Click(sender, e)
LoadImage()
Else
ToolStripStatusLabel1.Text = My.Computer.FileSystem.CurrentDirectory
ScanFile()
End If
|