Windows are the most fundamental element of a Microsoft Windows application. All applications have a main
“parent” window that can have a menu and icon associated with it. Windows are actually used in almost every aspect of an application’s user interface, ranging from the parent window to the individual buttons and edit controls on dialog boxes. Dialog boxes themselves are just another type of window.
There is only one function needed to create windows, the CreateWindow function, which is the most complex function in the Windows API. It is also one of the most important functions in the API. There are many types of predefined windows that can easily be created. These include buttons, static controls, list boxes, combo boxes, edit controls, and scroll bars. However, there are no predefined windows available for the first window that an application needs to create, the main window.
Creating the Main Window
An application requires a main window in order to process messages and user input. This window is also the parent for all top-level windows created in the application. WinMain is where the main window is created and the message processing loop resides. If it is the first instance of an application to be executed, then a
“window class” is first registered with the RegisterClass function. Once the new class is registered, the main window can be created. Listing 3-1 shows what a basic WinMain function should look like and how the class is registered on the first instance of an application. Figure 3-1 shows the main window that is created and points out the elements of a window.
Figure 3-1 Elements of a window Listing 3-1 WinMain function
static LPCTSTR lpszAppName = “MyApp”;
HINSTANCE hInst;
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
Go!
Keyword
---Go!
{
MSG msg;
WNDCLASSEX wc;
HWND hWnd;
// Fill in window class structure with parameters that describe the // main window.
//...
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, lpszAppName);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
wc.cbSize = sizeof(WNDCLASSEX);
// Create the main application window //...
ShowWindow( hWnd, nCmdShow );
// Process application messages until the application closes //...
while( GetMessage( &msg, NULL, 0, 0) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return( msg.wParam );
}
Messages are processed in the message loop in the WinMain function until the application ends. Messages are dispatched to the WndProc function that was registered when the window class was created. The application will process the message or pass it on to the DefWindowProc default processing.
Previous Table of Contents Next
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
Brief Full Advanced Search
Search Tips
To access the contents, click the chapter and section titles.
Windows NT Win32 API SuperBible (Publisher: Macmillan Computer Publishing) Author(s): Richard Simon
ISBN: 1571690891 Publication Date: 03/01/98
Search this book:
Previous Table of Contents Next
Creating Windows Using Existing Classes
As mentioned earlier, Microsoft Windows has predefined classes such as buttons, list boxes, combo boxes, and edit controls. There are also predefined classes for static text and scroll bars. Dialog boxes use window controls, created with these existing classes, for the majority of their user interface. An application does not have to create the individual controls on a dialog box. Instead, the dialog box manager (internal Windows code that creates and manages dialog boxes) creates the controls using a dialog template for the information needed in the CreateWindow function.
An application could create controls as well. They are created as child windows on a one-by-one basis using the CreateWindow function from within the body of the application. The existing classes that CreateWindow will work with are BUTTON, LISTBOX, COMBOBOX, STATIC, EDIT, MDICLIENT, and SCROLLBAR. Note that MDICLIENT is a special window class used only to create the client area of an MDI application.
Listing 3-2 shows how an application can create a static text, edit control, and button window on the client area of the parent window created in Listing 3-1. Figure 3-2 shows how the static text, edit control, and button appear on the main window’s client area once they are created.
Listing 3-2 Creating user controls using existing classes
#define IDC_TEXT 101
static HWND hText = NULL;
static HWND hEdit = NULL;
static HWND hBtn = NULL;
switch (message)
case IDM_TEST :
Figure 3-2 Main window with child windows
Messages When Creating Windows
When a window is created, Windows automatically sends messages to the application’s WndProc function. Most of these messages are passed to the DefWindowProc. An application’s WndProc by default should send messages that are not handled by the application to the DefWindowProc. This allows Windows to do most of the hard work and lets the application deal with only the messages it needs to.
When the CreateWindow function creates the main application window, five messages are sent to the application’s WndProc function. The WndProc function normally processes the WM_CREATE message while passing the other four messages to the DefWindowProc. The application uses the WM_CREATE message when initializing the window. The messages are listed in Table 3-1 in the order they are received.
Table 3-1 CreateWindow-generated messages
Message Meaning
WM_GETMINMAXINFO Gets the size and position of the window that is being created.
WM_NCCREATE The nonclient area of the window is about to be created. Memory for the window is allocated and scroll bars are initialized.
WM_NCCALCSIZE The size and position of the window’s client area are calculated.
WM_CREATE Notification that the window is about to be created. Perform initialization here.
WM_SHOWWINDOW The window is going to be displayed.
Creating Windows Function Descriptions
Table 3-2 summarizes the functions used in creating windows. The detailed descriptions follow.
Table 3-2 Creating windows function summary
Function Purpose
CreateWindow Creates a new window and returns a window handle.
CreateWindowEx Creates a new window with an extended style.
DestroyWindow Closes and removes a window.
RegisterClass Registers a new window class for creating windows.
RegisterClassEx Registers a new window class for creating windows that need a small icon.
UnregisterClass Removes a window class that was previously registered.
Previous Table of Contents Next
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
Brief Full Advanced Search
Search Tips
To access the contents, click the chapter and section titles.
Windows NT Win32 API SuperBible (Publisher: Macmillan Computer Publishing) Author(s): Richard Simon
ISBN: 1571690891 Publication Date: 03/01/98
Search this book:
Previous Table of Contents Next
CreateWindow... Windows 95 Windows NT
Description
CreateWindow is used to create a window based on one of the predefined classes or a class that was created with RegisterClass. The size, location, and style of the window are determined by the parameters passed to CreateWindow. ShowWindow is used to make the window visible after creation.
The CreateWindow function is used to create any type of window. CreateWindow is used in the WinMain function to create the main application window and within the application for creating child windows and user interface controls.
Syntax
HWND CreateWindow( LPCTSTR lpszClassName, LPCTSTR
lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hwndParent, HMENU hmenu, HANDLE hinst, LPVOID lpvParam )
Parameters
lpszClassName
LPCTSTR: Pointer to a null-terminated case-sensitive string containing a valid class name for the window or an integer atom. The class name can either be one created with
RegisterClass or one of the predefined classes in Table 3-3. Windows 95 introduced the new common controls (TreeView, ListView, etc.), which are also defined as window classes; however, they are not defined unless the application initializes the common control DLL. For more information on common controls, refer to Book 2, Common
Controls & Messages. If lpszClassName is an atom, then it must be created with a previous call to GlobalAddAtom.
Table 3-3 Predefined window classes
Class Meaning
BUTTON A rectangular pushbutton, group box, check box, radio button, or icon window. Buttons can be created with the style
BS_OWNERDRAW to give the application control over how the button looks in different states.
Go!
Keyword
---Go!
COMBOBOX Combination list box with edit field on top or a drop-down selection list box.
EDIT A rectangular edit control for user text entry. Can be single line or multiple lines.
LISTBOX A list box control. A control that has a list of strings that can be selected. The list box can allow multiple selections or limit the selections to one. List boxes can be created with the
LBS_OWNERDRAWFIXED or LBS_OWNERDRAW VARIABLE to give the application control over how the strings appear in the control.
MDICLIENT An MDI (multiple-document interface) client window. This window receives messages that control MDI child windows in an application. An MDI application must create an MDICLIENT window in order to work properly.
SCROLLBAR A scroll bar control.
STATIC A static text control. Static text is used to put text or frames on a window.
lpszWindowName
LPCTSTR: Points to a null-terminated string that contains the name of the window. The window name is displayed in the appropriate place for a particular window style.
dwStyle
DWORD: The style of the window that will be created. The style is made up of values that are combined together with a binary OR operator. An example of a style would be
WS_CHILD | ES_LEFT. Styles can be made of the values listed in Table 3-4 (following the example program).
x
int: The horizontal position of the upper-left corner of the window or control that will be created. Use CW_USEDEFAULT if the position is not important.
The x and y positions are relative to the upper-left corner of the screen or, for child windows and controls, the parent window. All sizes are measured in pixels, so a 640×480 resolution display would have 640 pixels horizontally and 480 pixels vertically.
y
int: The vertical position of the upper-left corner of the window or control. Use CW_USEDEFAULT if the position is not important.
nWidth
int: The horizontal width of the window or control. If the width is not important, use CW_USEDEFAULT.
nHeight
int: The vertical height of the window or control. Use CW_USEDEFAULT if the height is not important.
hwndParent
HWND: A handle to the window or control’s parent. Use a NULL value if there is no parent window. If no parent window is given, then the window will not be destroyed automatically when the application ends. Use DestroyWindow to remove the window before closing the application.
hmenu
HMENU: A handle to a window’s menu. Use a NULL value if the class menu should be used.
For controls, hmenu is set to an integer value that is the ID of the control being created. All WM_COMMAND messages reference this ID when an action has occurred with the control.
hinst
HANDLE: The instance handle of the application module creating the window or control.
lpvParam
LPVOID: A pointer to data that will be passed in as the LPARAM of the WM_CREATE message. When creating an MDICLIENT, use a pointer to a CLIENTCREATESTRUCT structure. For most windows and controls, set lpvParam to NULL.
Returns
HWND: Returns the window handle to the newly created window if the function was successful; NULL if the window could not be created.
Include File
This example shows a simple WinMain function and the steps that are needed to create the application’s main window.
static LPCTSTR lpszAppName = “MyApp”;
HWND hwndMain;
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow )
{
WNDCLASSEX wndclass;
MSG msg;
HWND hwndMain;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC)WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInst;
wndclass.hIcon = LoadIcon (hInst, lpszAppName);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
wndclass.lpszMenuName = lpszAppName;
wndclass.lpszClassName = lpszAppName;
wndclass.cbSize = sizeof( WNDCLASSEX );
wndclass.hIconSm = LoadImage( hInstance, lpszAppName, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR );
if ( RegisterClassEx( &wndclass ) == 0 ) return( FALSE );
// Create our main application window using our application
// class.
//...
hwndMain = CreateWindow( lpszAppName, lpszAppName,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL, NULL, hInst, NULL);
if (!hwndMain)
return( FALSE );
// Display the window we just created.
//...
ShowWindow( hwndMain, nCmdShow );
// Process messages until we exit the application.
//...
while( GetMessage(&msg, NULL, 0, 0) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
Previous Table of Contents Next
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
Brief Full Advanced Search
Search Tips
To access the contents, click the chapter and section titles.
Windows NT Win32 API SuperBible
(Publisher: Macmillan Computer Publishing) Author(s): Richard Simon
ISBN: 1571690891 Publication Date: 03/01/98
Search this book:
Previous Table of Contents Next
Table 3-4 Window and control styles
Window Styles Meaning
WS_BORDER Creates a window that has a thin-line border.
WS_CAPTION Creates a window that has a title bar.
Includes the WS_BORDER style.
WS_CHILD Creates a child window or control.
WS_POPUP cannot be used if this style is used.
WS_CHILDWINDOW Same as the WS_CHILD style.
WS_CLIPCHILDREN Clips around the child windows of a control when painting occurs. This style is used when creating parent windows.
WS_CLIPSIBLINGS Clips child windows relative to each other when painting occurs. If the style is not specified and a child window receives a WM_PAINT message, the entire area of the window will be included in the update region even if it is overlapped by a sibling window.
When the style is used, the area that a sibling window occupies is left out of the update region.
WS_DISABLED Creates a window that is initially disabled from receiving user input.
Go!
Keyword
---Go!
WS_DLGFRAME Creates a window that has a border of a style used with dialog boxes. Windows created with this style cannot have a title bar.
WS_GROUP Marks the first control in a group of controls. The next control that has the WS_GROUP style ends the group and starts a new group. Radio buttons are commonly grouped together so the arrow keys can be used to select a different option.
WS_HSCROLL Creates a window with a horizontal scroll bar.
WS_ICONIC Same as WS_MINIMIZE.
WS_MAXIMIZE Creates a window that is initially maximized.
WS_MAXIMIZEBOX Creates a window with a maximize button.
WS_MINIMIZE Creates a window that is initially minimized.
WS_MINIMIZEBOX Creates a window with a Minimize button.
WS_OVERLAPPED Creates a window with a title bar and a border.
WS_OVERLAPPEDWINDOW Combination of the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU,
WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles.
WS_POPUP Creates a pop-up window. WS_CHILD cannot be used with this style.
WS_POPUPWINDOW Combination of the WS_BORDER, WS_POPUP, and WS_SYSMENU styles.
WS_CAPTION must be specified to make the System menu visible.
WS_SIZEBOX Same as WS_THICKFRAME.
WS_SYSMENU Creates a window that has a System menu box in the title bar.
WS_TABSTOP Specifies a control that the key will stop on.
WS_THICKFRAME Creates a window with a sizing border.
WS_TILED Same as WS_OVERLAPPED.
WS_TILED_WINDOW Same as WS_OVERLAPPEDWINDOW.
WS_VISIBLE Creates a window that is initially visible.
WS_VSCROLL Creates a window with a vertical scroll bar.
Button Styles Meaning
BS_3STATE Creates a three–selection-state check box. The check box can be unselected, selected, or grayed. The grayed state is normally used to show that the check box value is not determined.
BS_AUTO3STATE Same as BS_3STATE except the check box will change its state when the user selects it.
BS_AUTOCHECKBOX Same as BS_CHECKBOX except the check box will change its state when the user selects it.
BS_AUTORADIOBUTTON Same as BS_RADIOBUTTON except the radio button is selected when the user clicks it, and any other radio buttons in the group are unselected.
BS_BITMAP Creates a button that will display a bitmap.
BS_BOTTOM Places the button title at the bottom of the button rectangle.
BS_CENTER Centers the button title horizontally in the button rectangle.
BS_CHECKBOX Creates a check box with the title displayed on the right side unless the BS_LEFTTEXT style is used.
BS_DEFPUSHBUTTON Creates a pushbutton that is pressed if the user presses the ENTER key. The button will have a heavy, black border.
BS_GROUPBOX Creates a box with the title displayed in the upper-left corner.
BS_ICON Creates a button that will display an icon.
BS_LEFT Left-justifies the title in the button rectangle. If the button is a check box or radio button and does not have the BS_RIGHTBUTTON style, the text is left-justified on the right side of the check box or radio button.
BS_LEFTTEXT Places text on the left side of a radio button or check box.
BS_MONO Specifies that the button has only one line of text for the title.
BS_MULTILINE Specifies that the button has multiple lines of text for the title. The title will be wrapped to a second line if it is too long to fit on a single line.
BS_NOTIFY Sends notification messages to the parent window. This is in addition to the BN_CLICKED and BN_DBLCLK that are sent by default.
BS_OWNERDRAW Creates an owner-drawn button. The parent window receives a
WM_MEASUREITEM message when the button is created and a WM_DRAWITEM message any time the button needs to be painted. This style should not be
combined with any other button styles.
BS_PUSHBUTTON Creates a pushbutton that posts a WM_COMMAND message to the parent window when the button is selected.
BS_PUSHLIKE Makes a check box or radio button have a pushbutton look and action.
BS_RIGHTBUTTON Places the radio button or check box selection area on the right side of the button rectangle.
BS_TEXT Specifies that the button displays text.
BS_TOP Positions the title at the top of the button rectangle.
BS_VCENTER Vertically centers the title in the button rectangle.
Combo Box Styles Meaning
CBS_AUTOHSCROLL Allows horizontal scrolling in the edit control of the combo box.
CBS_DISABLENOSCROLL Forces a vertical scroll bar to be visible even when all items in the list are visible. The scroll bar will be disabled unless it is needed to show all the items in the list. Normally, the scroll bar is only visible when needed.
CBS_DROPDOWN Creates a drop-down combo box. The list is only visible while selecting.
CBS_DROPDOWNLIST Creates a drop-list combo box. Editing is not allowed; the selected item is display only.
CBS_HASSTRINGS Used with an owner-drawn combo box
CBS_HASSTRINGS Used with an owner-drawn combo box