How to draw a square box
Hi all,
I know this isn't really related to C++ but I like to know how to scale an axis so that the drawing of a square box is using the correct aspect ratio. Meaning that in different resolution the box is still a box because the control that I have is not exactly equal in height and length. The control cannot be equal in height and length because of spacing. Any how would be appreciated.
[406 byte] By [
93Acura] at [2007-11-19 19:44:23]

# 1 Re: How to draw a square box
Maybe it's me, but I'm having trouble following your post.
CDC::FrameRect() may be what you're looking for, but somehow I doubt it.
GCDEF at 2007-11-10 23:42:55 >

# 2 Re: How to draw a square box
Maybe it's me
No.
May be it is necessary to use absolute coordinates.
# 3 Re: How to draw a square box
Hello,
You can draw in proper aspect ratio by changing your map mode using CDC::SetMapMode with MM_ISOTROPIC as parameter. You can draw in actual cm or inch by using other map modes as well.
Other method is to get the horizontal / vertical resolution (pixel / inch or pixel /cm) using CDC::GetDeviceCaps with LOGPIXELSX / LOGPIXELSY as arguments and using it to drawyour square.
Regards.
Pravin.
# 4 Re: How to draw a square box
You can draw in proper aspect ratio by changing your map mode using CDC::SetMapMode with MM_ISOTROPIC as parameter. You can draw in actual cm or inch by using other map modes as well.Actually... In order to maintain the aspect ration even with non-square pixels, MM_ISOTROPIC won't do - you will need to use MM_ANISOTROPIC, so you can have different scaling factors for the X- and Y-axis.
# 5 Re: How to draw a square box
Hello,
int MapMode = pDC->SetMapMode(MM_ISOTROPIC);
pDC->MoveTo(100, -100);
pDC->LineTo(100, -200);
pDC->LineTo(200, -200);
pDC->LineTo(200, -100);
pDC->LineTo(100, -100);
pDC->SetMapMode(MapMode);
displayes a perfect square. Similar is the case if you use map modes MM_HIMETRIC, MM_LOMETRIC, MM_HIENGLISH or MM_LOENGLISH also. Size of squares will vary in each case. Remember that Y direction is upwards in each of these cases.
If you do not want to change mapmodes, one can calculate the scale factors in X and Y directions using CDC::GetDeviceCaps and draw box using that information. The code given below also gives a perfect square.
int XUnit = pDC->GetDeviceCaps(LOGPIXELSX);
int YUnit = pDC->GetDeviceCaps(LOGPIXELSY);
pDC->MoveTo(XUnit, YUnit);
pDC->LineTo(XUnit, 2 * YUnit);
pDC->LineTo(2 * XUnit, 2 * YUnit);
pDC->LineTo(2 * XUnit, YUnit);
pDC->LineTo(XUnit, YUnit);
Regards.
Pravin.
# 6 Re: How to draw a square box
Hello,
You can draw a square keeping in mind that the width to height ratio of the monitor is 4:3. Use CDC::GetDeviceCaps to get the width and height of the display in pixels and use it draw the square. This way, one can draw figures which will be proportional to the monitor size under any resolution. For example, the code shown below draws a square whose sides are one-eighth of its width, which is equal to one-sixth of its height on any monitor of any resolution.
int Width = pDC->GetDeviceCaps(HORZSIZE) / 8;
int Height = pDC->GetDeviceCaps(VERTSIZE) / 6;
pDC->Rectangle(Width, Height, 2 * Width, 2 * Height);
This way, one can draw figures which will be visually same across monitors.
Regards,
Pravin.
# 7 Re: How to draw a square box
Hello,
You can draw a square keeping in mind that the width to height ratio of the monitor is 4:3...
or 16:10...
# 8 Re: How to draw a square box
or 16:10...
Is it, Vladimir? When I checked on my monitor, CDC::GetDeviceCaps with HORZSIZE & VERTSIZE as parameters returned 320 and 240 exactly!
Regards.
Pravin.
# 9 Re: How to draw a square box
Is it, Vladimir? When I checked on my monitor, CDC::GetDeviceCaps with HORZSIZE & VERTSIZE as parameters returned 320 and 240 exactly!Yes, your monitor - but there are 16:10 monitors as well. And there are also display adapters with non-square pixels, that's why MM_ISOTROPIC won't always do (as I said earlier). Besides that: Using MM_ISOTROPIC or MM_ANISOTROPIC without callin SetWindowExt() / SetViewportExt() / SetWindowOrg() / SetViewportOrg() doesn't make much sense. That's what differentiates these two "free" mapping modes from the others (MM_LOMETRIC, MM_HIENGLISH etc.).
# 10 Re: How to draw a square box
Is it, Vladimir? When I checked on my monitor, CDC::GetDeviceCaps with HORZSIZE & VERTSIZE as parameters returned 320 and 240 exactly!
Is it your phone? I got 1600x1200 on the desk and 1680x1050 on my laptop.
And how about portrait-oriented monitors? Or sub-compact portables?