





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture handout is for Windows Programming course. It was provided by Prof. Jaimini Chinmay at Ambedkar University, Delhi. It includes: Order, Windows, Review, Child, Client, Area, Client, Parent, Int, Function, Height, Popup, Overlapped
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






CreateWindow function have been discussing in our previous lectures. Much of its details including styles, class name, parent handles, instance handle and coordinates, etc have been discussed in chapter 11. CreateWindow Function is used to create window. CreateWindow function can create parent, child, popup and overlapped windows with dimensions x, y, width and height.
HWND CreateWindow ( LPCTSTR lpClassName, // registered class name LPCTSTR lpWindowName, // window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam // window-creation data );
Following are the characteristics of child windows.
LRESULT CALLBACK WindowProc ( HWND hwnd, // handle to window UINT uMsg, // WM_COMMAND WPARAM wParam, // notification code and identifier LPARAM lParam // handle to control (HWND) );
Common controls are normally taken as child windows that send notification messages to the parent window when events, such as input from the user, occur in the control. The application relies on these notification messages to determine what action the user wants it to take. Except for trackbars, which use the WM_HSCROLL and WM_VSCROLL messages to notify their parent of changes, common controls send notification messages as WM_NOTIFY messages.
For demonstration purpose we are going to create an example application.
Our application will be parent-child window application. This application will consist of three push buttons of names:
The Window classes used in this application are:
wc.lpfnWndProc = mainWindowProc; wc.hInstance = hAppInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_UPARROW); wc.hbrBackground= (HBRUSH)GetStockObject (GRAY_BRUSH); wc.lpszClassName= "MainWindowClass";
if(!RegisterClass(&wc)) { return 0; }
wc .lpfnWndProc = popupWindowProc; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.hCursor = LoadCursor(NULL, IDC_HELP); wc.lpszClassName = "PopupWindowClass";
if(!RegisterClass(&wc)) { return 0; }
System window classes are pre-registered. They do not need to register in our application. In this application, we will only used to create them not to register them.
Create a Main Window of the Application.
hWndMain = CreateWindow("MainWindowClass", "Virtual University",
NULL, NULL, hInstance, NULL );
// check the returned handle, don’t need to proceed if the returned handle is NULL if(!hWnd) { MessageBox(NULL,”Cannot Create Main Window”,”Error”, MB_ICONHAND|MB_OK); return 0; }
Now create a popup window with caption and visible style bit on.
hWndPopup = CreateWindow("PopupWindowClass", //window name (optional) "Popup Window", //class name WS_POPUP | WS_CAPTION | WS_VISIBLE, 250, 250, 300, 250, hWndMain, NULL, hInstance, NULL );
Create a button window bearing a text “Rectangle”.
hWndButton=CreateWindow("BUTTON", "Rectangle", WS_CHILD | WS_VISIBLE, 10, 10, 100, 50, hWndMain, 5, hInstance, NULL );
Create an Edit Window bearing a text “Message”
CreateWindow("EDIT", "Message", WS_CHILD | WS_VISIBLE n | ES_LOWERCASE, 10, 190, 200, 25, hWndMain, 8, hInstance, NULL );
here we check the button if button rectangle is pressed then draw a rectangle with Light Gray stock brush.
case WM_DRAW_FIGURE:
hDC = GetDC(hWndPopup); switch(wParam) { case RECTANGLE: SelectObject(hDC,GetStockObject( LTGRAY_BRUSH)); Rectangle(hDC, 50, 10, 230, 150); break; }
In case of Circle, we create hatch brush, we created a hatch brush which has a style of diagonal cross lines. After creating a hatch brush we select it on device context to draw a figure. After drawing, brush must be deleted.
case CIRCLE:
hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(170, 150, 180)); SelectObject(hDC, hBrush); Ellipse(hDC, 70, 10, 210, 150); DeleteObject(hBrush); break;
By pressing the button a text must be display with the stock object Ansi variable fonts and background brush. Text are displayed using TextOut GDI function.
case TEXT_MESSAGE: { TextOut(hDC, 50, 100, "Virtual University", 18); SelectObject(hDC, GetStockObject( ANSI_VAR_FONT)); SetBkColor(hDC, RGB(10, 255, 20)); TextOut(hDC, 50, 115, "knowledge Beyond Boundaries", 27); break; }
ReleaseDC(hWndPopup, hDC);
case WM_DRAW_FIGURE:
hDC = GetDC(hWndPopup); hwndEdit = GetDlgItem(GetParent(hWnd), 8);
switch(wParam) { case RECTANGLE: SelectObject(hDC,GetStockObject(LTGRAY_BRUSH)); Rectangle(hDC, 50, 10, 230, 150); SendMessage(hwndEdit, WM_SETTEXT, 0, "Rectangle DrAwN!"); break; }
Main window is destroyed through button’s notification message BN_CLICKED. Main Window can be destroyed using DestroyWindow Function.
WM_CREATE: CreateWindow("BUTTON", "Quit Application", WS_CHILD | WS_VISIBLE, n 75, 155, 150, 40, hWnd, 1234, hAppInstance, NULL); break;
case WM_COMMAND: wControlID = LOWORD(wParam); wNotificationCode = HIWORD(wParam); if(wNotificationCode == BN_CLICKED) { switch(wControlID) { case 1234: DestroyWindow(GetParent(hWnd)); break; }
}