Solved

How to "know" a control having his hDC

Posted on 2002-07-02
17
169 Views
Last Modified: 2008-02-01
I've a class that paints on a DC.

How can i "know" what control have this DC, to use his "refresh" method from the class?
0
Comment
Question by:parduz
17 Comments
 
LVL 6

Expert Comment

by:pierrecampe
Comment Utility
????
please elaborate on your question
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Hmm, interesting! Basically all graphical component (like form object, picturebox control) has the hDC Property to paint the hDC (handle device? If not wrong!)

Maybe you can tell us what you intend to do?

regards
0
 

Author Comment

by:parduz
Comment Utility
Ok!

I have a class that, having a destination DC, creates compatible DC and bitmaps, draw somewhat and bitblt on that destination DC.
The problem is that, if the destination DC comes from a picbox, you must refresh this picbox outside the class ('cause it does'nt have a reference to teh picbox, but only the DC).

So the question: how to know what control have a specific DC, to use a code like:

DCOwner=RetrieveDcOwner (hDC)
DCOwner.refresh

Is Window knowing who have a specific DC?
0
 

Author Comment

by:parduz
Comment Utility
Ok!

I have a class that, having a destination DC, creates compatible DC and bitmaps, draw somewhat and bitblt on that destination DC.
The problem is that, if the destination DC comes from a picbox, you must refresh this picbox outside the class ('cause it does'nt have a reference to teh picbox, but only the DC).

So the question: how to know what control have a specific DC, to use a code like:

DCOwner=RetrieveDcOwner (hDC)
DCOwner.refresh

Is Window knowing who have a specific DC?
0
 
LVL 6

Expert Comment

by:pierrecampe
Comment Utility
i dont quite understand what you mean
for a class to bitblt to a device context that class has to know to what devise context, so there has to be a routine that passes a handle to that device context to that class,so that routine knows the hDC of what picture box it has passed to the class (or so i think)  
if for some reason there is no routine that knows the hDC,make a routine that gets that hDC from that class and then use code such as:

Dim c As Control
For Each c In Controls
    If TypeOf c Is PictureBox Then
        If c.hDC = TheDC Then
            c.Refresh
        End If
    End If
Next
where 'TheDC' is the hDC you got from the class
however i may be completely off for i dont fully understand what you mean
0
 

Author Comment

by:parduz
Comment Utility
I'm sorry, i know that my english is a nightmare.

My problem is the opposite of your sample.
Your sample find the hDC of a known picbox.
I need to find what picbox have a known hDC.
The problem is that my class can't look
in the form controls. So i need a way to retrieve what control have/owns/retains/... (no more words!) a known hDC

0
 
LVL 6

Expert Comment

by:pierrecampe
Comment Utility
>>My problem is the opposite of your sample.
could be, but i dont think so
>>Your sample find the hDC of a known picbox.
no it does not
it finds the picbox that as a known hDC
>>I need to find what picbox have a known hDC.
that is what my example does
>>The problem is that my class can't look
>>in the form controls. So i need a way to retrieve what >>control have/owns/retains/... (no more words!) a known hDC
your class does not have to look in the form controls
a routine in the form asks the hDC from the class
and then searches for a picbox with that hDC
if it is not clear, you can post here your class code
and the code how you use the class
 
0
 

Author Comment

by:parduz
Comment Utility
>>it finds the picbox that as a known hDC
>>... cut ...
>>a routine in the form asks the hDC from the class
>>and then searches for a picbox with that hDC
When? How the form known when execute that routine?
The form "know" all controls on it.
If the picbox is on a form, i can execute the same routine from the class, using "For Each c In Form1.Controls".
But if the picbox is on a OCX surface, the class can't access the "usercontrol.Controls" collection.

No. My class make the job and finally it bitblt on the hDC the class know as destination.

The object witch have this hDC can be on a form, on a ocx surface, maybe on a form of another running application... my class really don't know that.
It needs to execute the "refresh" method itself...but how to have a reference to a picbox having his hDC?
0
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

 
LVL 6

Expert Comment

by:pierrecampe
Comment Utility
whatever surface uses your class,a routine in that surface has to send a hDC to your class, if not how can your class know what hDC to use ???
so whatever surface uses your class it knows what object has that hDC
>>No. My class make the job and finally it bitblt on the >>hDC the class know as destination.
and how does your class know what hDC to bitblt on ?



0
 

Author Comment

by:parduz
Comment Utility
>>whatever surface uses your class,a routine in that surface has to send a hDC to your class,

No surfaces uses my class. Is my class that works on surfaces.

>> if not how can your class know what hDC to use ???
The hDC is passed to my class by other object/apps that can
be destroyed while my class works. My class have all needed to works, a Destination hDC.

