Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Copying the form on to Clipboard

Posted on 2000-03-07
15
205 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
ID: 2594238
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
ID: 2594356
"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
ID: 2594375
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ravink
ID: 2594423
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
ID: 2594431
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
ID: 2594438
Is this an MDI application?
0
 

Author Comment

by:ravink
ID: 2594460
Yes, Is it possible to copy just the control on this form to the clipboard.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2594488
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
ID: 2594497
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
ID: 2594566
Slight modification:

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

Author Comment

by:ravink
ID: 2598481
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
ID: 2598559
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
ID: 2601830
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
ID: 2601889
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
ID: 2602177
Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

809 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