Re: [VB6 ] 關於pictureBox與scrollBar的幾個問題

看板Visual_Basic作者 (矇面加菲獅)時間19年前 (2006/05/13 10:21), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《ashinplay (..............)》之銘言: : 1.如何把繪pictureBox裡的圖片(全部物件)清除? 圖片物件:Set Picture1.Picture = Nothing 繪圖物件:Picture1.Cls() : 2.如何使用scrollBar來瀏覽pictureBox裡的圖片(只考慮垂直方向) : 就是如過圖片數超過pictureBox的大小的話,那scrollBar應該怎麼寫? : (p.s. pictureBox裡的圖片是用PaintPicture的方式來畫的) : 感謝~ '表單上放置 PictureBox、CommonDialog、CommandButton 三個控制項 Option Explicit Private PicMax As Long '儲存圖片的總高度 Private Sub Form_Activate() Dim w As Long, h As Long '調整視窗位置及大小 w = 800 * Screen.TwipsPerPixelX: h = 600 * Screen.TwipsPerPixelY Move (Screen.Width - w) / 2, (Screen.Height - h) / 2, w, h '調整 PictureBox 位置及大小 w = 500 * Screen.TwipsPerPixelX: h = ScaleHeight Picture1.Move 0, 0, w, h '調整 VScrollBar 位置及大小 VScroll1.Move Picture1.Left + w, 0, 300, h End Sub Private Sub Command1_Click() On Error GoTo errProc Dim PicFile As IPictureDisp, a As Variant Dim i As Integer, j As Integer, Count As Integer Dim FilePath As String, FileName As String, PictureWidth As Long Dim x As Long, y As Long, w As Long, h As Long, ratio As Single '取得圖片寬度(可顯示範圍@_@) PictureWidth = Picture1.ScaleWidth - VScroll1.Width '開啟自動繪圖功能 Picture1.AutoRedraw = True '設定檔案對話方塊路徑 ChDir App.Path CommonDialog1.InitDir = CurDir '多重選取、95以上標準對話方塊、使用目前路徑、禁示輸入不存在的檔案 CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer Or _ cdlOFNNoChangeDir Or cdlOFNFileMustExist '打開檔案對話方塊 CommonDialog1.ShowOpen '取得使用者選擇的檔案清單,Chr(0)為分隔符號 a = Split(CommonDialog1.FileName, Chr(0)) Count = UBound(a) + 1 x = 0: y = 0 '開始將選擇的圖片檔逐一印出來 For i = 0 To Count - 1 FileName = a(i) If i = 0 Then If Count > 0 Then '如果是複選的話,第一個項目即為檔案路徑 FilePath = a(i) FileName = "" Else '如果是單選的話,第一個項目為檔案路徑+檔案名稱 j = InStrRev(FileName, "\") FilePath = Left(FileName, j) FileName = Mid(FileName, j + 1) End If If Not FilePath Like "*\" Then FilePath = FilePath & "\" End If If Trim(FileName) <> "" Then '載入圖片,並取得圖片大小 Set PicFile = LoadPicture(FilePath & FileName) w = PicFile.Width: h = PicFile.Height '依比例縮圖 If w > PictureWidth Then ratio = PictureWidth / w w = PictureWidth: h = h * ratio End If '將 PictureBox 調整成適當大小,不然圖片會看不到 Picture1.Height = y + h '將圖片印出至 PictureBox Picture1.PaintPicture PicFile, x, y, w, h y = y + h End If Next '設定捲軸狀態 VScroll1.Min = 0 VScroll1.Max = y - ScaleHeight VScroll1.LargeChange = y / 10 VScroll1.SmallChange = y / 20 Exit Sub errProc: MsgBox Err.Description End Sub 'ScrollBar 兩個事件必須處理@_@ Private Sub VScroll1_Change() Call VScroll1_Scroll() End Sub Private Sub VScroll1_Scroll() '將 PictureBox 向上移動,即可看到下面超出視窗範圍的內容了 Picture1.Top = PicMax - VScroll1.Value End Sub -- 技巧來自於 vb 研究小站,提醒大家要多多參考。 -- ▃▅▇▆▄ ▆▂▃ ` 逝去感如何能留住,半點遺留殊不易,██▅▇▄▃ ▇▃▂" . █████████▃i ▁▄▇ 更多悽悽慘慘的遭遇………██▆▃ █▅▆▃ˍ▄* ▂█▄▇▅▂. 我不知道,王~八~蛋~~! ▂▆███ █▄▃ 。fumizuki。Check。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.58.156.43

05/13 12:56, , 1F
感激不盡<(_ _)>
05/13 12:56, 1F
文章代碼(AID): #14PKAVr- (Visual_Basic)
文章代碼(AID): #14PKAVr- (Visual_Basic)