Solved

hWnd to hDc is it possible?

Posted on 2001-08-07
10
1,347 Views
Last Modified: 2010-07-27
I have a commercial ocx that displays images. I want to superimpose another image over the top using my own ocx.

What I would like is to have the new image as a background.

Is it possible ?

If so how?
0
Comment
Question by:mulga
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6359489
Yes.
Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6359713
You should use the ReleaseDC function after you are done using the device context.

Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hWnd As Long, ByVal hDC As Long) As Long



From Microsoft:
============================

The GetDC function retrieves a common, class, or private DC depending on the class style specified for the specified window. For common DCs, GetDC assigns default attributes to the DC each time it is retrieved. For class and private DCs, GetDC leaves the previously assigned attributes unchanged.

After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is limited only by available memory.

Windows 95/98: There are only 5 common DCs available per thread, thus failure to release a DC can prevent other applications from accessing one.
0
 

Author Comment

by:mulga
ID: 6360181
Thanks for the comments. I can use the GetDC/ReleaseDC Ok and it shows the image. What I want is more ambitious, is it possible to show both images at the same time, ie one as a backdrop of the other. When I write either of the images the other gets zapped.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6360296
You mean merging both in one?
0
 
LVL 3

Accepted Solution

by:
adg earned 300 total points
ID: 6361762
Something like this?  Click on the small picture box to copy the pixels to the left.

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 Form_Resize()
    Dim i As Integer
    Randomize Timer
    Picture1.Move 0, 0, ScaleWidth / 2, ScaleHeight
    Picture2.Move ScaleWidth / 2, 0, ScaleWidth / 4, ScaleHeight / 4
    Picture1.ScaleMode = vbTwips
    Picture2.ScaleMode = vbTwips
    For i = 1 To 100
        Picture1.Circle (Rnd * ScaleWidth / 2, Rnd * ScaleHeight), 100, vbRed
    Next i
    For i = 1 To 10
        Picture2.Line (Rnd * ScaleWidth / 4, Rnd * ScaleHeight / 4)-(Rnd * ScaleWidth / 4, Rnd * ScaleHeight / 4), vbGreen, B
    Next i
    Picture1.ScaleMode = vbPixels
    Picture2.ScaleMode = vbPixels
End Sub

Private Sub Picture2_Click()
    Dim x1 As Long
    x1 = Picture2.ScaleWidth / 2
    Picture1.AutoRedraw = False
    BitBlt Picture1.hdc, x1, x1, Picture2.ScaleWidth, Picture2.ScaleHeight, Picture2.hdc, 0, 0, vbSrcCopy
End Sub
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:mulga
ID: 6361997
Yes, I want to merge. One image is displayed on screen and the other is held in memory, it is an aerial photo pulled from disc. The image on screen is displayed in an OCX control that doesn't have a hDc but I can get an hDc from GetDC etc.
There are other things that I need to do but I won't worry about them if it is not possible to merge.

0
 
LVL 3

Expert Comment

by:adg
ID: 6362189
Sorry, I'm not sure if your responding to Richie_Simonetti or me or both.  Can I ask you a few questions?  

1. When you say you want to merge, do you mean copying a smaller bitmap on top of a larger one?  If so, is the smaller image rectangular?

2. The above example draws some circles and lines and then copies the lines on on top of the circles, leaving the circles as a background. I used pictureboxes since I don't have access to your commercial control.  Is there some way I could modify the example to make it more useful to you?  

3. When you say the other image is being held in memory, is it a GDI bitmap or something else?

0
 

Author Comment

by:mulga
ID: 6362755
The bitmap in memory (aerial photo)is a simple GDI and I can convert it if neccessary. I want the background of the aerial photo to show as a background and the commercial OCX to appear as well.
I can show either in the DC but not both together.
 
0
 

Author Comment

by:mulga
ID: 6363010
The bitmap in memory (aerial photo)is a simple GDI and I can convert it if neccessary. I want the background of the aerial photo to show as a background and the commercial OCX to appear as well.
I can show either in the DC but not both together.
 
0
 
LVL 3

Expert Comment

by:adg
ID: 6365161
Sounds good.  That's what my example does.  It takes a smaller bitmap and blits it onto a larger bitmap so they both show. The larger one is the background for the smaller.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now