Category : Files from Magazines
Archive   : CGAZV5N1.ZIP
Filename : MSGDEMO.C

 
Output of file : MSGDEMO.C contained in archive : CGAZV5N1.ZIP

/****************************** MSGDEMO.C ********************************
* File: msgdemo.c
* Author: Michael J. Young
* Purpose: Basic Windows program: echo messages, paints screen
* Compiler: Microsoft C 6.0
* Compile time switches: none
* Associated files: msgdemo.h -> header file, provides links to resources
* msgdemo.m -> make file
* msgdemo.def -> linker definition file
* msgdemo.rc -> resource file: defines menu items
*
* Compile with:
* cl -c -W2 -AS -Gsw -Oas -Zpe msgdemo.c
* rc -r msgdemo.rc
* link /NOD msgdemo.obj,, nul.map, libw.lib slibcew.lib, msgdemo.def
* rc msgdemo.res
* (c) 1990 C Gazette. Use freely as long as authorship is acknowledged
***************************************************************************/

#include "windows.h"
#include "msgdemo.h"

BOOL InitInstance (HANDLE hInstance, int nCmdShow);
BOOL InitProgram (HANDLE hInstance);
long FAR PASCAL MainWndProc (HWND hwnd, unsigned message, WORD wParam,
LONG lParam);

HANDLE HInst; /* Handle to current program instance. */
char MsgLine1 [30]; /* Buffers for strings displayed in window. */
char MsgLine2 [80];

int PASCAL WinMain /* Program entry function. */
(HANDLE hInstance, /* Handle to current program instance. */
HANDLE hPrevInstance, /* Handle to previous program instance. */
LPSTR lpCmdLine, /* Pointer to command line. */
int nCmdShow) /* Indicates how to show window (open/icon). */
{
MSG Msg; /* Structure for holding messages. */

if (!hPrevInstance) /* Check for other program instances. */
if (!InitProgram (hInstance)) /* Initialize program. */
return (FALSE); /* Can't initialize; exit. */

if (!InitInstance (hInstance, nCmdShow)) /* Initialize instance. */
return (FALSE); /* Can't initialize; exit. */

while (GetMessage /* Get messages from the program queue */
/* until receiving WM_QUIT. */
(&Msg, /* Address of message structure. */
NULL, /* Window to receive messages: NULL=all */
NULL, /* Lowest message value: none. */
NULL)) /* Highest message value: none. */
{
TranslateMessage (&Msg); /* Convert virtual key messages into */
/* character messages (WM_CHAR). */
DispatchMessage (&Msg); /* Sends message to window procedure. */
}

return (Msg.wParam); /* Return value supplied by WM_QUIT. */

} /* end WinMain */


BOOL InitInstance /* Instance initialization function. */
(HANDLE hInstance, /* Current program instance. */
int hCmdShow) /* Indicates how to show window (open/icon). */
{
HWND HWindow; /* Handle for main program window. */

HInst = hInstance; /* Save instance handle in permanent variable.*/

HWindow = CreateWindow /* Create the main program window. */
("MsgDemoClass", /* Class name passed to RegisterClass. */
"Message Demo Program", /* Text for window title. */
/* Window style: */
WS_OVERLAPPEDWINDOW | /* Has title, system menu, & border. */
WS_HSCROLL | /* Include horizontal scroll bar. */
WS_VSCROLL, /* Include vertical scroll bar. */
CW_USEDEFAULT, /* Horizontal position: use default. */
CW_USEDEFAULT, /* Vertical position: use default. */
CW_USEDEFAULT, /* Width: use default. */
CW_USEDEFAULT, /* Height: use default. */
NULL, /* Parent: none. */
NULL, /* Menu: use class menu. */
HInst, /* Instance to be associated with window. */
NULL); /* Pointer value passed to window: none. */

if (!HWindow) /* 0 window handle = couldn't create window */
return (FALSE); /* FALSE indicates failure. */

ShowWindow /* Display the window. */
(HWindow, /* Window handle. */
hCmdShow); /* Display mode (open/icon). */
UpdateWindow (HWindow); /* Causes window proc. to update window. */

return (TRUE); /* TRUE indicates success. */

} /* end InitInstance */


BOOL InitProgram /* Program initialization function. */
(HANDLE hInstance) /* Current program instance. */
{
WNDCLASS WC; /* Structure for holding class information. */

WC.style = CS_DBLCLKS; /* Enable double-click. */
WC.lpfnWndProc = MainWndProc; /* Window function. */
WC.cbClsExtra = 0; /* No extra data for class */
WC.cbWndExtra = 0; /* No extra data for instance */
WC.hInstance = hInstance; /* Program instance that owns class */
WC.hIcon = LoadIcon (NULL, IDI_APPLICATION); /* Load/assign class icon*/
WC.hCursor = LoadCursor (NULL, IDC_ARROW); /* Load/assign cl. cursor*/
WC.hbrBackground = GetStockObject (WHITE_BRUSH); /* Background brush */
WC.lpszMenuName = "MsgDemoMenu"; /* Name of class menu */
WC.lpszClassName = "MsgDemoClass"; /* Name of window class */
return (RegisterClass (&WC)); /* Register window class */
/* return code*/

} /* end InitProgram */


