In this exercise, you fix the button size and layout. To do so, scale the size of the buttons from raw pixels to relative pixels to appropriately display the text size, and then scale the layout of the buttons from raw pixels to relative pixels to fix the button layout.
For this exercise, continue using the project file from the previous exercise.
- Open the HandsOnLab.cpp file. In the DemoApp::CreateChildWindows method, find the section where button initialization is specified.
Add the following code (shown in bold) to scale the size of all three buttons:// Now set up the size of the buttons that we will create.
SIZE buttonSize = { 60, 26 };
g_metrics.ScaleSize(&buttonSize);
Help:
The
g_metrics variable is an instance of the
CDPI class, and the
g_metrics.ScaleSize method scales from raw pixels to relative pixels.
Save the file, recompile, and then run the application. The application appears as shown in the following screen shot, with the appropriately scaled button size:
In the DemoApp::CreateChildWindows method, add the following code (shown in bold):
// Button 1 position.
POINT ptButton1Pos = { 10, 55 };
g_metrics.ScalePoint(&ptButton1Pos);
// Button 2 position is 75 pixels to the right of Button 1.
POINT ptButton2Pos = { 85, 55 };
g_metrics.ScalePoint(&ptButton2Pos);
// Button 3 is 75 pixels to the right of button 2.
POINT ptButton3Pos = { 160, 55 };
g_metrics.ScalePoint(&ptButton3Pos);
Help
The
g_metrics.ScalePoint method scales points from raw pixels to relative pixels.
Save the file, recompile, and then run the application. The application should look like the following screen shot, with the appropriately scaled button size and layout: