sunlight

Controls

Introduction

In this part of the tutorial, we will cover:

  • Controls in dialog boxes

Previous View Code Download Code Next

Control Types

Controls in dialog boxes fall into one of four categories:

  • Standard Windows controls, included with Windows.
  • 'Common controls', implemented in COMCTL32.DLL and included with Windows.
  • ActiveX controls.
  • User-defined controls.

We will discuss the first type here. The common controls are similar in use to the standard controls, but use a different method of communication with the application. I suggest you look through the help files and the sample programs to get a feel for these.

ActiveX controls are not really suitable for use with Windows API-based programs, since the amount of supporting code is quite large. I recommend you learn to use MFC after this tutorial if you wish to write large Windows programs.

User-defined controls are obtained by subclassing controls in the dialog to provide extra functionality over and above those of the standard controls. Such controls are outside of the scope of this tutorial. We will touch briefly on them when we talk about generic windows.

Standard Windows Controls

There are 7 main types of controls:
  • Buttons
  • Combo boxes
  • Edit controls
  • List boxes
  • Rich edit controls
  • Scroll bars
  • Static controls

You have already met buttons, in the previous tutorial. 'Static' controls are merely that - they send you no messages (in general); their purpose is to provide descriptions and pictures associated with the active items in the dialog box.

All these controls are well-described in MSDN, and so I will not reproduce this information here. I will, however, introduce you to some of the concepts of controls by presenting an application that uses edit boxes to implement a quadratic solving program.

For this application, you will need to modify the dialog box we used in the previous example. Add three edit boxes, with IDs ID_A, ID_B, and ID_C. You can add text as well, if you like. Your finished dialog will look like the following:

While hardly a model of user-interface design, it will serve our purposes.

Before we write the code, we should establish what our program will do. When the OK button is clicked, it will:

  • Read the values from the edit boxes as floating-point numbers.
  • Calculate the roots (possibly complex) of the quadratic equation.
  • Display the roots in a message box.
  • Return to the main dialog.

When Cancel is clicked, the dialog box will close.

With that in mind, let's write some code. First, add

#include <math.h>
#include <stdio.h>

to the top of your source file, so that we can access 'sqrt' and 'sprintf'. Next, we need some calculation code:

void GetQuadraticRoots(double a, double b, double c, 
	double *pReal1, double *pReal2, double *pImag1, double *pImag2)
{
	*pImag1 = *pImag2 = 0;
	if (a == 0)
	{
		if (b == 0)	// indeterminate - 'c = 0' so just set it to 0
			*pReal1 = *pReal2 = 0;
		else		// linear: bx + c = 0 (so x = -c/b)
			*pReal1 = *pReal2 = -c / b;
		return;
	}

	*pReal1 = *pReal2 = -b / (2 * a);
	if ((b * b) >= (4 * a * c))
	{
		// Real roots
		*pReal1 += sqrt(b*b - 4*a*c) / (2 * a);
		*pReal2 += sqrt(b*b - 4*a*c) / (2 * a);
	}
	else
	{
		// Complex roots
		*pImag1 = sqrt(4*a*c - b*b) / (2 * a);
		*pImag2 = -*pImag1;
	}
}

I trust everyone will understand the above code...

And now we need to perform some command handling. We use GetDlgItemText to get the numbers from the edit boxes, use strtod to convert these to doubles, and then sprintf and MessageBox to output the results. Note that we're using TCHAR, not char, _sntprintf instead of sprintf and _tcstod in place of strtod, to provide Unicode support.

BOOL MainDialog_OnCommand(HWND hWnd, WORD wCommand, WORD wNotify, HWND hControl)
{
	double	a, b, c, r1, r2, i1, i2;
	TCHAR	buffer[64], *pEnd;

	switch (wCommand)
	{
	case IDOK:
		GetDlgItemText(hWnd, IDC_A, buffer, sizeof(buffer) / sizeof(TCHAR));
		a = _tcstod(buffer, &pEnd);
		GetDlgItemText(hWnd, IDC_B, buffer, sizeof(buffer) / sizeof(TCHAR));
		b = _tcstod(buffer, &pEnd);
		GetDlgItemText(hWnd, IDC_C, buffer, sizeof(buffer) / sizeof(TCHAR));
		c = _tcstod(buffer, &pEnd);
		GetQuadraticRoots(a, b, c, &r1, &r2, &i1, &i2);
		_sntprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), 
			_T("Roots: %f%+fj, %f%+fj\n"), r1, i1, r2, i2);
		MessageBox(hWnd, buffer, _T("Quadratic Solver"), MB_OK);
		break;

	case IDCANCEL:
		EndDialog(hWnd, 1);
		break;
	}
	return TRUE;
}

In Unicode, characters are 16 bits long. So, we have to allow for this when doing sizeof() - sizeof(buffer) does not return the number of characters, it returns the number of bytes. This obviously makes a difference in Unicode.

There are lots of things you can do with controls over and above what I have shown you, and you can find out their capabilities by browsing the help files. For now, we have quite enough to continue.

 

Copyright © David McCabe, 1998 - 2001. All rights reserved.

You will need to download and install the m-math control to display any equations on this Web site. Without this control, you will not see most of the equations. Please do not e-mail me asking why the equations do not display!