long FAR PASCAL MainWndProc /* Window procedure for main window. */
(HWND hwnd, /* Window handle. */
unsigned message, /* Message type. */
WORD wParam, /* Message information. */
LONG lParam) /* More message information. */
{
HDC HDspCtx; /* Handle to display context. */
PAINTSTRUCT PS; /* Paint data structure. */
RECT Rect; /* Rectangle data structure. */

switch (message) /* Branch according to message. */
{
case WM_CHAR: /* User pressed character key. */

wsprintf (MsgLine1,"Message: WM_CHAR");
wsprintf (MsgLine2,
"wParam = %04x low lParam = %04x hi lParam = %04x",
wParam, LOWORD (lParam), HIWORD (lParam));
InvalidateRect /* Mark window for repainting. */
(hwnd, /* Handle of main window. */
NULL, /* Area to paint: entire client area */
TRUE); /* Erase background. */
UpdateWindow (hwnd); /* Force immediate repainting. */
return (NULL);

case WM_COMMAND: /* Menu item chosen. */
switch (wParam) /* Branch on menu item identifier. */
{
case IDM_ABOUT: /* "About" menu item chosen. */
MessageBox
(hwnd,
"Windows 3.0 Message Demo Program",
"MsgDemo",
MB_ICONINFORMATION | MB_OK);
break;

case IDM_EXIT: /* "Exit" menu item chosen. */
DestroyWindow (hwnd); /* Destroy main window. */
break;
}
return (NULL);

case WM_DESTROY: /* Window is being destroyed. */
PostQuitMessage (0); /* Posts a WM_QUIT message to */
/* program queue. */
return (NULL);

case WM_HSCROLL: /* Mouse clicked on horizontal scroll bar. */
wsprintf(MsgLine1,"Message: WM_HSCROLL");
wsprintf(MsgLine2,
"wParam = %04x low lParam = %04x hi lParam = %04x",
wParam, LOWORD (lParam), HIWORD (lParam));
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
return (NULL);

case WM_LBUTTONDBLCLK: /* Left mouse button double clicked. */
wsprintf(MsgLine1,"Message: WM_LBUTTONDBLCLK");
wsprintf(MsgLine2,
"wParam = %04x low lParam = %04x hi lParam = %04x",
wParam, LOWORD (lParam), HIWORD (lParam));
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
return (NULL);

case WM_LBUTTONDOWN: /* Left mouse button pressed. */
wsprintf(MsgLine1,"Message: WM_LBUTTONDOWN");
wsprintf(MsgLine2,
"wParam = %04x low lParam = %04x hi lParam = %04x",
wParam, LOWORD (lParam), HIWORD (lParam));
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
return (NULL);

case WM_LBUTTONUP: /* Left mouse button released. */
wsprintf(MsgLine1,"Message: WM_LBUTTONUP");
wsprintf(MsgLine2,
"wParam = %04x low lParam = %04x hi lParam = %04x",
wParam, LOWORD (lParam), HIWORD (lParam));
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
return (NULL);

case WM_MOUSEMOVE: /* Mouse cursor moved within client area.*/
wsprintf(MsgLine1,"Message: WM_MOUSEMOVE");
wsprintf(MsgLine2,
"wParam = %04x low lParam = %04x hi lParam = %04x",
wParam, LOWORD (lParam), HIWORD (lParam));
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
return (NULL);

case WM_PAINT: /* Client area needs repainting */
GetClientRect (hwnd, (LPRECT)&Rect); /* Get dimensions of */
/* client area. */
HDspCtx = BeginPaint /* Get handle to display context */
(hwnd, /* Window handle. */
&PS); /* Address of paint data structure */
DrawText /* Display first string in window */
(HDspCtx, /* Handle to display context. */
MsgLine1, /* Address of string to display. */
-1, /* -1 = string is NULL-terminated */
&Rect, /* Rect. in which string is displayed*/
/* How string is to be displayed: */
DT_CENTER | /* Center it horiz. within 'Rect'. */
DT_TOP | /* Place vert. at top of 'Rect'. */
DT_SINGLELINE); /* Single line of text. */
DrawText /* Display second string in window */
(HDspCtx,
MsgLine2,
-1,
&Rect,
DT_CENTER |
DT_VCENTER | /* Vertical position in center of 'Rect'.*/
DT_SINGLELINE);
EndPaint (hwnd, &PS); /* Tell Windows area is painted. */
return (NULL);


case WM_SIZE: /* Window size has changed. */
wsprintf(MsgLine1,"Message: WM_SIZE");
wsprintf(MsgLine2,
"wParam = %04x low lParam = %04x hi lParam = %04x",
wParam, LOWORD (lParam), HIWORD (lParam));
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
return (NULL);

case WM_VSCROLL: /* Mouse clicked on vertical scroll bar. */
wsprintf(MsgLine1,"Message: WM_VSCROLL");
wsprintf(MsgLine2,
"wParam = %04x low lParam = %04x hi lParam = %04x",
wParam, LOWORD (lParam), HIWORD (lParam));
InvalidateRect (hwnd, NULL, TRUE);
UpdateWindow (hwnd);
return (NULL);

default: /* Have Windows do default processing on all */
/* other messages: */
return (DefWindowProc (hwnd, message, wParam, lParam));

} /* end switch */

} /* end MainWndProc */