csammisrun

A rare situation

Chapter 5

without comments

All about controls

In chapter four, we made ourselves a very nice window that intercepted one event (the window closing). Let’s be honest with ourselves. So far, JotStuffDownPad is useless. But no longer, because in this chapter we’ll be learning how to put buttons, text fields, and other interesting things into a window. We’ll also talk about what we’re going to do in terms of our project. No code is going to be presented here, only concepts, but it’s worth the read to familiarize yourself with controls.

Concerning controls

An object placed on a window, such as a button or a text field, is commonly called a control. Other GUI building systems, such as the UNIX-born Qt, call them widgets. Personally I prefer “control,” so that’s what we’re going to use in this tutorial. The interesting thing about controls in Windows is that you already know how to create them! “But Chris,” you say, “you haven’t shown us a function called CreateControl or MakeButtonGoNow! In fact, I cheated and skipped to the end, and there’s no function like that *anywhere*!” Well my friend, that’s because of the way Windows handles control creation. Controls, from a button to a listbox to a Rich Text edit field, are actually windows, and are created in the same manner as standard windows: by using . This is actually a very nice way of doing things, because since controls are windows we can use all the same API functions for both windows and controls ( can show or hide a window or a single button in a window, for example).

The primary difference between “standard” windows and controls is that of hierarchy. The windows that represent controls are known as child windows, and the windows that contain controls are parent windows or top-level windows. When calling in chapter four, we specified for the parameter, and for the parameter. This is fine for a top-level window with no parent, but to create a child window we must specify for the style (this can, and will, be ORed with other values to specify more than one style) and the window handle of the control’s parent window instead of for .

(note: It’s easy to get confused, since you technically can refer to controls as windows. For the remainder of this tutorial, buttons and listboxes and whatever will be called “controls” and a standard window will be called a “window.” Nice and simple, eh?)

In addition to changing the and parameters, we also have to change the window class name. Instead of creating, registering, and then using a window class as we did for our initial window, we use a predefined system (or control) class as the argument to . There are nine built-in control classes to pick and choose from:

“Button”
Designates a button. This can either be a pushbutton, toggle button, radio button, or checkbox

“ComboBox”
Designates a combo box or drop-down list box

“Edit”
Designates an edit control (a text field)

“ListBox”
Designates a list of strings

“MDIClient”
Designates an MDI (Multiple Document Interface) client window **

“RichEdit”
Designates a Rich Edit Control, version 1.0 *

RICHEDIT_CLASS
Designates a Rich Edit Control, version 2.0 *

“ScrollBar”
Designates a scrollbar. These are scrollbars different than those for scrolling text fields up and down; the scrollbars in text fields are maintained automatically by the system *

“Static”
Designates a static text field (a “label” in Visual Basic parlance), a box, or a rectangle used to graphically divide other controls (commonly known as a “frame”). Static controls accept no input and send no output; they are decoration


* - This is here for completeness, we will probably not use it in the course of this tutorial
** - This is here for completeness, we will not use it in the course of this tutorial

You may notice that several control classes, the static control in particular, can represent different kinds of controls using only one control class name. How is this accomplished? Well, above I mentioned that values can be ORed together (using the operator) with the window styles used for controls to specify more than one style. This is how different controls are created from one class; for example, when the “Static” control class is used with and for the style values, a frame is created. When only is specified, a text label is created. I’ll present new style values as we run across them; for a complete list, there are links to control style tables from the MSDN entry.

Note: Style values are usually simply called styles. They are also called style bits, for reasons that are only interesting if you know binary (hint to those who do: style bits are ORed together to create a unique value).

Whoo! That’s a lot of hot air about controls…important hot air yes, but it gets us no futher in our project. Let’s delve deeper!

Designing JotStuffDownPad

As any good developer knows, it’s a good idea to have a picture in one’s head *before* one begins programming. To that end, I took pen to paper, then mouse cursor to drawing program, and came up with the following layout:

Sketch of JotStuffDownPad

Obviously, this isn’t what the real Notepad looks like, and there’s a very good reason for that: Notepad has exactly one major control (the large text field). It’s a poor example for a tutorial that should cover control creation, so I modified it thusly:

  • JotStuffDownPad will be more like a pad of Post-It notes, with the ability to flip through to different notes. This will be represented internally as a sizable array of s. I’ll be giving you the code that handles the addition, removal, and editing of notes as a freebie.
  • The “New” button will add a new note, initially empty, into the array and clear the text field
  • The “Delete” button will delete the currently displayed note, removing the entry from the array and clearing the text field
  • The “Exit” button will perform the same action as clicking on the “X” on the titlebar
  • The “Save” button will commit the current contents of the text field into the array, and add the first couple words into the combo box as the note’s title
  • The combo box will be used to “flip” between notes. Selecting a note’s title in the combo box will cause that note to be displayed

For right now, we’re not going to concern ourselves with the mechanics of saving the notes to disk when the program exits. So, now that we know what controls are and have a good idea of what we want to do, let’s get codin’!

Back to contents

Written by Chris

May 29th, 2007 at 6:34 pm

Posted in General

Leave a Reply