From: Charlie Gibbs on 9 Apr 2010 12:14 In article <06d96f2b-2e28-474a-85d4-ed8de4e3666c(a)z3g2000yqz.googlegroups.com>, gw7rib(a)aol.com (Paul N) writes: > On 9 Apr, 14:21, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote: > >> And finally, for the sake of efficiency it makes more sense to create >> the font one time only, outside of the WM_PAINT handler. �Keep the >> font handle in memory. Agreed. As much as people rant against global variables, IMHO they're the best thing to use for resource handles - you can have a single cleanup routine, callable from anywhere, that makes sure they're all freed when you exit. > Thanks Scott and Matti. I thinbk I shall have to do some planning here > but you have pointed me in the right direction. > > Just one quick question - is there a value for HFONT which is > guaranteed not to actually be a font? Such as 0? (Like the way a NULL > pointer is used to show that malloc has failed.) This would be handy > as a way to keep track of which fonts I've actually generated. Since CreateFontIndirect() returns NULL when it fails, that's the value to use. Whenever you delete a font, set its handle to NULL and subsequent code can tell that it's been deleted. If you also initialize such handles to NULL, your cleanup routine will always know which fonts have yet to be deleted. -- /~\ cgibbs(a)kltpzyxm.invalid (Charlie Gibbs) \ / I'm really at ac.dekanfrus if you read it the right way. X Top-posted messages will probably be ignored. See RFC1855. / \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!
From: John Smith on 12 Apr 2010 03:47 "ScottMcP [MVP]" <scottmcp(a)mvps.org> wrote: [snip] > >When you use SelectObject (with any type of GDI object) you are >obligated to later restore the state of the HDC to what it was. This >can be done by saving the return from the SelectObject call, and later >selecting the returned object back in to the HDC. Or it can be done >with SaveDC and RestoreDC. > >When you create a font you are obligated to later use DeleteObject to >free it. And, you can't delete it if it is still selected into DC! >So you have a double boo boo here with basic painting rules. > >And finally, for the sake of efficiency it makes more sense to create >the font one time only, outside of the WM_PAINT handler. Keep the >font handle in memory. What about a situation like this hdc=GetDC(mywindow) oldhdc=SelectObject(hdc,GetStockObject(somestockobject)); ...blah blah couple of lines of code for example ReleaseDC(hwnd,hdc); Do I have to select back the oldhdc before releasing the hdc?
From: Bob Smith on 12 Apr 2010 08:21 On 4/9/2010 7:04 AM, Paul N wrote: > My code seems to be working, but I am worried that it may be very > wasteful. The actual code is a bit complicated, but here is a working > cut-down example that (I think) illustrates the point: If I understand you correctly, I wouldn't bother keeping track of the fonts you generated; just delete them after you've used them. Try this code where changes are marked with // ***. HFONT makeitalic(HDC hdc) { // *** LOGFONT lf; HGDIOBJ hgdiobj; HFONT hfont; hgdiobj = GetCurrentObject(hdc, OBJ_FONT); GetObject(hgdiobj, sizeof lf, &lf); lf.lfItalic = TRUE; hfont = CreateFontIndirect(&lf); return SelectObject(hdc, hfont); // *** } (in WndProc) case WM_PAINT: PAINTSTRUCT ps; HDC hdc; HFONT hFontOld; // *** hdc = BeginPaint (hWnd, &ps); TextOut(hdc, 10, 10, _TEXT("Test"), 4); hFontOld = makeitalic(hdc); // *** TextOut(hdc, 10, 30, _TEXT("Italic"), 6); DeleteObject (SelectObject (hdc, hFontOld)); // *** EndPaint (hWnd, &ps); break; -- _________________________________________ Bob Smith -- bsmith(a)sudleydeplacespam.com To reply to me directly, delete "despam".
From: ScottMcP [MVP] on 12 Apr 2010 09:25 On Apr 12, 3:47 am, John Smith <do_not_...(a)invalid.address.com> wrote: > What about a situation like this > > hdc=GetDC(mywindow) > oldhdc=SelectObject(hdc,GetStockObject(somestockobject)); > ..blah blah couple of lines of code for example > ReleaseDC(hwnd,hdc); > > Do I have to select back the oldhdc before releasing the hdc? Yes. Always. As stated in the SelectObject function description.
From: John Smith on 13 Apr 2010 13:26 "ScottMcP [MVP]" <scottmcp(a)mvps.org> wrote: >On Apr 12, 3:47�am, John Smith <do_not_...(a)invalid.address.com> wrote: >> What about a situation like this >> >> hdc=GetDC(mywindow) >> oldhdc=SelectObject(hdc,GetStockObject(somestockobject)); >> ..blah blah couple of lines of code for example >> ReleaseDC(hwnd,hdc); >> >> Do I have to select back the oldhdc before releasing the hdc? > >Yes. Always. As stated in the SelectObject function description. I would think that no memory is lost if in above example. Am I correct?
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: What message control object receive when parent windows is resized? Next: Win32 bitmaps |