• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    用Visual Basic輕松實現看圖軟件

    發布: 2007-5-25 09:19 | 作者: 海藍 | 來源: 天極網 | 查看: 31次 | 進入軟件測試論壇討論

    領測軟件測試網  一、 前言

      在日常的工作或學習中,像一些常用的工具是必不可少的。比如ACDsee,WinRAR等等。其實在VB中就可以很容易地實現像ACDsee這樣的一些基本看圖功能,用著自己編寫的看圖軟件,感覺是不是非常棒?!好了,言歸正傳,現在就開始編寫吧!

      二、分析與實現

      編寫之前,首先把一些重要的實現功能思考一下。明白了原理,做其他類似的程序,應該是輕車熟路了。

      1) 在選擇的目錄中過濾出圖片格式的文件(gif,jpg,bmp,ico等)功能:

      VB中自帶的DriveListBox,DirListBox,FileListBox控件組合就可以輕易的實現上述功能,將FileListBox的Pattern屬性設置為*.jpg;*.bmp;*.ico;*.gif即可,這樣FileListBox中只顯示Pattern屬性設定好的擴展名所對應的圖片文件。

      2) 將當前目錄下的圖片按分頁的方式顯示功能:

      關于顯示圖片的控件問題,其實用VB自帶的Image控件就可以了。對于如何實現分頁的問題,就需要按照自定義的顯示圖片數量/頁(本程序定義為25張圖片/頁),動態生成Image控件組,然后將目錄中的圖片以縮放預覽的方式顯示出來,具體實現參見后面的代碼。

      以上兩個問題解決了,基本功能應該可以實現了。下面就是具體的實現步驟:

      a) 主窗體設計

      主窗體布局采用流行的T型框架,上方包括菜單及常用工具欄,左側顯示目錄結構樹,右側為顯示圖片區域(圖片預覽區),下方為狀態欄,用來顯示簡單的圖片信息。下表是用到的部分控件列表:

    控件名 控件類型 標題 說明
    Command1(1) CommandButton 上一頁 預覽上一頁圖片
    Command1(0) CommandButton 下一頁 預覽下一頁圖片
    Picture1 PictureBox   用來裝入預覽控件組的容器控件
    Image1(0) Image   顯示圖片控件
    Drive1 DriveListBox   顯示當前系統的磁盤列表
    Dir1 DirListBox   顯示當前磁盤的目錄列表
    File1 FileListBox   顯示當前目錄的圖片格式文件列表

      b) 代碼實現

      通過以下一段代碼將驅動器列表控件、目錄列表控件、文件列表控件聯系起來。

    Private Sub Dir1_Change()
     File1.Path = Dir1.Path
    End Sub

    Private Sub Drive1_Change()
     Dir1.Path = Drive1.Drive
     Call InitShowPic
    End Sub


    Private Sub File1_PathChange() ‘當文件列表發生變化時重新顯示圖片
     Call InitShowPic
    End Sub

      其中InitShowPic是自定義函數,用來顯示當前目錄下的第一頁圖片。主窗體初始化時,需要先將顯示圖片的Image控件組動態創建,以下就是創建代碼:

    Private Sub Form_Load()
     Dim i, j As Integer
     For i = 1 To 24 ‘動態創建24個Image控件
      Load Image1(i) ‘動態創建Image控件
      Image1(i).Visible = True ‘顯示此控件
     Next

     Drive1.Drive = "c:" ‘默認驅動器為C:
     Dir1.Path = "c:\"
     Picture1.DrawWidth = 3
     For i = 1 To 4 ‘在Picture畫線將Image控件組以5×5排列
      Picture1.Line (0, i * (Picture1.Height \ 5) - 30)-(Picture1.Width, i * (Picture1.Height \ 5) - 30), &H80000003

      Picture1.Line (i * (Picture1.Width \ 5) - 30, 0)-(i * (Picture1.Width \ 5) - 30, Picture1.Height), &H80000003
     Next

     For i = 0 To 4
      For j = 0 To 4
       Image1(i * 5 + j).Left = j * (Picture1.Width \ 5) + 50
       Image1(i * 5 + j).Top = i * (Picture1.Height \ 5) + 50
      Next
     Next
     currindex = -1 ‘當前選擇圖片的索引為-1表示沒有選擇圖片
    End Sub


    Private Sub InitShowPic() ‘顯示當前目錄的第一頁圖片(1到25)
     Dim i As Integer
     For i = 0 To 24 ‘所有Image控件不顯示圖片
      Image1(i).Picture = LoadPicture("")
      Image1(i).ToolTipText = ""
     Next
     If File1.ListCount = 0 Then ‘如果當前目錄沒有圖片
      StatusBar1.Panels(1).Text = ""
      StatusBar1.Panels(2).Text = ""
      StatusBar1.Panels(3).Text = ""
      StatusBar1.Panels(4).Text = ""
      StatusBar1.Panels(4).Visible = False
      If currindex <> -1 Then ‘如果選擇了圖片,則顯示此圖片的Image控件的邊框風格改為平板風格
       Image1(currindex).Appearance = 0
      End If 
      currpage = 1: currindex = -1 ‘當前選擇的頁號為1并不選擇圖片
      Exit Sub
     End If
     currpage = 1: currindex = -1 ‘如果當前目錄有圖片,則將頁號賦值為1并不選擇圖片
     Call DisplayPicPage(currpage) ‘調用自定義函數顯示指定頁號的圖片
    End Sub

    Private Sub DisplayPicPage(page As Integer) ‘顯示指定頁的圖片
     Dim i As Integer
     Dim usetime As Long
     On Error Resume Next
     usetime = GetTickCount
     If (File1.ListCount - (25 * (page - 1)) < 25) And (File1.ListCount - (25 * (page - 1)) > 0) Then
      For i = 0 To File1.ListCount Mod 25 - 1
       Image1(i).Picture = LoadPicture(Dir1.Path + "\" + File1.List((page - 1) * 25 + i))
       Image1(i).ToolTipText = File1.List((page - 1) * 25 + i)
      Next
      For i = File1.ListCount Mod 25 To 24
       Image1(i).Picture = LoadPicture("")
       Image1(i).ToolTipText = ""
      Next
      StatusBar1.Panels(1).Text = "圖片:" & File1.ListCount & "(張)" + Space(5) + "當前第" & (currpage - 1) * 25 + 1 & "-" & (currpage - 1) * 25 + File1.ListCount Mod 25 & "張"

     Else
      For i = 0 To 24
       Image1(i).Picture = LoadPicture(Dir1.Path + "\" + File1.List((page - 1) * 25 + i))
       Image1(i).ToolTipText = File1.List((page - 1) * 25 + i)
      Next
      StatusBar1.Panels(1).Text = "圖片:" & File1.ListCount & "(張)" + Space(5) + "當前第" & (currpage - 1) * 25 + 1 & "-" & currpage * 25 & "張"

     End If

     StatusBar1.Panels(4).Text = "用時:" & GetTickCount - usetime & "(ms)"
     StatusBar1.Panels(4).Visible = True
    End Sub

    ‘點擊【上一頁】,【下一頁】按鈕事件

    Private Sub Command1_Click(Index As Integer)
     File1.Refresh
     If File1.ListCount = 0 Then Exit Sub
     Select Case Index
      Case 0
       If currpage = (File1.ListCount \ 25 + 1) Then Exit Sub
       currpage = currpage + 1
       Call DisplayPicPage(currpage)
      Case 1
       If currpage = 1 Then Exit Sub
       currpage = currpage - 1
       Call DisplayPicPage(currpage)
      Case 2
       If currindex <> -1 Then
        Image1(currindex).Appearance = 0
       End If
       Call InitShowPic
     End Select
    End Sub

      以上代碼是程序的重點,后兩個函數我沒有注釋,大家可以自己分析一下。最后不要忘了,動態創建的資源要及時釋放,所以在Form_Unload事件中加上以下代碼即可。

    Private Sub Form_Unload(Cancel As Integer)
     Dim i As Integer
     For i = 1 To 24
      Unload Image1(i)
     Next
    End Sub

      在此基礎上,大家可以加上一些圖片處理的功能,現在網絡上有很多此方面的控件下載(比如Polar Draw等等),這樣就可以和ACDsee相媲美了哦~~~

      下圖是我的程序截圖,程序在VB6.0+WINDOWS2000環境下編譯通過。



    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>