|
-
September 8th, 2017, 03:42 PM
#3
Re: GetDC(null) sanity check...Will this DC ever get released?
Is calling GetDC() as the parameter to a method like this good practice?
No. See Victor's post #2.
Should something different be done here?
For every GetDC() used, there must be a corresponding ReleaseDC() as per the MSDN documentation. See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx re ReleaseDC().
Often you'll find code that at the start of a function/block obtains a DC with GetDC() and then at the end of the function/block calls ReleaseDC(). Note that ReleaseDC() requires both the DC handle and the windows handle. Note that even in the case of errors occurring from tested API return values, once GetDC() has been called, then ReleaseDC() still needs to be called. If you're using c++, then a RAII class can be used to obtain a DC and to ensure that ReleaseDC() is always called correctly.
PS Is GetDC() as used in the code in post #1 defined in the program elsewhere - as the standard Windows GetDC() takes a parameter of type HWND?
For a simple c++ class for DC, consider
Code:
class MyDC
{
public:
MyDC(HWND win) : hwnd(win), dc(::GetDC(win)) {}
~MyDC()
{
::ReleaseDC(hwnd, dc);
}
MyDC(const MyDC&) = delete;
MyDC& operator=(const MyDC&) = delete;
HDC GetDC() const
{
return dc;
}
private:
HDC dc;
HWND hwnd;
};
and useage
Code:
...
MyDC mydc(hwnd);
...
m_hOldBmp = (HBITMAP) ::SelectObject(mydc.GetDC(), m_hBmp);
...
Last edited by 2kaud; September 10th, 2017 at 05:23 AM.
Reason: TyPo
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
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
|