| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 其他 >> 实例分析正文
最新推荐 更多内容
剪贴板的实例
剪贴板的实例
作者:佚名    来源:乐博网收集     更新时间:2007-12-4

剪贴板的实例代码如下:
Imports System.IO

Public Class frmMain

    Private Sub btnGetImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetImage.Click

        'just some stupid generic names to just get by with
        Dim a As String = Nothing
        Dim b As Collection = New Collection

        Try

            If Clipboard.ContainsImage Then

                'the ContainsImage function seems to me to be alittle
                'messed up. If you set the image via the SetImage Sub
                'then the clipboard will reconize the data as a image.
                'If you copy from your right mouse click or something,
                'the ContainsImage will return false for some reason.
                'So, that is the reason I wrote the code below. So
                'whether the image is copied via the SetImage Sub or
                'via the mouse or whatever. As long as the image is
                'either a bitmap, gif, or jpeg. Then it will be pasted
                'to the picturebox.
                pic.Image = Clipboard.GetImage

                'don't go any further since the clipboard did auto
                'detect that it contained a image
                Exit Sub

            End If

            'make sure there is something in the FileList to check if it
            'contains a image or not first
            If Clipboard.GetFileDropList.Count > 0 Then

                For Each a In My.Computer.Clipboard.GetFileDropList

                    b.Add(a)

                Next

            Else

                'throw a message and then exit the sub if there are no
                'items in the DropFileList
                MessageBox.Show("There does not appear to be any image available to get from the Clipboards FileDropList.", " No Image", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub

            End If

            'Since I am just making a example, I will just make the code
            'paste only the first item in the collection. First make sure
            'that the item in the clipboard is a picture or not. The
            'built-in GetImage Function does not seem to work correctly.
            'So, I will write alittle code to check for 3 or so picture
            'formats to make sure there is a pic available to paste

            'first make sure make there is a item available
            If b.Count > 0 Then

                'I am going to just care about the first item in the list
                'just to show as a example. There could be many upon many
                'items contained in the clipboards FileDropList.
                a = LCase(b.Item(1).ToString)

            Else

                MessageBox.Show("There are not any items in the clipboard.", " Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub

            End If

            'check to see if the clipboard contains any of the image
            'formats specified in the below code.
            If a.EndsWith(".bmp") Then

                pic.Image = Image.FromFile(a.ToString)

            ElseIf a.EndsWith(".gif") Then

                pic.Image = Image.FromFile(a.ToString)

            ElseIf a.EndsWith(".jpg") Then

                pic.Image = Image.FromFile(a.ToString)

            Else

                'there is not a bmp, gif or jpeg image in the clipboard
                MessageBox.Show("The clipboard does not contain either a Bitmap, Gif, or JPeg image to paste from!", " No Image to Paste", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub

            End If

        Catch exc As Exception

            MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnGetItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetItems.Click

        Dim item As String = Nothing

        Try

            'check to see if there is a filedroplist
            If Clipboard.ContainsFileDropList <> 0 Then

                'go through the List and add to the listbox control
                For Each item In Clipboard.GetFileDropList

                    lstFileDropItems.Items.Add(item)

                Next

            Else

                'throw a messagebox if there is no filedroplist
                MessageBox.Show("There does not appear to be a FileDropList with any items in it!", " No FileDropList", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub

            End If

        Catch exc As Exception

            MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnSetText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetText.Click

        Try

            'first check to see whether the textbox has any text that can
            'be copied to the clipboard
            If Not txtText.Text = "" Then

                'there is text in the textbox, so copy the text to the
                'clipboard
                My.Computer.Clipboard.SetText(txtText.Text)

            Else

                'the textbox control does not contain any text, so add
                'just some generic text to the clipboard instead.
                My.Computer.Clipboard.SetText("This is just a test!")

            End If

        Catch exc As Exception

            MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnGetText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetText.Click
        Try

            'check to see whether the clipboard has any text to paste
            'first before doing anything else
            If My.Computer.Clipboard.ContainsText Then

                txtText.Text = My.Computer.Clipboard.GetText.ToString

            Else

                'the clipboard does not contain text
                MessageBox.Show("The clipboard does not contain any text to paste to the textbox control!", " No TEXT in Clipboard", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If

        Catch exc As Exception

            MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnLoadInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadInfo.Click
        Try

            'check to see if the clipboard contains any of the spefified
            'data below and put the result in the label controls
            lblContainsAudio.Text = "Contains Audio: " & My.Computer.Clipboard.ContainsAudio.ToString
            lblFileDropList.Text = "Contains Drop List: " & My.Computer.Clipboard.ContainsFileDropList.ToString
            lblContainsImage.Text = "Contains Image: " & My.Computer.Clipboard.ContainsImage.ToString
            lblContainsText.Text = "Contains Text: " & My.Computer.Clipboard.ContainsText.ToString

        Catch exc As Exception

            MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        Try

            DialogResult = _
                MessageBox.Show("Are you sure you want to clear?", _
                    " Clear the clipboard?", MessageBoxButtons.YesNo, _
                        MessageBoxIcon.None)

            If DialogResult = Windows.Forms.DialogResult.Yes Then

                'very easy to clear the clipboard
                My.Computer.Clipboard.Clear()

            End If

        Catch exc As Exception

            MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnSetSelectImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetSelectImage.Click

        Try

            'declare a new openfiledialog object
            Dim openDLG As OpenFileDialog = New OpenFileDialog

            'setup the filter to allow bitmap, gif, and jpeg images
            'only()
            openDLG.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg"

            'so we know the result of the dialog
            DialogResult = openDLG.ShowDialog

            'make sure ok was clicked
            If DialogResult = Windows.Forms.DialogResult.OK Then

                'first clear the clipboard
                Clipboard.Clear()

                'set the selected image to the clipboard
                Clipboard.SetImage(Image.FromFile(openDLG.FileName))

                'if returns true, then the image was successfully set
                'to the clipboard
                If Clipboard.ContainsImage Then

                    MessageBox.Show("The image should now be pasted in the clipboard.", " Image Successfully Pasted!", MessageBoxButtons.OK, MessageBoxIcon.Information)

                Else

                    MessageBox.Show("The image does not appear to have been successfully pasted to the clipboard!", " Possible Problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                End If

            End If

        Catch exc As Exception

            MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

End Class

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net获取文件和目录完整路径的实例
    VB.Net读取音频文件并复制到剪贴板的实例
    VB.Net剪贴板实例(2)
    VB.Net剪贴板实例(1)
    VB.Net剪贴板实例学习笔记.02.[乐博网]
    VB.Net剪贴板实例学习笔记.01.[乐博网]
    从剪贴板读取数据的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]