Window Management


Window management functions cover the creation, initialization, management, and eventual destruction of dialog boxes and other window types.

Creating Windows

The code listings in this section show some X Windows and Win32 implementations of window management. It is not likely that any large-scale X Windows client or Win32-based application would actually be implemented as these short code examples are. However, it is easy to see the conceptual similarities and some differences as well.

An X Windows X11 client might use XtAppInitialize() , XtVaAppInitialize() , XtOpenApplication() , or XtVaOpenApplication to get a top-level widget to create a window, as shown in the following code:

 main (int argc, char *argv[]) {   Widget toplevel;     /* Conceptual Application Window */   XtAppContext  app;   /* context of the app */   toplevel = XtVaAppInitialize(&app,                                  "myClassName",                                 NULL,0,&argc,argv,NULL,NULL);             OR   toplevel = XtOpenApplication(&app,                                 "myClassName",                                 NULL,0,&argc,argv,NULL,                                 whateverWidgetClass, NULL,0);     

In the following code, a Win32-based graphical application creates a main window:

 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {    HWND hWnd;  // handle to the Application Window    hWnd = CreateWindow("myClassName",                          "myWindowsName",                           WS_OVERLAPPEDWINDOW,                          CW_USEDEFAULT,                           0,                           CW_USEDEFAULT,                           0,                           NULL,                           NULL,                           hInstance,   // context of the app                          NULL); 

An X Windows client can create a control or widget as follows :

 main (int argc, char *argv[]) {   Widget toplevel;     /* Conceptual Application Window */   Widget button;          XtAppContext  app;   /* context of the app */   toplevel = XtVaAppInitialize(&app, "Example", NULL,0,&argc,argv,NULL,NULL);   button   = XtVaCreateManagedWidget("command",                                        commandWidgetClass,  /* class  */                                        toplevel,           /* parent */                                        XtNheight, 50,                                        XtNwidth, 100,                                        XtNlabel, "Press To Exit",                                        NULL); 

A Win32-based application can create a control or widget as follows:

 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {    HWND hWnd;  // handle to the Application Window    HWND hButton;    hWnd = CreateWindow("myClassName",                          "Example",                           WS_OVERLAPPEDWINDOW,                          CW_USEDEFAULT,                           0,                           CW_USEDEFAULT,                           0,                           NULL,                           NULL,                           hInstance,                           NULL);    hButton = CreateWindow("BUTTON",        // class                             "Press To Exit",                             WS_CHILD  BS_PUSHBUTTON,                             Xcoordinate,                             Ycoordinate,                             Width,                             Height,                             hWnd,           // parent                             (HMENU)idNumberOfThisControl,                             hInstance,                             NULL); 

Creating Controls

Controls ” such as X Windows widgets ” come in all shapes , sizes, colors, and functions. There are two ways to create controls in a Win32 environment. The first and simplest method is by using the resource editor in Visual Studio or one of the many dialog box editors. Use these tools to drag and drop controls onto a window or dialog box, which in X Windows is a widget itself. In Win32, controls are also windows in every respect.

Using a dialog editor produces a resource file with the extension .rc, for example MyProgram.rc.

The following code is a small part of a resource file, and shows the definition of a dialog box with several controls:

 //////////////////////////////////////////////////////////////////// // // Dialog // EXAMPLE_DIALOG DIALOG DISCARDABLE  0, 0, 267, 161 STYLE DS_MODALFRAME  WS_POPUP  WS_CAPTION  WS_SYSMENU CAPTION "  Mission Setup " FONT 8, "MS Sans Serif" BEGIN     PUSHBUTTON      "&Quit",IDC_EXIT,210,135,50,15     GROUPBOX        "  Select Mission ",IDC_STATIC,5,5,100,55     CONTROL         "Destroy Planet",IDC_DESTROY_PLANET,"Button",                     BS_AUTORADIOBUTTON  WS_GROUP,10,20,62,10     CONTROL         "Destroy Star",IDC_DESTROY_STAR,"Button",                     BS_AUTORADIOBUTTON,10,35,55,10     EDITTEXT        IDC_AUTHORIZATION_CODE,190,85,60,14,ES_AUTOHSCROLL     LTEXT           "Authorization Code :",IDC_TARGET_TEXT,120,85,65,13     PUSHBUTTON      "&Attack",IDC_ATTACK,155,135,50,14     LISTBOX         IDC_TARGET_LIST,5,90,100,60,LBS_NOINTEGRALHEIGHT                       WS_VSCROLL  WS_TABSTOP     LTEXT           "Select Target",IDC_STATIC,5,75,100,15     GROUPBOX        " Selected Mission Parameters ",IDC_STATIC,115,10,145,                     110     LTEXT           "Mission :",IDC_STATIC,120,35,60,8     LTEXT           "Target :",IDC_STATIC,120,60,60,8     EDITTEXT        IDC_MISSION_VALUE,190,35,60,14,ES_AUTOHSCROLL                       ES_READONLY     EDITTEXT        IDC_TARGET_VALUE,190,60,60,14,ES_AUTOHSCROLL                       ES_READONLY END 

The Rc.exe utility compiles the resource file that contains this definition, and the results are linked together with the executable file.

The following code creates the dialog box and all the controls that it contains. Notice that the second parameter is the name of the dialog box as it appears in the first line of the resource text.

 /*    ** Create modeless dialog box.    */   hExampleDlg = CreateDialog(hInstance,                               MAKEINTRESOURCE(EXAMPLE_DIALOG),                               (HWND)NULL,                               (DLGPROC)ExampleDlgProc); 

In the Microsoft Platform SDK, the Rc.exe utility and the Help file for it are found in the Microsoft Platform SDK\Bin directory. The Help file Rc.hlp describes all the necessary parts of the resource file, which is officially referred to as the resource definition script.

In Visual Studio, the Rc.exe and Rc.hlp files are found in the Microsoft Visual Studio\Common\MSDev98\Bin directory.

The second method is to call CreateWindow() with the necessary parameters to produce the desired control at the desired location inside a parent window.

An X Windows example is as follows:

 thisButton =      XtVaCreateManagedWidget("Fire Phasers",  button text  commandWidgetClass,  the type of widget  parentWidget,  parent widget  NULL); 

A Win32 example using GDI is as follows:

 HWND handleToThisButton; handleToThisButton =    CreateWindow("BUTTON",  the type of control  "Fire Phasers",  button text  WS_CHILD  WS_VISIBLE  BS_PUSHBUTTON,  the button style  XpositionInParent,                  yPositionInnParent,                  BUTTONWIDTH,                  BUTTONHEIGHT,                  handleOfParentWindow,  parent window  (HMENU)NUMBER_USED_TO_ID_THIS_CONTROL,                 hInst,                  NULL); 

Identifying a Control

To communicate with or respond to a control, it is necessary to identify it. This is done using the window handle and a unique ID associated with the control. The handle to a control can be used just as a handle to any window. For example, calling SetWindowPos() with the control s window handle can move the control or make it larger. Use the ID in the WindProc() switch statement to send or capture messages to and from the control.

The following API calls use the ID of the control along with the handle of the parent window:

  • SetDlgItemText()

  • GetDlgItemText()

  • GetDlgItemInt()

  • SetDlgItemInt()

If the developer uses CreateWindow() to build the control, both identification pieces are known. CreateWindow() returns the handle to the control, and the ninth parameter in the call to CreateWindow() is the unique ID the application associates with that control.

If the developer uses a resource editor to put the control in a containing window, the editor assigns a resource ID to the control or the developer can enter it manually. In either case, the Resource.h file that corresponds to the .rc file contains the ID number, as follows:

  • GetDlgCtrlID() returns the ID if it is passed the handle to the control.

  • GetDlgItem() returns the handle if it is passed the ID of the control.

Communicating with a Control

After the application identifies a control, it can communicate with it. The following are some examples of sending and receiving commands or messages from controls:

 //+ //  programmatically add strings to the list box //- // Win32 // SendMessage(handleToThisListBox, LB_ADDSTRING, 0, TEXT("String One")); SendMessage(handleToThisListBox, LB_ADDSTRING, 0, TEXT("String Two")); SendMessage(handleToThisListBox, LB_ADDSTRING, 0, TEXT("String Three")); // X11/Motif // XmString newString;              newString = XmStringCreateLocalized("String One");       XmListAddItem(listWidget, newString, 0);       XmStringFree(newString);       newString = XmStringCreateLocalized("String Two");       XmListAddItem(listWidget, newString, 0);       XmStringFree(newString);       newString = XmStringCreateLocalized("String Three");       XmListAddItem(listWidget, newString, 0);       XmStringFree(newString); //+ //  programmatically force the current focus  //  to this control, i.e., make the user select  //  something now! //- // Win32 // SetFocus(handleToThisListBox); // Xlib // XSetInputFocus(display, listWidget, RevertToParent, timeNow); //+ //  programmatically pick a string for them! //- // Win32 // SendMessage(handleToThisListBox, LB_SETCURSEL, 2, 0); // X11/Moif // XmListSelectPos(listWidget, 2, 0); //+ //  Controls can be managed in the WndProc() as follows //   //  Using the button created in the example above //- LRESULT CALLBACK WndProc(HWND hWnd,                            UINT message,                            WPARAM wParam,                            LPARAM lParam) {   switch (message) {      case WM_COMMAND:     switch (wParam) {          //+     //  this message is received because      //  the button was clicked by      //  the user     //-     case NUMBER_USED_TO_ID_THIS_CONTROL :       doSomethingWhenButtonIsPressed();       break; 



UNIX Application Migration Guide
Unix Application Migration Guide (Patterns & Practices)
ISBN: 0735618380
EAN: 2147483647
Year: 2003
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net