史萊姆論壇

返回   史萊姆論壇 > 專業主討論區 > 程式語言討論區
忘記密碼?
論壇說明

歡迎您來到『史萊姆論壇』 ^___^

您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的!

請點擊這裡:『註冊成為我們的一份子!』

Google 提供的廣告


發文 回覆
 
主題工具 顯示模式
舊 2014-02-06, 09:01 PM   #1
魔術王子
版區管理員
 
魔術王子 的頭像
榮譽勳章
UID - 115097
在線等級: 級別:42 | 在線時長:1996小時 | 升級還需:25小時級別:42 | 在線時長:1996小時 | 升級還需:25小時
註冊日期: 2004-01-13
住址: 魔術學園
文章: 3000
精華: 0
現金: 15016 金幣
資產: 2678646 金幣
預設 程式 - [使用版本VB2008]TIF圖檔瀏覽

藉由PictureBox元件其實也可以開啟各種圖檔格式
由於TIF格式比較特殊,因為這種格式可以多頁存放,所以多了一項挑戰
執行畫面
https://imagizer.imageshack.us/v2/720x576q90/703/vkb1.jpg
程式除了展現開啟多頁TIF格式外,另外還可以透過拖曳到圖示來開啟
https://imagizer.imageshack.us/v2/720x587q90/197/pmcy.jpg
https://imagizer.imageshack.us/v2/720x559q90/841/ekvd.jpg
如果你覺得Windows本身所附的工具太陽春,而一般市面上的工具太貴或資源吃太兇,想使用自己設計的程式,你可以修改[檔案關聯],將程式設為預設開啓工具
https://imagizer.imageshack.us/v2/720x550q90/191/ae01.jpg
https://imagizer.imageshack.us/v2/720x599q90/819/noa6.jpg
這樣變更後,以後只要在附檔名為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
魔術王子 目前離線  
送花文章: 1533, 收花文章: 1556 篇, 收花: 3895 次
回覆時引用此帖
有 2 位會員向 魔術王子 送花:
Dreamcast (2014-08-08),ppp0600 (2014-02-07)
感謝您發表一篇好文章
發文 回覆



發表規則
不可以發文
不可以回覆主題
不可以上傳附加檔案
不可以編輯您的文章

論壇啟用 BB 語法
論壇啟用 表情符號
論壇啟用 [IMG] 語法
論壇禁用 HTML 語法
Trackbacks are 禁用
Pingbacks are 禁用
Refbacks are 禁用

相似的主題
主題 主題作者 討論區 回覆 最後發表
程式 - [使用版本VB2008]ListView元件及Panel元件 魔術王子 程式語言討論區 0 2014-01-23 08:25 PM
程式 - [使用版本VB2008]DataGridView元件不連接資料庫使用 魔術王子 程式語言討論區 1 2014-01-21 09:50 PM
兩張tif檔怎結合在一起 b3885804 一般電腦疑難討論區 1 2003-10-01 11:35 PM
tif檔怎ㄇ上傳相簿? rOy~ 軟體應用問題討論區 2 2003-03-25 11:06 PM


所有時間均為台北時間。現在的時間是 11:53 AM


Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.


SEO by vBSEO 3.6.1