Borrowing Device Context Handles
Visual Basic hides the messy details of maintaining the HDC, but it provides the hDC property with forms, picture boxes, and printer objects for those intrepid souls who like messy details. The hDC property is the bridge between the Windows Way and the Basic Way. You can pass it to any Windows function that takes an HDC parameter.
But you needn’t limit yourself to objects that have an hDC property. You can borrow the HDC of anything that has an HWND. Borrow is the key word. If an object has an hDC property, it’s yours; use it however you want with no concern for what happens when you’re done. But if you get the HDC from an HWND, you’d better give it back when you’re finished.
The GetDC API function grabs the device context of the client area. If you like to live on the edge, you can use the GetWindowDC function to grab the whole window—border, title bar, minimize and maximize buttons, control box, and all. I’m not going to tell you how to draw pictures of Larry, Curly, and Moe to replace the minimize, restore, and maximize buttons of your forms, but neither Windows nor Visual Basic will try to stop you.
Whether you get the DC of the window or the DC of the client area only, you must return it with ReleaseDC when you’re done:
‘ Borrow window DC
hDCCur = GetWindowDC(hWndCur)
‘ Use the DC to do work
§
‘ Give DC back or die
Call ReleaseDC(hWndCur, hDCCur)
We’ll talk about other techniques in this code—BitBlt and assigning the Image property to the Picture property—later in this chapter.