Solved

Copying the form on to Clipboard

Posted on 2000-03-07
15
193 Views
Last Modified: 2013-11-25
I have a form in Visual basic that draws a chart on the form itself for the values in the database. I want to provide a button on the form, so that when user clicks the button, I want to copy the form on to the clipboard, so that user can paste the form on to Power point for presentation purposes. Can you suggest me any ideas or example codes?
0
Comment
Question by:ravink
  • 7
  • 6
  • 2
15 Comments
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
You can put a form on the clipboard this way:

Clipboard.Clear
Form1.Picture = Form1.Image
Clipboard.SetData Form1.Picture, vbCFBitmap




Cheers!®©
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
"HOWTO: Copy the Screen or Active Window to the Clipboard from Visual Basic"

http://support.microsoft.com/support/kb/articles/Q240/6/53.ASP
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Condensed version:

Option Explicit

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
   bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12

Private Sub Command1_Click()
   keybd_event VK_MENU, 0, 0, 0
   keybd_event VK_SNAPSHOT, 0, 0, 0
   keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
   keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
End Sub

Private Sub Form_Load()
   Command1.Caption = "Copy Me to ClipBoard"
End Sub
0
 

Author Comment

by:ravink
Comment Utility
mcrider,
Sorry, It is my mistake I mentioned it as form, but it is not a form. It is a UserControl. I created this user control and drawing lines on the control. I can place a button on this control, if we can implement copying mechanism of copying the control on to clipboard in this button. or I can place a button on the form along with this usercontrol, so as to implement the copying mechanism in the button.
0
 

Author Comment

by:ravink
Comment Utility
Erick37
  Your idea did work, but it is copying the other main forms along with this active form on to the clipboard. (The main form is bigger than the active form.)Is there any idea that I can just copy the active form or Just one OCX control on this form onto clipboard.
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Is this an MDI application?
0
 

Author Comment

by:ravink
Comment Utility
Yes, Is it possible to copy just the control on this form to the clipboard.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 14

Expert Comment

by:mcrider
Comment Utility
Take a look at this Microsoft KB article:

HOWTO: Capture and Print the Screen, a Form, or any Window
http://support.microsoft.com/support/kb/articles/Q161/2/99.ASP?LNG=ENG&SA=MSDN&FR=0

You can capture the control and put it into a picture, then put the picture on the clipboard using:

Clipboard.SetData Picture1.Picture, vbCFBitmap



Cheers!®©
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Ok, this code will copy the image of a control located on the form.
Put a picturebox on the form.
In this example, a FileListBox is captured and copied to clipboard:

Option Explicit
Private Declare Function BitBlt Lib "gdi32" _
    (ByVal hDestDC As Long, ByVal x As Long, _
    ByVal y As Long, ByVal nWidth As Long, _
    ByVal nHeight As Long, ByVal hSrcDC As Long, _
    ByVal xSrc As Long, ByVal ySrc As Long, _
    ByVal dwRop As Long) As Long

Private Sub Command1_Click()
    Dim PrevScaleMode As Long
    PrevScaleMode = Me.ScaleMode
    Me.ScaleMode = 3 'pixel
    With File1 'choose the control you wish to copy
    Picture1.Width = .Width
    Picture1.Height = .Height
    Call BitBlt(Picture1.hDC, 0, 0, .Width, .Height, Me.hDC, .Left, .Top, vbSrcCopy)
    Picture1.Refresh
    Picture1.Picture = Picture1.Image
    Clipboard.SetData Picture1.Picture
    Picture1.Picture = LoadPicture("")
    End With
    Me.ScaleMode = PrevScaleMode
End Sub

Private Sub Form_Load()
    Picture1.BorderStyle = 0 'none
    Picture1.AutoRedraw = True
    Picture1.Visible = False
    Picture1.ScaleMode = 3 'pixel
End Sub
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Slight modification:

'...
Clipboard.Clear
Clipboard.SetData Picture1.Picture, vbCFBitmap
'...
0
 

Author Comment

by:ravink
Comment Utility
Erick37
 I tried your code, but it is copying the whole screen with all background onto the clipboard. I tried with Frame control, List view control. Let me know if there is any fix that captures only the control.
Mcrider
  I will try the example you suggested.
Thanks Guys
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Make sure that the properties like .Width refer to the control named in the With statement.  Try to implicitly name them as in the following code.  I retried the code and it copied only the image of the FileListBox to the ClipBoard.  I used an MDI child form loaded by calling Form1.Show from the MDIMain_Load sub.

Child form code:

Option Explicit
Private Declare Function BitBlt Lib "gdi32" _
    (ByVal hDestDC As Long, ByVal x As Long, _
    ByVal y As Long, ByVal nWidth As Long, _
    ByVal nHeight As Long, ByVal hSrcDC As Long, _
    ByVal xSrc As Long, ByVal ySrc As Long, _
    ByVal dwRop As Long) As Long

Private Sub Command1_Click()
    Dim PrevScaleMode As ScaleModeConstants
    PrevScaleMode = Me.ScaleMode
    Me.ScaleMode = 3 'pixel

    Picture1.Width = File1.Width
    Picture1.Height = File1.Height
    Picture1.AutoRedraw = True
    Call BitBlt(Picture1.hDC, 0, 0, File1.Width, File1.Height, Me.hDC, File1.Left, File1.Top, vbSrcCopy)
    Picture1.Refresh
    Picture1.Picture = Picture1.Image
    Clipboard.Clear
    Clipboard.SetData Picture1.Picture, vbCFBitmap
    Picture1.Picture = LoadPicture("")
    Picture1.AutoRedraw = False

    Me.ScaleMode = PrevScaleMode
End Sub

Private Sub Form_Load()
    Picture1.BorderStyle = 0 'none
    Picture1.AutoRedraw = False
    Picture1.Visible = False
    Picture1.ScaleMode = 3 'pixel
End Sub

0
 

Author Comment

by:ravink
Comment Utility
Erick37
 It worked. Earlier my controls were in the splitter control, that is the reason it didn't work. But I moved them out of the spilitter and it worked. Please propose this answer, I will accept. And also, What do we need to do to implement this same thing in Microsoft Access forms?? Treat this as a new question if you want to..
Thanks.
Mcrider, Thanks to you too, for the suggestion of good example.
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 200 total points
Comment Utility
Previous code as answer... Thanks!

Regarding Access forms, I must admit that I do not have Access installed an cannot answer that question.  This ticle may, however, get you started:

"ACC: How to Capture Screens of Your Forms (95/97)"

"The following sample Visual Basic for Applications code captures an object on the screen in the Clipboard. Note that you can change the handle AccessHwnd in the sample code below to create screen shots of other applications, including specific objects within Microsoft Access."

http://support.microsoft.com/support/kb/articles/Q148/3/92.asp
0
 

Author Comment

by:ravink
Comment Utility
Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now