Is too long to explain why i need this (and i'm not able to do it in english, sorry).

I try to modify the question: if i give you a long variable, telling you that this long is a Device Context, can you return to me a reference to the object (in one of the running Windows tasks) have this hDC?

0
 
LVL 6

Expert Comment

by:pierrecampe
Comment Utility
>>No surfaces uses my class. Is my class that works on surfaces.
well i was just using your terminology
>>The hDC is passed to my class by other object/apps that can be destroyed while my class works.
are you telling me that you are bitblt'ing into a device context that no longer exists ?
>>Is too long to explain why ...
well i already asked you to put your class here
and how to use its methods
but if you dont want to do that...well then thats it  
>>I try to modify the question:...
Declare Function WindowFromDC Lib "user32" (ByVal hdc As Long) As Long
0
 

Author Comment

by:parduz
Comment Utility
Ok, PierreCampe, i know that it's hard understanding me.

If you already like to help me, try with this "simulation" of my working condition.

Open a standard exe project, place a picbox on the form, and a button that write the hDC, the scalewidth and the scaleheight (in pixels) of the picbox on the form. Compile and run it.

Open another project, put 3 textboxes and a button on the form, then create a class with a "Set_Destination" method.

Write the hdc, width and height in the three textboxes, and
whith the button, call
  DrawClass.Set_Destination (Clng(val(textDC)),clng(val(textW)),clng(val(textH)))

In the Set_Destination code, build a compatibleDC using the hDC parameter, build a compatible bitmap end select it in the compatibleDC.

Now draw what you want on this bitmap (i use polylines, but i think it's not important now) and finally bitblt your bitmap on the hDC you know is from the picbox of the first app.

If all run like to me, you will see nothing, 'cause you can't call the picbox refresh method.

So how can you refresh that picbox, having only the hDC?



Just in case you like to laugth a bit:
In real life, the first app is somewhat i've found to the customer, written in VB by a dead programmer, without a line of source code about it. It's a BIIIIG app, that it's impossible to rewrite in a time lesser than a year.
The way i have to know the hDC of the picbox i need to redraw is with an unknown OCX, written by the same programmer (again, i can't find a line of source code). This OCX exposes a lot (43) of property/method, but each one generate a error (really!!) that crashes the app wich use this OCX (no error trapping possible!!!). The only working method is the "ReturnMainScreenDC", that is the hDC i need for drawing, then my app can work, even if another call to a method/property of this OCX generate a windows blue screen (so i can't use more of one time per boot this OCX).

If i can find a way to refresh that picbox, i will solve all my problems, and a little of my customer problems too, until i rewrite the whole things.

What a mess, huh?
0
 

Author Comment

by:parduz
Comment Utility
Ok, PierreCampe, i know that it's hard understanding me.

If you already like to help me, try with this "simulation" of my working condition.

Open a standard exe project, place a picbox on the form, and a button that write the hDC, the scalewidth and the scaleheight (in pixels) of the picbox on the form. Compile and run it.

Open another project, put 3 textboxes and a button on the form, then create a class with a "Set_Destination" method.

Write the hdc, width and height in the three textboxes, and
whith the button, call
  DrawClass.Set_Destination (Clng(val(textDC)),clng(val(textW)),clng(val(textH)))

In the Set_Destination code, build a compatibleDC using the hDC parameter, build a compatible bitmap end select it in the compatibleDC.

Now draw what you want on this bitmap (i use polylines, but i think it's not important now) and finally bitblt your bitmap on the hDC you know is from the picbox of the first app.

If all run like to me, you will see nothing, 'cause you can't call the picbox refresh method.

So how can you refresh that picbox, having only the hDC?



Just in case you like to laugth a bit:
In real life, the first app is somewhat i've found to the customer, written in VB by a dead programmer, without a line of source code about it. It's a BIIIIG app, that it's impossible to rewrite in a time lesser than a year.
The way i have to know the hDC of the picbox i need to redraw is with an unknown OCX, written by the same programmer (again, i can't find a line of source code). This OCX exposes a lot (43) of property/method, but each one generate a error (really!!) that crashes the app wich use this OCX (no error trapping possible!!!). The only working method is the "ReturnMainScreenDC", that is the hDC i need for drawing, then my app can work, even if another call to a method/property of this OCX generate a windows blue screen (so i can't use more of one time per boot this OCX).

If i can find a way to refresh that picbox, i will solve all my problems, and a little of my customer problems too, until i rewrite the whole things.

What a mess, huh?
0
 
LVL 6

Accepted Solution

by:
pierrecampe earned 300 total points
Comment Utility
i did what you suggested(almost)and this are my findings:
(btw it would have been easier if you had supplied the code)
seems to me a picbox has 2 kinds of device context
one when the autoredraw is true
and an other when the autoredraw is false
i made a prog with a picbox that prints the hDC of that picbox,it also had a button to toggle the autoredraw of the picbox true/false
then i made the following prog:

Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function WindowFromDC Lib "user32" (ByVal hdc As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Sub Command1_Click()
    Dim a As Long
    Dim b As Long
    Dim strString As String
    a = WindowFromDC(Val(Text1))
    strString = "AAAAAAAAAAAAA"
    b = TextOut(GetDC(a), 0, 0, strString, Len(strString))
End Sub
turns out that this prog can write to the first prog picbox if that picbox autoredraw is false
now if i toggle the autoredraw to true (so it prints the other hDC)
the second prog can still write to the picbox IF and ONLY IF the first hDC is used
when the second hDC is used it writes to the screen
strange behaviour
and what was strange to, if the printed hDC was used directly it did not work at all
the hDC HAD to be fed to windowfromdc
maybe you could post the real code you use to print to that picbox
>>What a mess, huh?
yeah


0
 
LVL 6

Expert Comment

by:pierrecampe
Comment Utility
any progres ?
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Hi parduz,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept pierrecampe's comment(s) as an answer.

parduz, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

763 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

12 Experts available now in Live!

Get 1:1 Help Now