none
Contextmenustrip 顯示勾選問題 RRS feed

  • 問題

  • Public Class Form1
        Dim b As Bitmap
        Dim g As Graphics
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim picfile As IO.StreamReader
            Dim menulist As String
            Me.PictureBox1.ContextMenuStrip = ContextMenuStrip1
            picfile = IO.File.OpenText(".\list.txt")
            For i As Integer = 1 To 10
                menulist = Trim(picfile.ReadLine)
                ContextMenuStrip1.Items.Add(menulist)
            Next i

            FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop '設定起始開啟目錄
            TextBox1.Text = "尚未選取資料夾" 'TextBox1用來顯示開啟資料夾
        End Sub


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Pic, PicDir As String
            Dim fl As String

            ListBox1.Items.Clear()          '清除ListBox
            FolderBrowserDialog1.ShowDialog() '利用FolderBrowserDialog可以選取開啟資料夾
            PicDir = FolderBrowserDialog1.SelectedPath '開啟的資料夾
            TextBox1.Text = FolderBrowserDialog1.SelectedPath 'TextBox1用來顯示開啟資料夾

            For Each fl In My.Computer.FileSystem.GetFiles(PicDir, FileIO.SearchOption.SearchTopLevelOnly, "*.jpg", "*.gif", "*.bmp", "*.png")
                Pic = My.Computer.FileSystem.GetName(fl)         '取得相關圖檔檔案名稱
                ListBox1.Items.Add(Pic) '列入檔名
            Next
        End Sub

        Private Sub ListBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick

            If ListBox1.SelectedItem <> Nothing Then '確保已經有檔案名稱在你的ListBox
                PictureBox1.Image = Image.FromFile(TextBox1.Text & "\" & ListBox1.SelectedItem)
                PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
                b = PictureBox1.Image
                g = Graphics.FromImage(b)

            End If

        End Sub
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            Dim zoomx, zoomy As Single
            zoomx = PictureBox1.Image.Width / PictureBox1.Width
            zoomy = PictureBox1.Image.Height / PictureBox1.Height

            If ListBox1.SelectedItem <> Nothing Then '確保已經有檔案名稱在你的ListBox
                If e.Button = Windows.Forms.MouseButtons.Right Then
                    g.FillEllipse(Brushes.Red, New RectangleF(e.X * zoomx - 2, e.Y * zoomy - 2, 4 * zoomx, 4 * zoomy))
                    PictureBox1.Image = b
                End If
            End If

        End Sub

      
     
    End Class

     

    --------------------------------------------------------------------

    Q1:Contextmenustrip 使用file方式去抓txt的欄位名稱卻顯示x1 x2 x3 X4 X5 x6 x7 x8 x9 x10

    (txt檔案內文字每行為: 塔台1 塔台2 塔台3 .....塔台10)

    Q2:在圖片上標記色點的功能可以做成當出現時ContextMenuStrip選單時顯示的選項為check on check嗎?)*不可複選*

    (我只試過ContextMenuStrip裡面加入ToolStripMenuItem 才有check on check選項)

    Q3:已標記的色點可以用右鍵再次點選取消標記嗎?希望用ContextMenuStrip的選單此色點所對應的勾選項目取消來達到色點標記取消
     

    VB2005 express

    2007年8月15日 下午 02:14

解答