Created
November 22, 2011 10:28
-
-
Save pke/1385377 to your computer and use it in GitHub Desktop.
WTL GDI extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class T = HGDIOBJ> | |
class COldObject { | |
public: | |
typedef T (WINAPI* restore)(HDC hdc, T old); | |
COldObject(HDC dc, T old, restore func = SelectObject) : dc(dc), old(old), func(func) {} | |
~COldObject() { | |
func(dc, (T)old); | |
} | |
operator T() const { | |
return old; | |
} | |
private: | |
T old; | |
restore func; | |
HDC dc; | |
}; | |
typedef COldObject<HGDIOBJ> COldFont; | |
typedef COldObject<HGDIOBJ> COldBitmap; | |
typedef COldObject<HGDIOBJ> COldPen; | |
typedef COldObject<HGDIOBJ> COldBrush; | |
class COldBkMode : public COldObject<int> { | |
public: | |
COldBkMode(HDC hdc, int old) : COldObject<int>(hdc, old, SetBkMode) {} | |
}; | |
class COldTextColor : public COldObject<COLORREF> { | |
public: | |
COldTextColor(HDC hdc, COLORREF old) : COldObject<COLORREF>(hdc, old, SetTextColor) {} | |
}; | |
class COldBkColor : public COldObject<COLORREF> { | |
public: | |
COldBkColor(HDC hdc, COLORREF old) : COldObject<COLORREF>(hdc, old, SetBkColor) {} | |
}; | |
/** | |
Use this when you heavily manipulate a DC and want to restore its state quickly. | |
@author_philk | |
*/ | |
class CSaveDC { | |
public: | |
CSaveDC(HDC hdc) : hdc(hdc), id(::SaveDC(hdc)) {} | |
~CSaveDC() { | |
ATLVERIFY(::RestoreDC(hdc, id)); | |
} | |
private: | |
HDC hdc; | |
const int id; | |
}; | |
// Usage | |
// WTL::COldFont oldFont(dc, dc.SelectFont(newFont)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment