|
|
1.1 root 1: // ************************************************************************
2: //
3: // Microsoft Developer Support
4: // Copyright (c) 1993 Microsoft Corporation
5: //
6: // ************************************************************************
7: // MODULE : MinRec.C
8: // PURPOSE : A Small Win32 Recorder-like Sample Application
9: // FUNCTIONS :
10: // WinMain() - initializes the main window, dispatches messages
11: // MainDlgProc() - processes messages for the main dialog box window
12: // ErrorMessageBox() - displays an error message box when called
13: // COMMENTS :
14: // ************************************************************************
15: #define STRICT // strict type checking enabled
16: #define UNICODE // make the application unicode complient
17: #include <Windows.H> // required for all Windows applications
18:
19: #include "MinRec.H" // specific to this program
20: #include "RecHook.H" // global journal hook functions
21:
22: // internal defines
23: // ------------------------------------------------------------------------
24: typedef struct GLOBAL_STRUCT {
25: HWND hDlgMain; // Main dialog box window handle
26: HINSTANCE hInstance; // current instance
27: LPCTSTR lpszApiFailedMsg; // "A Windows API Failed" message
28: } GLOBAL, *PGLOBAL;
29:
30: // internal data
31: // ------------------------------------------------------------------------
32: GLOBAL Global;
33: LPCTSTR lpszSourceFileName = TEXT(__FILE__);
34:
35: // internal function prototypes
36: // ------------------------------------------------------------------------
37: BOOL CALLBACK MainDlgProc ( HWND, UINT, WPARAM, LPARAM );
38: BOOL CALLBACK AboutDlgProc( HWND, UINT, WPARAM, LPARAM );
39: BOOL ErrorMessageBox ( LPCTSTR, LPCTSTR, LPCTSTR, INT );
40:
41: // ************************************************************************
42: // FUNCTION : WinMain( HINSTANCE, HINSTANCE, LPSTR, INT )
43: // PURPOSE : Calls initialization function, processes message loop
44: // COMMENTS :
45: // ************************************************************************
46: INT PASCAL
47: WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
48: INT nCmdShow )
49: {
50: MSG Msg;
51: HACCEL hAccel;
52: LPCTSTR lpszClassName = TEXT( "MinRecClass" );
53: LPCTSTR lpszMenuName = TEXT( "MinRecMenu" );
54: LPCTSTR lpszIconName = TEXT( "MinRecIcon" );
55: LPCTSTR lpszAccelName = TEXT( "MinRecAccel" );
56:
57: //-- check if Win32s, if so display notice and terminate
58: if( GetVersion() & 0x80000000 ) {
59: MessageBox( NULL,
60: TEXT( "Sorry, MinRec requires Windows NT.\n" )
61: TEXT( "This application will now terminate." ),
62: TEXT( "Windows NT Required!" ),
63: MB_OK );
64: return( -1 );
65: }
66:
67: Global.hInstance = hInstance;
68: Global.lpszApiFailedMsg = TEXT( "A Windows API Failed" );
69:
70: //-- register the window class
71: {
72: WNDCLASS WndClass;
73:
74: WndClass.style = CS_HREDRAW | CS_VREDRAW;
75: WndClass.lpfnWndProc = (WNDPROC) MainDlgProc;
76: WndClass.cbClsExtra = (int) NULL;
77: WndClass.cbWndExtra = DLGWINDOWEXTRA;
78: WndClass.hInstance = hInstance;
79: WndClass.hIcon = LoadIcon( Global.hInstance, lpszIconName );
80: WndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
81: WndClass.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE+1);
82: WndClass.lpszMenuName = lpszMenuName;
83: WndClass.lpszClassName = lpszClassName;
84:
85: if( !RegisterClass(&WndClass) ) {
86: ErrorMessageBox( TEXT("RegisterClass()"),
87: Global.lpszApiFailedMsg, lpszSourceFileName, __LINE__ );
88: return( FALSE );
89: }
90: }
91:
92: //-- Create a main dialog window for this application instance
93: Global.hDlgMain = CreateDialog( Global.hInstance, TEXT("MainDlgBox"),
94: NULL, NULL );
95:
96: //-- If window could not be created, return "failure"
97: if( !Global.hDlgMain ) {
98: ErrorMessageBox( TEXT("CreateDialog()"),
99: Global.lpszApiFailedMsg, lpszSourceFileName, __LINE__ );
100: return( FALSE );
101: }
102:
103: //-- Load main menu accelerators
104: if( !(hAccel = LoadAccelerators( Global.hInstance, lpszAccelName) ) ) {
105: ErrorMessageBox( TEXT("LoadAccelerators()"),
106: Global.lpszApiFailedMsg, lpszSourceFileName, __LINE__ );
107: return( FALSE );
108: }
109:
110: //-- Make the window visible; update its client area; and return "success"
111: ShowWindow( Global.hDlgMain, SW_SHOWDEFAULT ); // Show the window
112:
113: //-- Acquire and dispatch messages until a WM_QUIT message is received.
114: while( GetMessage( &Msg, NULL, 0, 0 ) ) {
115: if( !TranslateAccelerator( Global.hDlgMain, hAccel, &Msg ) ) {
116: if( !IsDialogMessage( Global.hDlgMain, &Msg ) ) {
117: TranslateMessage( &Msg ); // Translates virtual key codes
118: DispatchMessage( &Msg ); // Dispatches message to window
119: }
120: }
121: }
122:
123: return( Msg.wParam ); // Returns the value from PostQuitMessage
124: UNREFERENCED_PARAMETER( lpszCmdLine ); // avoid the warning
125: }
126:
127:
128: // ************************************************************************
129: // FUNCTION : MainDlgProc( HWND, UINT, WPARAM, LPARAM )
130: // PURPOSE : Processes messages
131: // MESSAGES :
132: // WM_COMMAND - application menu
133: // IDM_FILE_EXIT - exit the application
134: // IDM_HELP_ABOUT - About Dialog Box
135: // ...
136: // WM_CREATE - window initialization
137: // WM_CLOSE - handles cleanup
138: // WM_DESTROY - destroys window
139: // ************************************************************************
140: BOOL CALLBACK
141: MainDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
142: {
143: switch( uMsg ) {
144:
145: case WM_COMMAND:
146: switch( LOWORD(wParam) ) {
147:
148: case IDM_FILE_EXIT:
149: PostMessage( hWnd, WM_CLOSE, (WPARAM) 0, (LPARAM) 0 );
150: return( TRUE );
151:
152: case IDM_MACRO_PLAYBACK:
153: StartJournalPlayback();
154: return( TRUE );
155:
156: case IDM_MACRO_RECORD:
157: StartJournalRecord();
158: return( TRUE );
159:
160: case IDM_MACRO_STOP:
161: StopAllJournalling();
162: return( TRUE );
163:
164: case IDM_HELP_ABOUT:
165: if( DialogBox( Global.hInstance, TEXT( "AboutDlgBox" ),
166: hWnd, AboutDlgProc ) == -1 )
167: ErrorMessageBox( TEXT("DialogBox()"),
168: Global.lpszApiFailedMsg, lpszSourceFileName, __LINE__ );
169: return( TRUE );
170:
171: }
172: break;
173:
174: case WM_SYSCOMMAND:
175: switch( LOWORD(wParam) ) {
176:
177: // return a nonzero value to disable the screen saver
178: case SC_SCREENSAVE:
179: return( TRUE );
180:
181: }
182: break;
183:
184: case WM_INITDIALOG:
185: return( TRUE );
186:
187: case WM_CLOSE:
188: DestroyWindow( Global.hDlgMain );
189: Global.hDlgMain = NULL;
190: PostQuitMessage( FALSE );
191: }
192:
193: return( DefDlgProc( hWnd, uMsg, wParam, lParam ) );
194: }
195:
196:
197: // ************************************************************************
198: // FUNCTION : AboutDlgProc( HWND, UINT, WPARAM, LPARAM )
199: // PURPOSE : Processes messages for "About" dialog box
200: // MESSAGES :
201: // WM_INITDIALOG - initialize dialog box
202: // WM_COMMAND - Input received
203: // IDOK - OK button selected
204: // IDCANCEL - Cancel button selected
205: // COMMENTS:
206: // No initialization is needed for this particular dialog box.
207: // In this case, TRUE must be returned to Windows.
208: // Wait for user to click on "Ok" button, then close the dialog box.
209: // ************************************************************************
210: BOOL CALLBACK
211: AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
212: {
213: switch( uMsg ) {
214:
215: case WM_COMMAND:
216: switch( LOWORD(wParam) ) {
217:
218: case IDOK:
219: EndDialog( hDlg, TRUE );
220: return( TRUE );
221:
222: case IDCANCEL:
223: EndDialog( hDlg, FALSE );
224: return( TRUE );
225: }
226: break;
227:
228: case WM_INITDIALOG:
229: return( TRUE );
230:
231: case WM_CLOSE:
232: return( TRUE );
233:
234: }
235:
236: return( FALSE );
237: UNREFERENCED_PARAMETER( lParam );
238: }
239:
240:
241: // ************************************************************************
242: // FUNCTION : ErrorMessageBox( LPCTSTR, LPCTSTR, LPCTSTR, INT )
243: // PURPOSE : Displays an error message box with various error information
244: // and allows the user to terminate or continue the process.
245: // For a Win32 Application, GetLastError and FormatMessage are
246: // user to retrieve the last API error code and error message.
247: // COMMENTS :
248: // ************************************************************************
249: BOOL
250: ErrorMessageBox( LPCTSTR lpszText, LPCTSTR lpszTitle, LPCTSTR lpszFile,
251: INT Line )
252: {
253: #define ERROR_BUFFER_SIZE 512
254:
255: static TCHAR szMessageFormat[] =
256: TEXT( "%s\n" )
257: TEXT( "\n" )
258: TEXT( "-- Error Information --\n" )
259: TEXT( "File : %s\n" )
260: TEXT( "Line : %d\n" )
261: TEXT( "Error Number : %d\n" )
262: TEXT( "Error Message : %s\n" )
263: TEXT( "\n" )
264: TEXT( "Press OK to terminate this application.\n" )
265: TEXT( "Press Cancel to ignore the error and continue." );
266:
267: LPTSTR lpFormatMessageBuffer;
268: DWORD dwFormatMessage;
269: DWORD dwGetLastError;
270: HLOCAL hMessageBoxBuffer;
271: LPVOID lpMessageBoxBuffer;
272:
273: //-- get the system error
274: dwGetLastError = GetLastError();
275:
276: //-- perform a simple check on the needed buffer size
277: if( lstrlen(lpszText) > (ERROR_BUFFER_SIZE - lstrlen(szMessageFormat)) )
278: return( FALSE );
279:
280: //-- allocate the message box buffer
281: hMessageBoxBuffer = LocalAlloc( LMEM_FIXED, ERROR_BUFFER_SIZE );
282: lpMessageBoxBuffer = LocalLock( hMessageBoxBuffer );
283:
284: //-- get the system error message
285: dwFormatMessage = FormatMessage(
286: FORMAT_MESSAGE_ALLOCATE_BUFFER
287: | FORMAT_MESSAGE_FROM_SYSTEM,
288: NULL, dwGetLastError, LANG_NEUTRAL,
289: (LPTSTR) &lpFormatMessageBuffer, 0, NULL );
290: if( !dwFormatMessage )
291: lpFormatMessageBuffer = TEXT("FormatMessage() failed!");
292:
293: //-- format the error messge box string
294: wsprintf( lpMessageBoxBuffer, szMessageFormat, lpszText, lpszFile,
295: Line, dwGetLastError, lpFormatMessageBuffer );
296:
297: // -- display the error and allow the user to terminate or continue
298: if( MessageBox( NULL, lpMessageBoxBuffer, lpszTitle,
299: MB_APPLMODAL | MB_ICONSTOP | MB_OKCANCEL ) == IDOK )
300: ExitProcess( 0 );
301:
302: //-- free all buffers
303: if( dwFormatMessage )
304: LocalFree( (HLOCAL) lpFormatMessageBuffer );
305: LocalFree( (HLOCAL) hMessageBoxBuffer );
306:
307: return( TRUE );
308: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.