|
-
Jun 10th, 2013, 01:04 AM
#1
How to Set a Picturebox object to the handle of another Picturebox in another App
App1 has a Picturebox. App2 has App1's Picture1.hwnd.
Is it possible to set an object to App1's Picturebox.hwnd and use that object as a picturebox in App2?
What I am asking is can I have a Picturebox in App2, draw lines and circles on it (like how you would do it in MSPaint) and have the drawings appear on App1's picturebox? I would like the show the results on App1's Picturebox in real-time instead of after the figures have been drawn
I know I can use BitBlt to copy (which would be after the figures have been drawn) because it uses the hwnd but can it be done like I'm asking?
Last edited by jmsrickland; Jun 10th, 2013 at 01:25 AM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jun 10th, 2013, 01:17 AM
#2
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
I don't think you can use the picturebox (of APP1) and transfer it (keeping same handle #) to APP2.
You can use API to take picture from picturebox (of APP1) and put that picture into another (different) picturebox on APP2.
-
Jun 10th, 2013, 01:29 AM
#3
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
 Originally Posted by Max187Boucher
I don't think you can use the picturebox (of APP1) and transfer it (keeping same handle #) to APP2.
You can use API to take picture from picturebox (of APP1) and put that picture into another (different) picturebox on APP2.
Yes, you can transfer App1's Picture1.hwnd to App2 and use it and it will work but all I have been able to do is to use BitBlt
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jun 10th, 2013, 01:59 AM
#4
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
Bearing in mind I don't do graphics, I was surprised at just how 'simple' this was.
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
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 Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Private Sub Command_Click()
Dim lngHandle As Long
Dim lngDCTarget As Long
Dim lngDCSource As Long
Dim lngRet As Long
'
' Find the Source Application
'
lngHandle = FindWindow(vbNullString, "Doogle's Folly")
If lngHandle <> 0 Then
'
' Find the PictureBox handle in the Source Application
'
lngHandle = FindWindowEx(lngHandle, 0&, "ThunderPictureBoxDC", vbNullString)
If lngHandle <> 0 Then
'
' Get the DC of the PictureBox in this Application
'
lngDCTarget = GetDC(pic2.hwnd)
If lngDCTarget <> 0 Then
'
' Get the DC of the PictureBox in the Source Application
'
lngDCSource = GetDC(lngHandle)
If lngDCSource <> 0 Then
'
' Copy the Source Picture to the Target PictureBox
'
lngRet = BitBlt(lngDCTarget, 0&, 0&, pic2.ScaleWidth, pic2.ScaleHeight, lngDCSource, 0&, 0&, SRCCOPY)
If lngRet = 0 Then
Debug.Print "Error in BitBlt " & Err.LastDllError
Else
'
' Release the DCs
'
lngRet = ReleaseDC(lngHandle, lngDCSource)
lngRet = ReleaseDC(pic2.hwnd, lngDCTarget)
End If
Else
Debug.Print "Error obtaining Source DC " & Err.LastDllError
End If
Else
Debug.Print "Error obtaining Target DC " & Err.LastDllError
End If
Else
Debug.Print "Error finding Source PictureBox hwnd " & Err.LastDllError
End If
Else
Debug.Print "Error finding Source Application " & Err.LastDllError
End If
End Sub
EDIT: Just seen JM's edit so I guess they already know how to do this.
-
Jun 10th, 2013, 02:02 AM
#5
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
I suppose you could do all the BitBlt stuff in a Timer event - wouldn't be 'real time' but it would be close.
-
Jun 10th, 2013, 02:16 AM
#6
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
Alternatively, rather than 'pull' the picture from the Source to the Target, would it be possible to 'push' it to the Target from the Source as changes are made?
-
Jun 10th, 2013, 02:38 AM
#7
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
SetParent might simplify matters, such as doing away with the other PictureBox.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jun 10th, 2013, 03:05 AM
#8
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
 Originally Posted by Bonnie West
