2

What is the difference between GetDC(hwnd) and GetDC(NULL)? I understand that the latter gets a DC for the entire (virtual) screen, but I'm not sure what that means practically.

I want to set the size of an initial window based on the font in use. I use CreateFontIndirect to create the font handle but, in my opinion, only when you SelectObject that font into a DC can you use GetTextMetrics to figure out the actual height used rather than one specified. I'm going on the assumption that they may not be the same.

I would normally use GetDC(hwnd) to get a DC and select the font into it. But, given I don't have a hwnd yet, can I select a font into a DC returned from GetDC(NULL)?

Edit: Related. I guess it may make no difference! GetDC(NULL) gets primary monitor or virtual screen?

Community
  • 1
  • 1
carveone
  • 889
  • 9
  • 16

1 Answers1

4

You don't own the screen DC and should not select objects into it. What you can do is use CreateCompatibleDC, passing the screen DC, to get a DC into which you can perform text metrics calculations.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Ah, bother. I guess all I can use it for it to get infomation (eg: GetDeviceCaps). I figured selecting objects into the screen DC might turn out badly alright. Thanks! – carveone Sep 11 '14 at 14:54
  • This works just fine. I found it interesting that (for Segoe UI, 9pt) the logfont lfHeight is 12 while the textmetrics tmHeight is 15. TextMetrics is the one to use I guess! – carveone Sep 11 '14 at 15:37
  • Are those two numbers comparable? Do they represent the same thing? – David Heffernan Sep 11 '14 at 15:45
  • Originally I thought they were but I'm evidently wrong. From MSDN - (LOGFONT)lfHeight is "The height, in logical units, of the font's character cell or character" and (TEXTMETRIC)tmHeight is "The height (ascent + descent) in characters" (also in logical units). I believe the difference is the descent. – carveone Sep 11 '14 at 16:43
  • @DavidHeffernan Do you know of any documentation/references regarding that limitation? I'm looking through the Delphi VCL source code and there are a number of places that use GetDC(null) followed by SelectObject to get text metrics (e.g., TCustomLabel.AdjustBounds). Should those all be using CreateCompatibleDC(null) or CreateCompatibleDC(ScreenDC) instead? – Zoë Peterson Feb 02 '21 at 22:58