Results 1 to 8 of 8

Thread: How long does the screen's device context handle last?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    How long does the screen's device context handle last?

    Say I use GetDC(0) to get an hDC directly to the screen, how long before that number is no longer valid, and I need to get a new hDC for the screen? Does this happen each time the screen is refreshed? Or does the screen's hDC last indefinitely. I vaguely remember reading documentation somewhere that an hDC shouldn't be stored, because hDCs can change, so you should get an hDC right before you need to use it, rather than storing it and using it later.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How long does the screen's device context handle last?

    As a rule of thumb: never store a DC handle unless you are the one creating the DC, since you have control over its destruction. Without that control, you will not have any clue other than documentation, if any. The DC you get is generally valid until you release it. Failing to release it can lead to whatever is using that DC, to not be able to update itself and even generate memory leaks. When retrieving a DC, don't hold on to it any longer than needed to perform the specific action you requested it for.

    Simple example: add a picturebox and command button to a form. In the command button, add this
    Code:
    Picture1.AutoRedraw = False
    Debug.Print Picture1.hDC; 
    Picture1.BackColor = vbWhite
    Debug.Print Picture1.hDC
    ' now try it with auto-redraw
    Picture1.AutoRedraw = True
    Debug.Print Picture1.hDC; 
    Picture1.BackColor = vbCyan
    Debug.Print Picture1.hDC
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: How long does the screen's device context handle last?

    Quote Originally Posted by LaVolpe View Post
    As a rule of thumb: never store a DC handle unless you are the one creating the DC, since you have control over its destruction. Without that control, you will not have any clue other than documentation, if any. The DC you get is generally valid until you release it. Failing to release it can lead to whatever is using that DC, to not be able to update itself and even generate memory leaks. When retrieving a DC, don't hold on to it any longer than needed to perform the specific action you requested it for.

    Simple example: add a picturebox and command button to a form. In the command button, add this
    Code:
    Picture1.AutoRedraw = False
    Debug.Print Picture1.hDC; 
    Picture1.BackColor = vbWhite
    Debug.Print Picture1.hDC
    ' now try it with auto-redraw
    Picture1.AutoRedraw = True
    Debug.Print Picture1.hDC; 
    Picture1.BackColor = vbCyan
    Debug.Print Picture1.hDC

    I was wondering in the context of a video player. I've got a series of images that I want to display to a the screen directly. I have a loop for playing the series of images, that uses SetDIBitsToDevice to directly draw the video to the screen. Currently, within the loop there is a ScreenDC=GetDC(0) to get the DC (then stored in the Long-type variable ScreenDC), after that comes the SetDIBitsToDevice command (which uses that ScreenDC to draw a frame directly to the screen), and then after that I use ReleaseDC 0,ScreenDC to release the screen's DC. Now for optimizing the program, I would like to move the GetDC and ReleaseDC out of the loop (GetDC just before the video playing loop, and ReleaseDC just after the video playing loop). But I'm not sure how long the hDC can be stored safely. I would hate to have to call GetDC and ReleaseDC for every single frame (as it does now by having these functions in the loop itself), but I also don't want to make a buggy program. I'm not sure how long the screen's hDC (the hDC you get when you call GetDC(0)) remains valid. If somebody can let me know, it would be a big help.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How long does the screen's device context handle last?

    Keep them in the loop. Other windows, including the desktop itself, may be retrieving the desktop DC for their own purposes. Another option is to create your own window and render the video frames to that?

    Until you call ReleaseDC, I doubt other apps, including the desktop, can update the desktop properly, i.e., artifacts. Retrieving & releasing the DC would be very quick. You are not forcing creation of DCs, just retrieving a handle to an existing DC.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: How long does the screen's device context handle last?

    Quote Originally Posted by LaVolpe View Post
    Keep them in the loop. Other windows, including the desktop itself, may be retrieving the desktop DC for their own purposes. Another option is to create your own window and render the video frames to that?

    Until you call ReleaseDC, I doubt other apps, including the desktop, can update the desktop properly, i.e., artifacts. Retrieving & releasing the DC would be very quick. You are not forcing creation of DCs, just retrieving a handle to an existing DC.
    Isn't it possible for more than one program to access an hDC for a device? Each call of hDC from a different program, should return a different value for the hDC, so each program has a different handle (hDC) for the same device context (DC). So theoretically a DC is a shared resource, with each program using a different hDC for the same DC. So there should theoretically be no conflict. Just like different programs can simultaniously access the same file and read/write to it, because each program has a different handle (hFile) for the same file. Only exception to that is if one program locks the file, so that no other program may access it. And I don't think it's possible for a program to lock a DC, such that no other program can use it.

    I've never found that the screen's DC can be locked in such a way as to prevent any other program (including the OS itself) from updating the screen.

    How many milliseconds does it take to get and release an hDC?

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How long does the screen's device context handle last?

    Per MSDN
    The GetDC function retrieves a common, class, or private DC depending on the class style of the specified window. For class and private DCs, GetDC leaves the previously assigned attributes unchanged. However, for common DCs, GetDC assigns default attributes to the DC each time it is retrieved.
    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.
    Obviously it depends on whether the DC you are using is a common or class/private DC. For more information on the different types of DC, see this msdn article
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: How long does the screen's device context handle last?

    Quote Originally Posted by LaVolpe View Post
    Per MSDN


    Obviously it depends on whether the DC you are using is a common or class/private DC. For more information on the different types of DC, see this msdn article

    What about the screen's DC? You know, the one who's hDC you get by calling GetDC(0). How does it behave? It is unique because it refers to the entire visible screen. Does MSDN have any special instructions for working with handles to the whole screen's DC?

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How long does the screen's device context handle last?

    Here's a simple test I performed.

    1. Created form with 3 buttons
    2. For 2 of the buttons, they cached value of GetDC(0) if not already done, then filled a rectangle on the DC (different colors & sizes)
    3. Third button released the DCs

    As each button was clicked the rectangles were drawn over the previous rectangles correctly & completely. This surely indicates that each button rendered properly on the desktop with each copy of GetDC(0)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width