{"id":180,"date":"2005-12-01T13:50:59","date_gmt":"2005-12-01T21:50:59","guid":{"rendered":"http:\/\/spacefold.com\/colin\/morethanfour\/?p=180"},"modified":"2020-08-16T13:51:35","modified_gmt":"2020-08-16T21:51:35","slug":"fontmetric-txtwidth-and-screen-dpi-in-windows","status":"publish","type":"post","link":"https:\/\/spacefold.com\/colin\/morethanfour\/2005\/12\/01\/fontmetric-txtwidth-and-screen-dpi-in-windows\/","title":{"rendered":"FONTMETRIC(), TXTWIDTH(), and screen DPI in Windows"},"content":{"rendered":"\n<p>If you are trying to size objects on a VFP form to match a specific font, you have to take into account the current screen DPI setting as well. Consider sizing a Rectangle object to surround a given piece of text. You know the fontname, fontsize, and the actual text string, but you don&#8217;t know the dimensions. No problem, you say, I&#8217;ll just use FONTMETRIC() and TXTWIDTH() to figure out the size of the text in pixels:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iAvgCharWidth = FONTMETRIC(6, cFontName, cFontSize, \"\" )\niTextWidth = TXTWIDTH( cString, cFontName, cFontSize, \"\" ) * iAvgCharWidth\n<\/pre>\n\n\n\n<p>&#8230;and now I can size my Rectangle appropriately, you say.<\/p>\n\n\n\n<p>Not so fast. What if your Windows display is set to 120 DPI? The text will be sized differently. You have to take this into account:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">*---------------------------------\n* Determine the screen DPI:\n*---------------------------------\n#define LOGPIXELSX 88\n\ndeclare integer GetDeviceCaps in WIN32API integer HDC, integer item\ndeclare integer GetDC         in WIN32API integer hWnd\n*declare integer DeleteDC      in WIN32API integer HDC\ndeclare integer ReleaseDC     in WIN32API integer hWnd, integer HDC\n\nlocal hdc, screenDPI\nhdc    = GetDC(0)\nTHIS.screenDPI = GetDeviceCaps( m.hdc, LOGPIXELSX )\nReleaseDC( 0, m.hdc )\n\nIF iScreenDPI &lt;&gt; 96\n   iTextWidth = INT( (iTextWidth * iScreenDPI)\/96 )\nENDIF\n<\/pre>\n\n\n\n<p>I was grappling with issue recently, dealing with a bug reported by someone who said that Pageframe text captions were getting truncated when they set their environment to 120DPI + &#8220;Large Fonts&#8221;.<\/p>\n\n\n\n<p>Well, this morning I have read a&nbsp;<a href=\"http:\/\/www.charlespetzold.com\/blog\/blog.xml#0511250723\">post by the remarkable Charles Petzold<\/a>&nbsp;about Device Independent Units (DIU) that goes some way (and more) to explaining why we have to do it, because I believe that Visual FoxPro uses actual pixels rather than DIU. His post his worth a read.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are trying to size objects on a VFP form to match a specific font, you have to take into account the current screen DPI setting as well. Consider sizing a Rectangle object to surround a given piece of text. You know the fontname, fontsize, and the actual text string, but you don&#8217;t know [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-foxpro","post-preview"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}