You are on page 1of 13

2010

WINDOWS PROGRAMMING

ASSIGNMENT 3

SUBMITTED TO:

JASPREET MAM SUBMITTED BY :


ANJANI KUNWAR
RA1803A10
10807973
B.TECH(CSE)-H
PART A

1.Differentiate between DestroyMenu() and DeleteMenu().

ANS:

DestroyMenu Function

Destroys the specified menu and frees any memory that the menu occupies.

Syntax

BOOL WINAPI DestroyMenu(


__in HMENU hMenu
);

Parameters
hMenu

HMENU

A handle to the menu to be destroyed.

Return Value

DeleteMenu Function

Deletes an item from the specified menu. If the menu item opens a menu or
submenu, this function destroys the handle to the menu or submenu and frees the
memory used by the menu or submenu.

Syntax:

BOOL WINAPI DeleteMenu(


__in HMENU hMenu,
__in UINT uPosition,
__in UINT uFlags
);

Parameters
hMenu [in]

HMENU

A handle to the menu to be changed.

uPosition [in]

UINT

2.Differentiate between the use of WM_SYSCOMMAND and WM_COMMAND.

ANS:

WM_SYSCOMMAND

This message is sent to a window when the user chooses a command from the
window menu, formerly known as the system or control menu, or when the user
chooses the maximize button or the minimize button.

Syntax

WM_SYSCOMMAND uCmdType = wParam;


xPos = LOWORD(lParam);
yPos = HIWORD(lParam);

Parameters

uCmdType

Specifies the type of system command requested. It is one of the following values.

Value Description

SC_CLOSE Closes the window.


SC_KEYMENU Retrieves the window menu as a result of a keystroke.
xPos

Specifies the horizontal position of the cursor, in screen coordinates, if a


window menu command is chosen with the mouse. Otherwise, the xPos
parameter is not used.

yPos

Specifies the vertical position of the cursor, in screen coordinates, if a


window menu command is chosen with the mouse. This parameter is –1 if
the command is chosen using a system accelerator, or zero if using a
mnemonic.

WM_COMMAND

This message is sent when the user selects a command item from a menu, when a
control sends a message to its parent window, or when an accelerator keystroke is
translated.

Syntax

WM_COMMAND wNotifyCode = HIWORD(wParam);


wID = LOWORD(wParam);
hwndCtl = (HWND) lParam;

Parameters
wNotifyCode

Value of the high-order word of wParam. Specifies the notification code if


the message is from a control. If the message is from an accelerator, this
parameter is 1. If the message is from a menu, this parameter is 0.

wID

Value of the low-order word of wParam. Specifies the identifier of the menu
item, control, or accelerator.
hwndCtl

Handle to the control sending the message if the message is from a control.
Otherwise, this parameter is NULL.

3.List the steps necessary for creating child windows.

ANS:
 

A Window's Childhood
After creating the main window, you can use it as a parent for other windows. To
specify that a window is a child of another window, when creating it with either the
CreateWindow() or the CreateWindowEx() function, pass the handle of the parent as
the hWndParent argument. Here is an example:
// Create a window
CreateWindowEx(0, WndClassName, CaptionOrText,
ChildStyle, Left, Top, Width, Height,
hWndParent, NULL, hInstance, NULL);

If a window is a child of another window, to get a handle to its parent, you can call the
GetParent() function. Its syntax is:

HWND GetParent(HWND hWnd);

The hWnd argument is a handle to the child window whose parent you want to find out.
Alternatively, you can also use the GetWindowLong() function, passing the second
argument as GWL_HWNDPARENT, to get a handle to the parent of a window.

 
The Borders of a Window
To distinguish a particular window from the other objects on a screen, a window can be
defined by surrounding borders on the left, the top, the right, and the bottom. One of the
effects the user may want to control on a window is its size. For example, the user may
want to narrow, enlarge, shrink, or heighten a window. To do this, a user would
position the mouse on one of the borders, click and drag in the desired direction. This
action is referred to as resizing a window. For the user to be able to change the size of a
window, the window must have a special type of border referred to as a thick frame. To
provide this border, apply or add the WS_THICKFRAME style:
CreateWindow(ClsName, WndName,
WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX |
WS_THICKFRAME);

Because many windows will need this functionality, a special style can combine them
and it is called WS_OVERLAPPEDWINDOW. Therefore, you can create a resizable
window as follows:

CreateWindow(ClsName, WndName, WS_OVERLAPPEDWINDOW,

Window's Location and Size


The location of a window is defined by the distance from the left border of the monitor
to the window's left border and its distance from the top border of the monitor to its
own top border. The size of a window is its width and its height. These can be
illustrated for a main window frame as follows:
For a Win32 application, the original distance from the left border of the monitor is
passed as the x argument to the CreateWindow() or the CreateWindowEx() function.
The distance from top is specified using the y argument. The x and y arguments define
the location of the window. The distance from the left border of the monitor to the right
border of the window is specified as the nWidth argument. The distance from the top
border of the monitor to the lower border of the window is specified with the nHeight
value.

If you cannot make up your mind for these four values, you can use the
CW_USEDEFAULT (when-Creating-the-Window-USE-the-DEFAULT-value) constant
for either one or all four arguments. In such a case, the compiler would select a value
for the argument. Here is an example:

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,


LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClsEx;
...

RegisterClassEx(&WndClsEx);

HWND hWnd = CreateWindow(ClsName,


WndName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
return 0;
}
 
Displaying the Window
Once a window has been created and if this was done successfully, you can display it to
the user. This is done by calling the ShowWindow() function. Its syntax is:
BOOL ShowWindow(HWND hWnd, int nCmdShow);

The hWnd argument is a handle to the window that you want to display. It could be the
window returned by the CreateWindow() or the CreateWindowEx() function.

The nCmdShow specifies how the window must be displayed. Its possible values are:

Value Description
SW_SHOW Displays a window and makes it visible
SW_SHOWNORMAL Displays the window in its regular size. In
most circumstances, the operating system
keeps track of the last location and size a
window such as Internet Explorer or My
Computer had the last time it was
displaying. This value allows the OS to
restore it.
SW_SHOWMINIMIZED Opens the window in its minimized state,
representing it as a button on the taskbar
SW_SHOWMAXIMIZED Opens the window in its maximized state
SW_SHOWMINNOACTIVE Opens the window but displays only its
icon. It does not make it active
SW_SHOWNA As previous
SW_SHOWNOACTIVATE Retrieves the window's previous size and
location and displays it accordingly
SW_HIDE Used to hide a window
SW_MINIMIZE Shrinks the window and reduces it to a
button on the taskbar
SW_MAXIMIZE Maximizes the window to occupy the whole
screen area
SW_RESTORE If the window was minimized or maximized,
it would be restored to its previous location
and size

To show its presence on the screen, the window must be painted. This can be done by
calling the UpdateWindow() function. Its syntax is:

BOOL UpdateWindow(HWND hWnd);

This function simply wants to know what window needs to be painted. This window is
specified by its handle. Here is an example:

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,


LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClsEx;
...

RegisterClassEx(&WndClsEx);

HWND hWnd = CreateWindow(ClsName, WndName,


WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL);

if( !hWnd ) // If the window was not created,


return 0; // stop the application

ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
return 0;
PART:B
Q3.Suppose we click on upper left corner of the whole screen, it means we aren’t
restricting on the client area, we are clicking on non-client area’s upper left corner,
elaborate the action taken by windows. Also, elaborate the things necessary for
attaching that function in your window.

The system maintains an update region for the nonclient area. When an application
receives a WM_NCPAINT message, the wParam parameter contains a handle to a
region defining the dimensions of the update region. The application can use the
handle to combine the update region with the clipping region for the window
device context. The system does not automatically combine the update region
when retrieving the window device context unless the application uses GetDCEx
and specifies both the region handle and the DCX_INTERSECTRGN flag. If the
application does not combine the update region, only drawing operations that
would otherwise extend outside the window are clipped. The application is not
responsible for clearing the update region, regardless of whether it uses the region.

If an application processes the WM_NCACTIVATE message, after processing it


must return TRUE to direct the system to complete the change of active window. If
the window is minimized when the application receives the WM_NCACTIVATE
message, it should pass the message to DefWindowProc. In such cases, the default
function redraws the label for the icon.

2.Suppose two instances of same window application or different window


applications are opened, one is in active state and the other is in inactive state.
Now, if we shift from one instance into the other with the help of mouse,
elaborate the actions taken and the messages generated by the windows.

The nonclient-area mouse messages parallel almost exactly the client-area mouse
messages. The message identifiers include the letters "NC" to indicate "nonclient."
If the mouse is moved within a nonclient area of a window, the window procedure
receives the message WM_NCMOUSEMOVE. The mouse buttons generate these
messages:

The wParam and lParam parameters for nonclient-area mouse messages are
somewhat different from those for client-area mouse messages. The wParam
parameter indicates the nonclient area where the mouse was moved or clicked. It is
set to one of the identifiers beginning with HT (standing for "hit-test") that are
defined in the WINUSER.H.

The lParam parameter contains an x-coordinate in the low word and a y-coordinate
in the high word. However, these are screen coordinates, not client-area
coordinates as they are for client-area mouse messages. For screen coordinates, the
upper-left corner of the display area has x and y values of 0. Values of x increase as
you move to the right, and values of y increase as you move down the screen. (See
Figure 7-3.)

You can convert screen coordinates to client-area coordinates and vice versa with
these two Windows functions:

ScreenToClient (hwnd, &pt) ;


ClientToScreen (hwnd, &pt) ;

where pt is a POINT structure. These two functions convert the values stored in the
structure without preserving the old values. Note that if a screen-coordinate point is
above or to the left of the window's client area, the x or y value of the client-area
coordinate could be negative.

3.Suppose WM_SYSCOMMAND is generated, but not handled as per different


changes we had done in the event of its generation by changing default menu
items, then how this message must be handled and where the handling of this
message be transferred?

ANS:
A window receives this message when the user chooses a command from the
Window menu (formerly known as the system or control menu) or when the user
chooses the maximize button, minimize button, restore button, or close button.

This message can be handled without declaring its event at the generating time
are as follows

An application can carry out any system command at any time by passing a
WM_SYSCOMMAND message to DefWindowProc.Any WM_SYSCOMMAND
messages not handled by the application must be passed to DefWindowProc. Any
command values added by an application must be processed by the application and
cannot be passed to DefWindowProc.

The menu items in a window menu can be modified by using the GetSystemMenu,
AppendMenu, InsertMenu, ModifyMenu, InsertMenuItem, and SetMenuItemInfo
functions. Applications that modify the window menu must process
WM_SYSCOMMAND messages.
If password protection is enabled by policy, the screen saver is started regardless of
what an application does with the SC_SCREENSAVE notification—even if fails
to pass it to DefWindowProc.
Accelerator keys that are defined to choose items from the window menu are
translated into WM_SYSCOMMAND messages; all other accelerator keystrokes
are translated into WM_COMMAND messages.
If the wParam is SC_KEYMENU, lParam contains the character code of the key
that is used with the ALT key to display the popup menu. For example, pressing
ALT+F to display the File popup will cause a WM_SYSCOMMAND with
wParam equal to SC_KEYMENU and lParam equal to 'f'.

You might also like