Clipboard tutorial for VB6
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Clipboard tutorial for VB6

  1. #1
    Join Date
    Apr 2003
    Posts
    322

    Clipboard tutorial for VB6

    Can anyone recommend a good Clipboard tutorial for VB6?

    thanks

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Clipboard tutorial for VB6

    Are you looking for Example code or documentation ????

    Gremmy
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,099

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,886

    Re: Clipboard tutorial for VB6

    To add to the nice work of HanneSThEGreaT (as always)...

    API
    Clipboard API

    Basic
    Visual Basic Clipboard Programming

    Also, press F2 and check your Object Browser. This should be your first stop for any object in VB. From there, MSDN, PlanetSourceCode, AllAPI.net and CodeGuru should be on the list.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Apr 2003
    Posts
    322

    Re: Clipboard tutorial for VB6

    Quote Originally Posted by GremlinSA
    Are you looking for Example code or documentation ????

    Gremmy
    A tutorial would be, example code, or an article that explains the process and has supporting VB code examples

  6. #6
    Join Date
    Apr 2003
    Posts
    322

    Re: Clipboard tutorial for VB6

    This is helpfull- thanks

    I know there are different formats of data that can be sent to the clipboard, what I would like to do is, use the clipboard for my application, but if there is any data in the clipboard, preserve it's contents, do a copy & pase in my app, then restore the original contents when my app closes.

    Is it possible to declare a variant to preserve data of any format from the clipboard, or do I need to use variables of the specific data type that is being preserved?
    Last edited by cappy2112; December 9th, 2005 at 04:53 PM.

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Clipboard tutorial for VB6

    VB Books online..
    The Object

    Provides access to the system Clipboard.
    Syntax
    Clipboard
    Remarks
    The Clipboard object is used to manipulate text and graphics on the Clipboard. You can use this object to enable a user to copy, cut, and paste text or graphics in your application. Before copying any material to the Clipboard object, you should clear its contents by as performing a Clear method, such as Clipboard.Clear.
    Note that the Clipboard object is shared by all Windows applications, and thus, the contents are subject to change whenever you switch to another application.
    The Clipboard object can contain several pieces of data as long as each piece is in a different format. For example, you can use the SetData method to put a bitmap on the Clipboard with the vbCFDIB format, and then use the SetText method with the vbCFText format to put text on the Clipboard. You can then use the GetText method to retrieve the text or the GetData method to retrieve the graphic. Data on the Clipboard is lost when another set of data of the same format is placed on the Clipboard either through code or a menu command.
    Methods
    Clear Method
    GetText Method
    GetData Method
    GetFormat Method
    SetData Method
    SetText Method

    GetData Method

    Returns a graphic from the Clipboard object. Doesn't support named arguments.
    Syntax
    object.GetData (format)
    ..
    The GetData method syntax has these parts:
    Part Description

    object Required. An object expression that evaluates to an object in the Applies To list.
    format Optional. A constant or value that specifies the Clipboard graphics format, as described in Settings. Parentheses must enclose the constant or value. If format is 0 or omitted, GetData automatically uses the appropriate format.
    ..
    Settings
    The settings for format are:
    Constant Value Description

    vbCFBitmap 2 Bitmap (.bmp files)
    vbCFMetafile 3 metafile (.wmf files)
    vbCFDIB 8 Device-independent bitmap (DIB)
    vbCFPalette 9 Color palette
    ..
    Remarks
    These constants are listed in the Visual Basic (VB) object library in the Object Browser.
    If no graphic on the Clipboard object matches the expected format, nothing is returned. If only a color palette is present on the Clipboard object, a minimum size (1 x 1) DIB is created.

    Checking data Formats

    You can use the GetFormat method to determine whether the data on the Clipboard is in a particular format. For example, you can disable the Paste command depending on whether the data on the Clipboard is compatible with the currently active control.
    Code:
    Private Sub mnuEdit_Click ()
    ' Click event for the Edit menu.
    	mnuCut.Enabled = True
    	mnuCopy.Enabled = True
    	mnuPaste.Enabled = False
    	If TypeOf Screen.ActiveControl Is TextBox Then
    		If Clipboard.GetFormat(vbCFText) Then mnuPaste.Enabled = True
    	ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
    		If Clipboard.GetFormat(vbCFText) Then mnuPaste.Enabled = True
    	ElseIf TypeOf Screen.ActiveControl Is ListBox Then
    		If Clipboard.GetFormat(vbCFText) Then mnuPaste.Enabled = True
    	ElseIf TypeOf Screen.ActiveControl Is PictureBox _
    			Then
    		If Clipboard.GetFormat(vbCFBitmap) Then mnuPaste.Enabled = True
    	Else
    		' Can't cut or copy from the other types 
    		'	of controls.
    		mnuCut.Enabled = False
    		mnuCopy.Enabled = False
    	End If
    End Sub
    Note You might also want to check for other data formats with the constants vbCFPalette, vbCFDIB, and vbCFMetafile. If you want to replace a picture’s palette using Clipboard operations, you should request vbCFBitmap rather than vbCFDIB from the Clipboard. See "Working with 256 Colors” later in this chapter for more information on working with the color palette.

    Getdata method
    This example uses the GetData method to copy a bitmap from the Clipboard object to a form. To try this example, paste the code into the Declarations section of a form, and then press F5 and click the form.
    Code:
    Private Sub Form_Click ()
    	Const CF_BITMAP = 2	' Define bitmap format.
    	Dim Msg	' Declare variable.
    	On Error Resume Next	' Set up error handling.
    	Msg = "Choose OK to load a bitmap onto the Clipboard."
    	MsgBox Msg	' Display message.
    	Clipboard.Clear	' Clear Clipboard.
    	Clipboard.SetData LoadPicture("PAPER.BMP")  ' Get bitmap.
    	If Err Then
    		Msg = "Can't find the .bmp file."
    		MsgBox Msg	' Display error message.
    		Exit Sub
    	End If
    	Msg = "A bitmap is now on the Clipboard. Choose OK to copy "
    	Msg = Msg & "the bitmap from the Clipboard to the form "
    	MsgBox Msg	' Display message.
    	Picture = Clipboard.GetData()	' Copy from Clipboard.
    	Msg = "Choose OK to clear the form."
    	MsgBox Msg	' Display message.
    	Picture = LoadPicture()	' Clear form.
    End Sub
    SetData Method

    Puts a picture on the Clipboard object using the specified graphic format. Doesn't support named arguments.
    Syntax
    object.SetData data, format
    ..
    The SetData method syntax has these parts:
    Part Description

    object Required. An object expression that evaluates to an object in the Applies To list.
    data Required. A graphic to be placed on the Clipboard object.
    format Optional. A constant or value that specifies one of the Clipboard object formats recognized by Visual Basic, as described in Settings. If format is omitted, SetData automatically determines the graphic format.
    ..
    Settings
    The settings for format are:
    Constant Value Description

    vbCFBitmap 2 Bitmap (.bmp files)
    vbCFMetafile 3 Metafile (.wmf files)
    vbCFDIB 8 Device-independent bitmap (DIB)
    vbCFPalette 9 Color palette
    ..
    Remarks
    These constants are listed in the Visual Basic (VB) object library in the Object Browser.
    You set the graphic that is to be placed onto the Clipboard object with either the LoadPicture function or the Picture property of a Form, Image, or PictureBox.

    Working with the ActiveControl Property

    If you want the Copy, Cut, and Paste commands to work with any text box that has the focus, use the ActiveControl property of the Screen object. The following code provides a reference to whichever control has the focus:
    Screen.ActiveControl

    You can use this fragment just like any other reference to a control. If you know that the control is a text box, you can refer to any of the properties supported for text boxes, including Text, SelText, and SelLength. The following code assumes that the active control is a text box, and uses the SelText property:
    Code:
    Private Sub mnuCopy_Click ()
    	Clipboard.Clear
    	Clipboard.SetText Screen.ActiveControl.SelText
    End Sub
    
    Private Sub mnuCut_Click ()
    	Clipboard.Clear
    	Clipboard.SetText Screen.ActiveControl.SelText
    	Screen.ActiveControl.SelText = ""
    End Sub
    Hope this Helps

    Gremmy
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  8. #8
    Join Date
    Jan 2005
    Posts
    46

    Re: Clipboard tutorial for VB6

    Thank you for sharing with newbies like me. It helps alot.

  9. #9
    Join Date
    Oct 2010
    Posts
    1

    Re: Clipboard tutorial for VB6

    Hello guys.
    I have almost the same problem the original poster, but I need a solution to copy html-text with pictures to the clipboard
    We're using this function at the moment


    Public Sub PutHTMLClipboard(sHtmlFragment As String)
    On Error Resume Next
    Me.MousePointer = vbHourglass
    If RegisterCF <> 0 Then

    Const sContextStart = "<HTML><BODY>"
    Const sContextEnd = "</BODY></HTML>"

    Dim sData As String

    ' 'Build the HTML given the description, the fragment and the context.
    ' 'And, replace the offset place holders in the description with values
    ' 'for the offsets of StartHMTL, EndHTML, StartFragment and EndFragment.

    sData = m_sDescription & sContextStart & sHtmlFragment & sContextEnd
    sData = Replace(sData, "aaaaaaaaaa", _
    Format$(Len(m_sDescription), "0000000000")) 'MLHIDE
    sData = Replace(sData, "bbbbbbbbbb", Format$(Len(sData), "0000000000")) 'MLHIDE
    sData = Replace(sData, "cccccccccc", Format$(Len(m_sDescription & _
    sContextStart), "0000000000")) 'MLHIDE
    sData = Replace(sData, "dddddddddd", Format$(Len(m_sDescription & _
    sContextStart & sHtmlFragment), "0000000000")) 'MLHIDE

    'Add the HTML code to the clipboard
    If CBool(OpenClipboard(0)) Then
    Dim hMemHandle As Long, lpData As Long
    hMemHandle = GlobalAlloc(0, Len(sData) + 10)
    If CBool(hMemHandle) Then
    lpData = GlobalLock(hMemHandle)
    If lpData <> 0 Then
    CopyMemory ByVal lpData, ByVal sData, Len(sData)
    GlobalUnlock hMemHandle
    EmptyClipboard
    SetClipboardData m_cfHTMLClipFormat, hMemHandle
    End If
    End If

    Call CloseClipboard
    End If
    End If
    Me.MousePointer = vbNormal
    End Sub



    The problem is, this one isnt working for special letters like etc.
    Since the normal windows clipboard doesnt have problems with this ones, is there a solutation to use the clipboard like the standard?


    Thanks alot and sorry for my bad english.

    Mller Matthias, Switzerland

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center