Home  Contents

Controls I

Controls are basic building blocks of a windows application. Controls are called widgets in UNIX environment.

Static control

The static control displays text and graphics. The static control cannot be selected. It cannot have keyboard focus.

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
			LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Static Control" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0,IDC_ARROW);

  
  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Static control"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                100, 100, 330, 270, 0, 0, hInstance, 0);  

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  static char *lyrics =  TEXT("Fuck what I said it dont mean shit now\n\
               Fuck the presents might as well throw em out\n\
               Fuck all those kisses, they didn't mean jack\n\
               Fuck you, you hoe, I dont want you back\n\
               \n\
               You thought, you could\n\
               Keep this shit from me, yeah\n\
               Ya burnt bitch, I heard the story\n\
               Ya played me, ya even gave him head\n\
               Now ya askin for me back\n\
               Ya just another act, look elsewhere\n\
               Cuz ya done with me\n\
             ");
    
  switch(msg)  
  {
	case WM_CREATE:
	{
	    CreateWindow(TEXT("STATIC"), lyrics, 
		    WS_CHILD | WS_VISIBLE | SS_LEFT,
		    20, 20, 300, 230,
		    hwnd, (HMENU) 1, NULL, NULL);
	    return 0;
	}

    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
 CreateWindow(TEXT("STATIC"), lyrics, 
        WS_CHILD | WS_VISIBLE | SS_LEFT,
        20, 20, 300, 230,
        hwnd, (HMENU) 1, NULL, NULL);

Here we create the static control. We display text. It is aligned to the left.


Static control

Button

A button is a simple control. It has a text label. It is used to trigger an action.

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
					LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Buttons" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0, IDC_ARROW);

  
  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Buttons"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                150, 150, 230, 150, 0, 0, hInstance, 0);  

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{

    
  switch(msg)  
  {
	case WM_CREATE:
	{
           CreateWindow(TEXT("button"), TEXT("Beep"),    
		             WS_VISIBLE | WS_CHILD ,
		             20, 50, 80, 25,        
		             hwnd, (HMENU) 1, NULL, NULL);    

	    CreateWindow(TEXT("button"), TEXT("Quit"),    
		             WS_VISIBLE | WS_CHILD ,
		             120, 50, 80, 25,        
		             hwnd, (HMENU) 2, NULL, NULL);    
	    break;
	}

      case WM_COMMAND:
      {
	   if (LOWORD(wParam) == 1) {
	       Beep(40, 50);
	   }

	   if (LOWORD(wParam) == 2) {
              PostQuitMessage(0);
	   }

	   break;
       }

      case WM_DESTROY:
      {
         PostQuitMessage(0);
         break;
      }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}	

In our example we have created two buttons. On button will beep. The other one will close the window.

 case WM_COMMAND:
 {
     if (LOWORD(wParam) == 1) {
      Beep(40, 50);
     }
     if (LOWORD(wParam) == 2) {
      PostQuitMessage(0);
     }
     break;
 }

The control id is in the LOWORD of the wParam. Depending on the control id, we call the Beep() function or the PostQuitMessage() function.


Button controls

Check box

A check box control is a box that you can click to turn an option on or off.

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static char *title = TEXT("Check Box");

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
					LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Check Box" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0, IDC_ARROW);

  
  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, title,
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                150, 150, 230, 150, 0, 0, hInstance, 0);  

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    
  switch(msg)  
  {
      case WM_CREATE:
      {
	    CreateWindow(TEXT("button"), TEXT("Show Title"),
                     WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
                     20, 20, 185, 35,        
                     hwnd, (HMENU) 1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
	    CheckDlgButton(hwnd, 1, BST_CHECKED);
	    break;
      }

      case WM_COMMAND:
      {
           BOOL checked = IsDlgButtonChecked(hwnd, 1);
	    if (checked) {
	        CheckDlgButton(hwnd, 1, BST_UNCHECKED);
		 SetWindowText(hwnd, TEXT(""));
	    } else {
		 CheckDlgButton(hwnd, 1, BST_CHECKED);
		 SetWindowText(hwnd, title);
	    }
	    break;
       }

      case WM_DESTROY:
      {
           PostQuitMessage(0);
           break;
      }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}	

In our example, we show or hide the window title depending on the state of the check box.

On windows, check box is a special kind of a button. We create a check box, if we set a specific style to the button class. In our example, it is BS_CHECKBOX.

 BOOL checked = IsDlgButtonChecked(hwnd, 1);

We determine the state of the check box using the IsDlgButtonChecked() function.

 CheckDlgButton(hwnd, 1, BST_UNCHECKED);

We check and uncheck the check box using the CheckDlgButton() function.

 SetWindowText(hwnd, TEXT(""));

The SetWindowText() function sets the title of the window.


Checkbox control

Edit Control

Edit control is a rectangular child window. Edit control is used to enter and edit text. It can be single line or multiline.

#include <windows.h>

#define ID_EDIT 1
#define ID_BUTTON 2

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

  static HWND hwndEdit;
  static HWND hwndButton;
  static int len;
  static TCHAR text[30];


  switch(msg)
  {
    case WM_CREATE:
	hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
				50, 50, 150, 20, hwnd, (HMENU) ID_EDIT,
				NULL, NULL);

	hwndButton = CreateWindow(
		TEXT("button"), TEXT("Set Title"),       
		WS_VISIBLE | WS_CHILD,  
		50, 100, 80, 25,        
		hwnd, (HMENU) ID_BUTTON, NULL, NULL);      

	break;

	case WM_COMMAND:	
           if (HIWORD(wParam) == BN_CLICKED) {
               len = GetWindowTextLength(hwndEdit) + 1;
               GetWindowText(hwndEdit, text, len);
               SetWindowText(hwnd, text);
           }
	break;

	case WM_DESTROY:
		PostQuitMessage(0);
	break;
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
			LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Edit Control" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0,IDC_ARROW);

  
  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Edit control"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                220, 220, 280, 200, 0, 0, hInstance, 0);  

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

In our example, we have an edit control and a button. We can put some text into the edit control. If we click on the button, the entered text will be displayed in the titlebar of the main window.

  if (HIWORD(wParam) == BN_CLICKED) {
      len = GetWindowTextLength(hwndEdit) + 1;
      GetWindowText(hwndEdit, text, len);
      SetWindowText(hwnd, text);
  }	

The GetWindowTextLength() returns the text length entered. Notice, that we add 1 to the length. This is to include the zero terminator. Try to omit it and see, what happens. The GetWindowText() receives the text from the edit control. We use hwndEdit as an id. The SetWindowText() sets the text for the window. In this context, it is a title of the main window.


Figure: Edit control