home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
w3_prog
/
mleenter.arj
/
MLEENTER.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
9KB
|
355 lines
/*
*
* Window EditSubclasser App
*
* Microsoft WinSDK Support
* Copyright (c) 1991 Microsoft Corporation. All rights reserved.
*
*/
#define NOMINMAX
#include <windows.h>
#include <stdlib.h>
#include "mleenter.h"
HWND hgWnd; //Global Window Handle. Very useful.
HANDLE hgInst; //Global Instance Handle. Ditto.
//Variables for the dialog box.
WORD wEnterMethod;
FARPROC lpMLEEnterSubProc;
FARPROC lpEditProcOrg;
/*
* Array of procedure addresses for all the dialog boxes. DoDialog
* makes a ProcInstance when needed from the entry in this array.
*/
FARPROC rglpDialogs[CDIALOGS]={
MLEEnterDlgProc
};
/*
* WinMain
*
* Purpose:
* Main entry point of application. Should register the app class
* if a previous instance has not done so and do any other one-time
* initializations.
*
* Parameters:
* See Windows SDK Guide to Programming, page 2-3
*
* Return Value:
* Value to return to Windows--termination code.
*
*/
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
WNDCLASS wndClass;
HANDLE hStringMem;
HBRUSH hBrushBk;
HWND hWnd;
HDC hDC;
MSG msg;
short i;
hgInst=hInstance;
if (!hPrevInstance)
{
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = MLEEnterWndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(hInstance, "MLEEnterIcon");
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = COLOR_WINDOW + 1;
wndClass.lpszMenuName = "MLEEnterMenu";
wndClass.lpszClassName = "MLEEnter";
if (!RegisterClass(&wndClass))
return FALSE;
}
hWnd=CreateWindow("MLEEnter",
"Edit Control Enter Key",
WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW,
35, 35, 400, 150,
NULL, NULL, hInstance, NULL);
hgWnd=hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0,0 ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*
* MLEEnterWndProc
*
* Purpose:
* Window class procedure. Standard callback.
*
* Parameters:
* The standard. See Section 2.4 Windows SDK Guide to Programming,
* page 2-4.
*
* Return Value:
* See Parameters, above.
*
*/
long FAR PASCAL MLEEnterWndProc(HWND hWnd, unsigned iMessage,
WORD wParam, LONG lParam)
{
switch (iMessage)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch (wParam)
{
case IDM_DIALOGMLEENTER:
WDoDialog(IDD_MLEENTER);
break;
case IDM_DIALOGEXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0L);
break;
}
default:
return (DefWindowProc(hWnd, iMessage, wParam, lParam));
}
return 0L;
}
/*
* WDoDialog
*
* Purpose:
* Handles necessary housekeeping to display a dialog, i.e. making
* the ProcInstance and calling DialogBox. Also processes the return
* value in case of errors.
*
* Parameters:
* wID WORD indicating the dialog to display. This should
* ONLY be a value defined by IDD_*
*
* Return Value:
* -1 If the function fails to display the dialog.
* Otherwise the value that the dialog passes from EndDialog.
*
*/
WORD PASCAL WDoDialog(WORD wID)
{
FARPROC lpDialogProc;
WORD wRet;
/*
* Associate the procedure with this instance's data. Exit
* if we can't do it.
*/
lpDialogProc=MakeProcInstance(rglpDialogs[wID-1], hgInst);
if (lpDialogProc==NULL)
wRet=-1;
else
wRet=DialogBox(hgInst, MAKEINTRESOURCE(wID), hgWnd, lpDialogProc);
FreeProcInstance(lpDialogProc);
return wRet;
}
/*
* MLEEnterDlgProc
*
* Purpose:
* Dialog procedure that subclasses the multiline edit control to
* detect the caret position within the control.
*
* Parameters:
* Standard for a window procedure.
*
* Return Value:
* Standard for dialog procedure.
*
*/
BOOL FAR PASCAL MLEEnterDlgProc(HWND hDlg, WORD iMessage,
WORD wParam, LONG lParam)
{
static BOOL fEditFocus;
static BOOL fEditChange;
PAINTSTRUCT ps;
TEXTMETRIC tm;
BYTE rgb[256];
LONG lOld;
HDC hDC;
HWND hEdit;
switch (iMessage)
{
case WM_INITDIALOG:
wEnterMethod=ID_METHODDEF;
CheckRadioButton(hDlg, ID_METHODDEF, ID_METHODSUB, ID_METHODDEF);
lpMLEEnterSubProc=MakeProcInstance((FARPROC)MLEEnterSubProc, hgInst);
lOld=GetWindowLong(GetDlgItem(hDlg, ID_EDIT), GWL_WNDPROC);
lpEditProcOrg=(FARPROC)lOld;
return(TRUE);
case DM_GETDEFID:
/*
* Do this method only if ID_METHODDEF is selected, the edit control
* has the focus, and the TAB key is not down. We check the TAB since
* we will get DM_GETDEFID when we are tabbed to, in which case
* we don't want to send a LF.
*/
if (ID_METHODDEF==wEnterMethod && fEditFocus &&
!(GetKeyState(VK_TAB) & 0x8000))
{
SendDlgItemMessage(hDlg, ID_EDIT, WM_CHAR, 0x0A, 0L);
return TRUE;
}
break;
case WM_COMMAND:
switch(wParam)
{
case ID_METHODSUB:
wEnterMethod=wParam;
//Hook in the subclass procedure.
SetWindowLong(GetDlgItem(hDlg, ID_EDIT),
GWL_WNDPROC, (LONG)lpMLEEnterSubProc);
break;
case ID_METHODDEF:
wEnterMethod=wParam;
//Unhook the subclass procedure.
SetWindowLong(GetDlgItem(hDlg, ID_EDIT),
GWL_WNDPROC, (LONG)lpEditProcOrg);
break;
case ID_EDIT:
if (EN_KILLFOCUS==HIWORD(lParam))
fEditFocus=FALSE;
if (EN_SETFOCUS==HIWORD(lParam))
fEditFocus=TRUE;
break;
case IDOK:
EndDialog(hDlg, TRUE);
SetWindowLong(GetDlgItem(hDlg, ID_EDIT),
GWL_WNDPROC, (LONG)lpEditProcOrg);
FreeProcInstance(lpMLEEnterSubProc);
return TRUE;
default:
break;
}
break;
default:
break;
}
return FALSE;
}
/*
* MLEEnterSubProc
*
* Purpose:
* Dialog procedure that subclasses the multiline edit control to
* detect the caret position within the control.
*
* Parameters:
* Standard for a window procedure.
*
* Return Value:
* Standard for dialog procedure.
*
*/
LONG FAR PASCAL MLEEnterSubProc(HWND hWnd, WORD iMessage,
WORD wParam, LONG lParam )
{
WORD iLine;
WORD iCol;
WORD iSel;
WORD iTopLine;
POINT pt;
switch (iMessage)
{
case WM_KEYDOWN:
if (ID_METHODSUB==wEnterMethod && VK_RETURN==wParam)
{
PostMessage(hWnd, WM_CHAR, 0x0A, 0L);
return 0L;
}
break;
default:
break;
}
return CallWindowProc(lpEditProcOrg, hWnd, iMessage, wParam, lParam);
}