home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Microsoft Programmer's Library 1.3
/
Microsoft-Programers-Library-v1.3.iso
/
sampcode
/
win_lrn
/
dialog
/
senddlg.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-08-10
|
10KB
|
238 lines
/*
* SendDlgItemMessage
* senddlg.c,
*
* This program will open up a Dialog box with an edit conrol. When the
* user hits the ADD button, the program will send the LISTBOX a
* LB_ADDSTRING message to along with appropriate parameters to add the
* string in the edit control to the listbox.
*
*/
#include "windows.h" /* required for all Windows applications */
#include "senddlg.h" /* specific to this program */
HANDLE hInst; /* current instance */
int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
HANDLE hInstance; /* current instance */
HANDLE hPrevInstance; /* previous instance */
LPSTR lpCmdLine; /* command line */
int nCmdShow; /* show-window type (open/icon) */
{
HWND hWnd; /* window handle */
MSG msg; /* message */
HANDLE hMenu; /* Handle to the menu */
if (!hPrevInstance) /* Has application been initialized? */
if (!SendDlgInit(hInstance))
return (NULL); /* Exits if unable to initialize */
hInst = hInstance; /* Saves the current instance */
hMenu = LoadMenu( hInst, (LPSTR)"SendDlgMenu" );
/* Load the menu */
hWnd = CreateWindow("SendDlg", /* window class */
"SendDlg Sample Application", /* window name */
WS_OVERLAPPEDWINDOW, /* window style */
CW_USEDEFAULT, /* x position */
CW_USEDEFAULT, /* y position */
CW_USEDEFAULT, /* width */
CW_USEDEFAULT, /* height */
NULL, /* parent handle */
hMenu, /* menu or child ID */
hInstance, /* instance */
NULL); /* additional info */
if (!hWnd) /* Was the window created? */
return (NULL);
ShowWindow(hWnd, nCmdShow); /* Shows the window */
UpdateWindow(hWnd); /* Sends WM_PAINT message */
while (GetMessage(&msg, /* message structure */
NULL, /* handle of window receiving the message */
NULL, /* lowest message to examine */
NULL)) /* highest message to examine */
{
TranslateMessage(&msg); /* Translates virtual key codes */
DispatchMessage(&msg); /* Dispatches message to window */
}
return (msg.wParam); /* Returns the value from PostQuitMessage */
}
BOOL SendDlgInit(hInstance)
HANDLE hInstance; /* current instance */
{
HANDLE hMemory; /* handle to allocated memory */
PWNDCLASS pWndClass; /* structure pointer */
BOOL bSuccess; /* RegisterClass() result */
hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
pWndClass = (PWNDCLASS) LocalLock(hMemory);
pWndClass->style = NULL;
pWndClass->lpfnWndProc = SendDlgWndProc;
pWndClass->hInstance = hInstance;
pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
pWndClass->lpszMenuName = (LPSTR) "SendDlgMenu";
pWndClass->lpszClassName = (LPSTR) "SendDlg";
bSuccess = RegisterClass(pWndClass);
LocalUnlock(hMemory); /* Unlocks the memory */
LocalFree(hMemory); /* Returns it to Windows */
return (bSuccess); /* Returns result of registering the window */
}
long FAR PASCAL SendDlgWndProc(hWnd, message, wParam, lParam)
HWND hWnd; /* window handle */
unsigned message; /* type of message */
WORD wParam; /* additional information */
LONG lParam; /* additional information */
{
FARPROC lpProcAbout; /* pointer to the "About" function */
HMENU hMenu; /* handle to the System menu */
switch (message) {
case WM_SYSCOMMAND: /* message: command from system menu */
if (wParam == ID_ABOUT) {
lpProcAbout = MakeProcInstance(About, hInst);
DialogBox(hInst, /* current instance */
"AboutBox", /* resource to use */
hWnd, /* parent handle */
lpProcAbout); /* About() instance address */
FreeProcInstance(lpProcAbout);
break;
}
else /* Lets Windows process it */
return (DefWindowProc(hWnd, message, wParam, lParam));
case WM_CREATE: /* message: window being created */
/* Get the handle of the System menu */
hMenu = GetSystemMenu(hWnd, FALSE);
/* Add a separator to the menu */
ChangeMenu(hMenu, /* menu handle */
NULL, /* menu item to change */
NULL, /* new menu item */
NULL, /* menu identifier */
MF_APPEND | MF_SEPARATOR); /* type of change */
/* Add new menu item to the System menu */
ChangeMenu(hMenu, /* menu handle */
NULL, /* menu item to change */
"A&bout SendDlg...", /* new menu item */
ID_ABOUT, /* menu identifier */
MF_APPEND | MF_STRING); /* type of change */
break;
case WM_COMMAND:
if ( wParam == IDM_DIALOG )
{
FARPROC lpProcLineList;
lpProcLineList = MakeProcInstance( LineList, hInst );
DialogBox( hInst, /* Current Instance */
"ListIn", /* Resource to use */
hWnd, /* Parent Handle */
lpProcLineList );
FreeProcInstance( lpProcLineList );
break;
}
case WM_DESTROY: /* message: window being destroyed */
PostQuitMessage(0);
break;
default: /* Passes it on if unproccessed */
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (NULL);
}
BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
switch (message) {
case WM_INITDIALOG: /* message: initialize dialog box */
return (TRUE);
case WM_COMMAND: /* message: received a command */
if (wParam == IDOK) { /* "OK" box selected? */
EndDialog(hDlg, NULL); /* Exits the dialog box */
return (TRUE);
}
break;
}
return (FALSE); /* Didn't process a message */
}
BOOL FAR PASCAL LineList(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
char szBuffer[ 50 ]; /* Buffer to hold text from edit control */
switch (message) {
case WM_INITDIALOG: /* message: initialize dialog box */
return (TRUE);
case WM_COMMAND: /* message: received a command */
if (wParam == ID_ADD ) /* "ADD" box selected? */
{
GetDlgItemText( hDlg, /* Handle to the dialog box */
ID_EDIT, /* ID of item to get text from */
(LPSTR)szBuffer, /* Pointer to buffer */
50 ); /* Maximum size of buffer */
SendDlgItemMessage( hDlg, /* Handle to the dialog box */
ID_LISTBOX, /* ID for item to send the
* message to */
LB_ADDSTRING, /* Message to send */
0, /* The wParam parameter */
(LONG)(LPSTR)szBuffer ); /* lParam */
SetDlgItemText( hDlg, /* Handle to the dialog box */
ID_EDIT, /* ID for the edit control */
(LPSTR)"" ); /* Text to put in edit control */
SetFocus( GetDlgItem( hDlg, ID_EDIT ) );
/* Give the focus to the edit control */
/* The SendDlgItemMessage sends a message to the Listbox to add the
* string to the listbox. After that we set the text in the edit control
* to an empty string, effectively clearing the edit control. Then
* finally we set the focus to the edit control. */
return (TRUE); /* We processed a message */
}
if ( wParam == ID_OK )
{
EndDialog(hDlg, NULL); /* Exits the dialog box */
return (TRUE);
}
if ( wParam == ID_CANCEL )
{
EndDialog(hDlg, NULL); /* Exits the dialog box */
return (TRUE);
}
break;
}
return (FALSE); /* Didn't process a message */
}