I suspect you intend to do something like:
- print preview or
- an editing area that is an on-screen mock-up of a piece of paper.
Or both (different editors are usually designed this way). As far as I know, this can be done in two ways:
- reading the pixel density of the device (screen, printer) per unit of length (e.g. pixel/inch, pixel/mm) and then calculating the distance and length according to the coefficient obtained in this way - this is what you are trying to do,
- changing the mapping mode (length units, etc.) using the operating system API.
I used to do this in Delphi to support print preview and printing on paper. For this purpose, I used WinAPI functions to draw. First of all, the
SetMapMode function, used to change the mapping mode. Moreover, you can use the following functions:
GetWindowExtEx,
SetWindowExtEx,
GetViewPortExtEx,
SetViewPortExtEx,
GetViewPortOrgEx,
SetViewPortOrgEx.
The starting point is to change the mapping mode. You can then draw using inches or millimeters, or more precisely: 1/100, 1/1000 of an inch or 1/10 or 1/100 mm. There are more of these modes than the ones I mentioned, but these 4 refer to physical units. Their use simplifies the handling of graphics, which are to be both displayed on the monitor (editing, print preview) and later printed on a physical device (printer, plotter).
What is usually done is to first remember the current mapping mode. The
SetMapMode function returns the current mapping mode, which can be stored in a variable. You then turn on the desired mapping mode just before drawing with using of physical units. You can then use the remaining functions that I mentioned earlier (Get.../Set...). Finally, the previous mapping mode is restored using the
SetMapMode function, giving it the previously remembered mapping mode as an argument.
The given WinAPI functions need a handle. This is the canvas handle that is supposed to support drawing. For example, TPaintBox, TCustomControl and TBitmap have a canvas. All you need to do is pass the handle of this canvas to the WinAPI function.
I used a book that described the use of screen graphics and printing in Delphi:
"Delphi 4 Developer's Guide" by Xavier Pacheco, Steve Teixeira