SetParent might simplify matters, such as doing away with the other PictureBox.
Just tried that and it 'steals' the picture from the Source Application. Also the Source Application 'freezes' (i.e. I can't bring it to the Foreground) and VB (running the 'Folly')aborts when the Target Application (i.e. the one that now has the PictureBox) closes.
I just created a Project with a PictureBox with a Picture in it and a Form Caption of 'Doogle's Folly' and ran it. I then ran this code
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private lngOwner As Long
Private lngHandle As Long
Private Sub Command_Click()
'
' Find the Source Application
'
lngOwner = FindWindow(vbNullString, "Doogle's Folly")
If lngOwner <> 0 Then
'
' Find the PictureBox handle in the Source Application
'
lngHandle = FindWindowEx(lngOwner, 0&, "ThunderPictureBoxDC", vbNullString)
If lngHandle <> 0 Then
'
' Set the PictureBox's Parent to me
'
SetParent lngHandle, Me.hwnd
Else
Debug.Print "Error finding Source PictureBox hwnd " & Err.LastDllError
End If
Else
Debug.Print "Error finding Source Application " & Err.LastDllError
End If
End Sub
I found that I had to 'give it back' to the previous parent before closing, by adding:
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
SetParent lngHandle, lngOwner
End Sub
to avoid VB exploding.
-
Jun 10th, 2013, 03:21 AM
#9
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
I seem to recall from jmsrickland's other thread(s) that App2 runs hidden. In that case, it probably wouldn't matter if App2 couldn't receive the focus or the GUI freezes. But I don't know, I haven't really tried it.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jun 10th, 2013, 03:44 AM
#10
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
 Originally Posted by Bonnie West
I seem to recall from jmsrickland's other thread(s) that App2 runs hidden. In that case, it probably wouldn't matter if App2 couldn't receive the focus or the GUI freezes. But I don't know, I haven't really tried it.
That begs the question "if App2 is hidden, why is it drawing pictures that no one will see?". Wouldn't it be easier for App1 to daw them ? If this is JM's streaming sound system, then both applications have access to the sound data (doesn't App1 send the data to App2?) so both or either could draw the sine waves in real time.
-
Jun 10th, 2013, 03:58 AM
#11
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
 Originally Posted by Doogle
That begs the question "if App2 is hidden, why is it drawing pictures that no one will see?". Wouldn't it be easier for App1 to daw them ? ...
I think I'll let James answer that since I really don't know what he's up to.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jun 10th, 2013, 12:24 PM
#12
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
App1 Shells App2 like this:
Shell App.Path & "\App2.exe " & Picture1.hWnd, vbHide
App1 is what the user sees and App2 is hidden. App2 receives sound data from another App3 in real-time and plays the sound and also displays a sine wave in a picturebox. As the sine wave is being drawn I want to reproduce that in App1 so it can be seen by the user.
App2 is hidden because it needs to be undisturbed by user (such as moving it around and I don't want it to be unmovable). If I only used one App to receive the sound then moving the Form interrupts the incoming sound and then it gets out of sync. So from another thread I have here (http://www.vbforums.com/showthread.p...ing-Processing) Max suggested using a second EXE and after trying various options I decided to take his advise.
Since the sine wave is made as the sound is received then it is drawn in a timer loop as each small chunk of sound data arrives. I know I can use BitBlt in the timer loop to copy from App2's picturebox to App1's picturebox and this works and to the user it would appear as though it is in real-time.
As I get involved in one project I sometimes get carried away and come up with ideas about another project. My idea of the other project was to have two Apps where a user can draw on a picturebox in one App and have it appear on a picturebox in another App as the user draws. So, that is the main reason I started this thread just to see if I can get it done without using BitBlt.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jun 10th, 2013, 01:39 PM
#13
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
OK, I tried the BitBlt method with the hidden app. It works perfectly.
I'm still interested in knowing if I can use the hwnd and treat it like an object in App2. I'm guessing from what I have read it cannot.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jun 11th, 2013, 02:11 AM
#14
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
Bonnie's method of App1 'stealing' the PictureBox from App2, by changing the Parent, seems to work, in the sense that any changes made to it by App2 are reflected in the 'stolen window' in App1. For single instances of App1 I guess it would be OK but if you're going to have multiple App1 'clients' then it's a non-starter.
-
Jun 11th, 2013, 09:51 AM
#15
Re: How to Set a Picturebox object to the handle of another Picturebox in another App
App1 is a client so wouldn't it be a single instance per user?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|