COMDLG32.C
资源名称:MSDN_VC98.zip [点击查看]
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:18k
源码类别:
Windows编程
开发平台:
Visual C++
- //THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
- //ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
- // PARTICULAR PURPOSE.
- //
- // Copyright 1994-1996 Microsoft Corporation. All Rights Reserved.
- //
- // PROGRAM: Comdlg32.c
- //
- // PURPOSE: Demonstrates how to manipulate the file open common dialog.
- //
- // PLATFORMS: Windows 95
- //
- // FUNCTIONS:
- // WinMain() - calls initialization function, processes message loop
- // InitApplication() - Initializes window data nd registers window
- // InitInstance() -saves instance handle and creates main window
- // MainWindProc() Processes messages
- // About() - Process menssages for "About" dialog box
- // OpenTheFile() - Processes the File Open common dialog box
- // ComDlg32DlgProc() - Hook procedure for GetOpenFileName() common dialog
- // TestNotify() - Processes the WM_NOTIFY function for ComDlg32DlgProc
- // ProcessCDError() - uses CommonDialogExtendedError() to output useful error
- // messages
- //
- // SPECIAL INSTRUCTIONS: N/A
- //
- #include <windows.h> // includes basic windows functionality
- #include <commdlg.h> // includes common dialog functionality
- #include <dlgs.h> // includes common dialog template defines
- #include <cderr.h> // includes the common dialog error codes
- #include "COMDLG32.h" // includes application-specific information
- const char szmsgSHAREVIOLATION[] = SHAREVISTRING; // string for sharing violation
- const char szmsgFILEOK[] = FILEOKSTRING; // string for OK button
- const char szCommdlgHelp[] = HELPMSGSTRING; // string for Help button
- UINT cdmsgShareViolation = 0; // identifier from RegisterWindowMessage
- UINT cdmsgFileOK = 0; // identifier from RegisterWindowMessage
- UINT cdmsgHelp = 0; // identifier from RegisterWindowMessage
- typedef struct _MYDATA
- {
- char szTest1[80]; // a test buffer containing the file selected
- char szTest2[80]; // a test buffer containing the file path
- } MYDATA, FAR * LPMYDATA;
- HINSTANCE g_hInst; // the current instance
- MYDATA sMyData; // an instance of a MYDATA
- //
- // FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
- //
- // PURPOSE: Entry point for the application.
- //
- // COMMENTS:
- //
- // This function initializes the application and processes the
- // message loop.
- //
- int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow )
- {
- MSG msg;
- if (!InitApplication(hInstance))
- return (FALSE);
- // Create the main window.
- if (!InitInstance(hInstance, nCmdShow))
- return (FALSE);
- // Acquire and dispatch messages until a WM_QUIT message is received.
- while (GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg); // Translates virtual key codes.
- DispatchMessage(&msg); // Dispatches message to window.
- }
- return (msg.wParam); // Returns the value from PostQuitMessage.
- }
- //
- // FUNCTION: InitApplication(HANDLE)
- //
- // PURPOSE: Initializes window data and registers window class
- //
- // COMMENTS:
- //
- // In this function, we initialize a window class by filling out a data
- // structure of type WNDCLASS and calling the Windows RegisterClass()
- // function.
- //
- BOOL InitApplication(HANDLE hInstance)
- {
- WNDCLASS wc;
- // Register the window class for my window. */
- wc.style = 0; // Class style.
- wc.lpfnWndProc = (WNDPROC)MainWndProc; // Window procedure for this class.
- wc.cbClsExtra = 0; // No per-class extra data.
- wc.cbWndExtra = 0; // No per-window extra data.
- wc.hInstance = hInstance; // Application that owns the class.
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = "Comdlg32Menu"; // Name of menu resource in .RC file.
- wc.lpszClassName = "Comdlg32WClass"; // Name used in call to CreateWindow.
- return (RegisterClass(&wc));
- }
- //
- // FUNCTION: InitInstance(HANDLE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- BOOL InitInstance( HANDLE hInstance, int nCmdShow)
- {
- HWND hWnd;
- // Save off the handle to the current instance.
- g_hInst = hInstance;
- // Create a main window for this application instance.
- hWnd = CreateWindow(
- "Comdlg32WClass",
- "Common Dialog Sample Application",
- WS_OVERLAPPEDWINDOW, // Window style.
- CW_USEDEFAULT, // Default horizontal position.
- CW_USEDEFAULT, // Default vertical position.
- CW_USEDEFAULT, // Default width.
- CW_USEDEFAULT, // Default height.
- NULL, // Overlapped windows have no parent.
- NULL, // Use the window class menu.
- g_hInst, // This instance owns this window.
- NULL // Pointer not needed.
- );
- // If window could not be created, return "failure".
- if (!hWnd)
- return (FALSE);
- // Register the window messages to receive.
- cdmsgShareViolation = RegisterWindowMessage(szmsgSHAREVIOLATION);
- cdmsgFileOK = RegisterWindowMessage(szmsgFILEOK);
- cdmsgHelp = RegisterWindowMessage(szCommdlgHelp);
- // Make the window visible; update its client area; and return "success".
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return (TRUE);
- }
- //
- // FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // MESSAGES:
- //
- // WM_CREATE - initialize window and create the MLE
- // WM_COMMAND - process the application menu
- // WM_SIZE - size the MLE in the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LONG APIENTRY MainWndProc( HWND hWnd, UINT message, UINT wParam, LONG lParam)
- {
- static HWND hwndEdit;
- CHAR lpszHello[] = "Choose File Open from the Options menu for a demo.";
- switch (message) {
- case WM_CREATE:
- // Create an MLE for the file contents.
- hwndEdit = CreateWindow(
- "EDIT",
- NULL,
- WS_CHILD | WS_VISIBLE | WS_VSCROLL |
- ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
- 0, 0, 0, 0,
- hWnd,
- (HMENU) ID_EDITCHILD,
- (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
- NULL);
- // Update the MLE.
- SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM) lpszHello);
- return 0;
- case WM_SIZE:
- // Make the edit control the size of the window's client area.
- MoveWindow(hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
- return 0;
- case WM_COMMAND: // message: command from application menu
- switch( LOWORD( wParam ))
- {
- case IDM_FILEOPEN:
- // Call the FileOpen common dialog to open the file.
- OpenTheFile( hWnd, hwndEdit );
- break;
- case IDM_EXIT:
- PostQuitMessage(0);
- break;
- case IDM_ABOUT:
- DialogBox(g_hInst,
- MAKEINTRESOURCE(IDD_ABOUT),
- hWnd,
- (DLGPROC)About);
- break;
- default:
- return (DefWindowProc(hWnd, message, wParam, lParam));
- }
- break;
- case WM_DESTROY: // message: window being destroyed
- PostQuitMessage(0);
- break;
- default:
- return (DefWindowProc(hWnd, message, wParam, lParam));
- }
- return (0);
- }
- //
- // FUNCTION: OpenTheFile(HWND hwnd, HWND hwndEdit)
- //
- // PURPOSE: Invokes common dialog function to open a file and opens it.
- //
- // COMMENTS:
- //
- // This function initializes the OPENFILENAME structure and calls
- // the GetOpenFileName() common dialog function.
- //
- // RETURN VALUES:
- // TRUE - The file was opened successfully and read into the buffer.
- // FALSE - No files were opened.
- //
- //
- BOOL OpenTheFile( HWND hWnd, HWND hWndEdit )
- {
- HANDLE hFile;
- DWORD dwBytesRead;
- DWORD dwFileSize;
- OPENFILENAME OpenFileName;
- TCHAR szFile[MAX_PATH] = "