OPEN.C
资源名称:MSDN_VC98.zip [点击查看]
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:33k
源码类别:
Windows编程
开发平台:
Visual C++
- /************************************************************************
- File: open.c
- Purpose:
- This file contains the routines to control the CDTEST.EXE "Open" dialog
- box. The "Open" dialog box allows the user to enter values into an
- OPENFILENAME structure and then create GetOpenFileName() dialog boxes
- on the fly.
- Functions:
- DoOpenDialog() -- starts off the main dialog for "open"
- OpenFunc() -- Callback function for main dialog
- InitOpenStruct() -- Fills initial OPENFILENAME structure.
- FillOpenDlg() -- Fills the dialog with the values from
- the OPENFILENAME structure.
- GetOpenDlg() -- Retrieves the users entries from the
- main dialog and puts them in the
- OPENFILENAME structure.
- InitFilterString() -- Creates filter string
- InitCustFiltString() -- Creates custom filter string
- InterpretCustomFilterString() -- Parses custom filter string returned
- from GetOpenFileName()
- GetCorrectResourceHandle() -- Loads custom templates from file
- as resource handles
- OpenSaveHookProc() -- The hook function that will be
- called if GetOpen/Save is called
- with the OFN_ENABLEHOOK flag set.
- MultiThreadOpenSave() -- Creates two Open/Save dialogs that
- the user can simultaneously access
- OpenSaveThread1Proc() -- The starting address of thread 1
- OpenSaveThread2Proc() -- The starting address of thread 2
- DoOpenSaveStuff() -- Does the actuall calling of
- GetOpen/SaveFileName()
- OpenMultiThreadEnableButtons() -- Enables and disables buttons in
- main dialog. Needed when multi-
- threading.
- ************************************************************************/
- #include <windows.h>
- #include <commdlg.h>
- #include <stdlib.h>
- #include <winnls.h>
- #include "cdtest.h"
- #include "open.h"
- #include "save.h"
- #include "dlgs.h" //include file that contains all #defines for the
- //commdlg dialog templates.
- /* All functions defined in this file + 1 external function
- and one external variable */
- extern UINT uMode ; //see cdtest.c
- extern LONG MyAtol(LPTSTR, BOOL, LPBOOL) ;
- void InterpretCustomFilterString(void) ;
- HANDLE GetCorrectResourceHandle(void) ;
- void DoOpenSaveStuff(LPOPENFILENAME) ;
- UINT APIENTRY OpenSaveHookProc(HWND hwnd, UINT msg, UINT wParam, LONG lParam) ;
- DWORD OpenSaveThread1Proc(LPDWORD) ;
- DWORD OpenSaveThread2Proc(LPDWORD) ;
- void MultiThreadOpenSave(void) ;
- void OpenMultiThreadEnableButtons(BOOL, HWND) ;
- /* All global variables defined in this file */
- HWND hwndMainDialog ; //global handle for open dialog.
- HANDLE hRes ; //handles to the resource and dialog for
- HANDLE hDialog ; //ofn_enabletemplatehandle
- HBRUSH hBrushDlg ;
- HBRUSH hBrushEdit ; //brush handles for new colors done with hook proc
- HBRUSH hBrushButton ;
- HANDLE hOpenSaveThread1, hOpenSaveThread2 ; //variables for the
- DWORD dwThreadID1, dwThreadID2 ; //multithreading part
- DWORD dwThreadParm1, dwThreadParm2 ;
- OPENFILENAME ofnThread1, ofnThread2 ;
- int nOpenDialogCount ;
- /************************************************************************
- Function: DoOpenDialog(HWND)
- Purpose: To create the GetOpenFileName() and GetSaveFileName()
- creation dialog.
- Returns: Nothing.
- Comments:
- GetOpenFileName() and GetSaveFileName() are similiar enough so that
- the same dialog can be used to edit their creation structure elements,
- so a global variable "bDoOpenDlg" keeps track of which one to create
- when the user clicks the OK or Multithread buttons...
- ************************************************************************/
- void DoOpenDialog(HWND hwnd)
- {
- bDoOpenDlg = TRUE ;
- DialogBox(hInst, MAKEINTRESOURCE(ID_OPENDIALOG), hwnd, OpenFunc) ;
- }
- /************************************************************************
- Function: OpenFunc(HWND, UINT, UINT, LONG)
- Purpose:
- This is the callback function for the dialog box containing the
- GetOpenFileName() and the GetSaveFileName() creation options.
- This function will handle the messages for this dialog and create
- either a GetOpenFileName() dialog or a GetSaveFileName() dialog
- depending on the state of the bDoOpenDlg variable.
- Returns: TRUE or FALSE depending on the situation.
- Comments:
- ************************************************************************/
- BOOL APIENTRY OpenFunc(HWND hwnd, UINT msg, UINT wParam, LONG lParam)
- {
- switch (msg)
- {
- case WM_INITDIALOG:
- if (bDoOpenDlg)
- SetWindowText(hwnd, TEXT("GetOpenFileName()")) ;
- else
- SetWindowText(hwnd, TEXT("GetSaveFileName()")) ;
- /* initialize the OPENFILENAME structure members */
- InitOpenStruct(hwnd, &ofn) ;
- /* Fill these values into the creation dialog */
- FillOpenDlg(hwnd, &ofn) ;
- /* There are three separate OPENFILENAME structures. One for
- the main Open/Save dialog and one for each multithreaded dialog.
- Set them equal to begin with */
- *(&ofnThread1) = *(&ofnThread2) = *(&ofn) ;
- hwndMainDialog = hwnd ;
- SetFocus(GetDlgItem(hwnd, ID_STRUCTSIZEO)) ;
- break ;
- case UMSG_DECREMENTDLGCOUNT: //user defined message indicating
- //the closure of a multithreaded dialog
- /* When we are multithreading, there is nothing to prevent the
- user from interacting with the creation dialog once the first
- GetOpen(Save) file name dialog has returned. So, in order
- to prevent the Multithread" button from being pressed again
- before the previous two multithreaded dialogs have been canceled,
- disable the controls until we get a message from each thread
- that the dialog has ended */
- nOpenDialogCount-- ;
- if (nOpenDialogCount == 0)
- OpenMultiThreadEnableButtons(TRUE, hwnd) ;
- break ;
- case WM_COMMAND:
- {
- switch (LOWORD(wParam))
- {
- case IDOK:
- GetOpenDlg(hwnd, &ofn) ; //get the user's input
- DoOpenSaveStuff(&ofn) ; //do the dialog
- break ;
- case IDCANCEL:
- EndDialog(hwnd, FALSE) ;
- break ;
- case ID_RESETOPEN:
- SendDlgItemMessage(hwnd, ID_FILTERO, CB_RESETCONTENT,
- (WPARAM) 0, (LPARAM) 0) ;
- InitOpenStruct(hwnd, &ofn) ;
- FillOpenDlg(hwnd, &ofn) ;
- SendDlgItemMessage(hwnd, ID_NULLSTRUCTO, BM_SETCHECK, (WPARAM)0, (LPARAM)0) ;
- SendDlgItemMessage(hwnd, ID_USEHINSTO, BM_SETCHECK, (WPARAM)0, (LPARAM)0) ;
- *(&ofnThread1) = *(&ofnThread2) = *(&ofn) ;
- SetFocus(GetDlgItem(hwnd, ID_STRUCTSIZEO)) ;
- break ;
- case ID_ADD1O:
- GetDlgItemText(hwnd, ID_FILTERO, szTemp, 100) ;
- if (*szTemp)
- {
- SendDlgItemMessage(hwnd, ID_FILTERO, CB_ADDSTRING, (WPARAM) 0,
- (LPARAM) (LPTSTR) szTemp) ;
- SetWindowText(GetDlgItem(hwnd, ID_FILTERO), TEXT("")) ;
- }
- break ;
- case ID_ADD2O:
- GetDlgItemText(hwnd, ID_CUSTFILTO, szTemp, 100) ;
- if (*szTemp)
- {
- SendDlgItemMessage(hwnd, ID_CUSTFILTO, CB_ADDSTRING, (WPARAM) 0,
- (LPARAM) (LPTSTR) szTemp) ;
- SetWindowText(GetDlgItem(hwnd, ID_CUSTFILTO), TEXT("")) ;
- }
- break ;
- case ID_CLEAR1O:
- SendDlgItemMessage(hwnd, ID_FILTERO, CB_RESETCONTENT,
- (WPARAM) 0, (LPARAM) 0) ;
- break ;
- case ID_CLEAR2O:
- SendDlgItemMessage(hwnd, ID_CUSTFILTO, CB_RESETCONTENT,
- (WPARAM) 0, (LPARAM) 0) ;
- break ;
- case ID_MULTIOPEN:
- /* First, disable the OK, Cancel, and MultiThread buttons */
- OpenMultiThreadEnableButtons(FALSE, hwnd) ;
- /* Then multithread the dialogs */
- nOpenDialogCount = 2 ;
- MultiThreadOpenSave() ;
- break ;
- default: //end WM_COMMAND case
- break ;
- }
- }
- default:
- /* If the help button is pressed in the GetOpen/SaveFileName()
- dialogs, it will send a message Registered with RegisterWindowMessage()
- to the parent window. The message nHelpMessage was registered
- at application startup */
- if (msg == nHelpMessage)
- MessageBox(GetForegroundWindow(),
- TEXT("Hello from the help button"),
- TEXT("Open Help Button"), MB_OK | MB_APPLMODAL) ;
- break ;
- }
- return FALSE ;
- }
- /************************************************************************
- Function: InitOpenStruct(HWND, LPOPENFILENAME)
- Purpose:
- Initializes the OPENFILENAME structure. The structure is referenced
- via a pointer passed in as the second parameter so that we can pass
- any of the three OPENFILENAME structures into this function and
- Initialize them.
- Returns: Nothing.
- Comments:
- The szFilterInits and szCustFiltInits arrays are initialized to
- contain some default strings. Eventually the strings in
- these arrays must be arranged one after the other with a null
- character between them and two null characters at the end:
- "Text files *.txt All files *.* "
- ************************************************************************/
- void InitOpenStruct(HWND hwnd, LPOPENFILENAME po)
- {
- int i = 0 ;
- szFileName[0] = 0 ;
- szFileTitle[0] = 0 ;
- dwFlags = OFN_READONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_SHOWHELP ;
- if (bDoOpenDlg)
- lstrcpy(szDlgTitle, TEXT("Open Dialog Title")) ;
- else
- lstrcpy(szDlgTitle, TEXT("Save Dialog Title")) ;
- lstrcpy(szDefExt, TEXT("rat")) ;
- lstrcpy(szInitialDir, TEXT("c:\")) ;
- lstrcpy(szTempName, TEXT("opentemp1")) ;
- lstrcpy(&szFilterInits[0][0], TEXT("All Files (*.*)")) ;
- lstrcpy(&szFilterInits[1][0], TEXT("*.*")) ;
- lstrcpy(&szFilterInits[2][0], TEXT("Fat Files (*.fat)")) ;
- lstrcpy(&szFilterInits[3][0], TEXT("*.fat")) ;
- szFilterInits[4][0] = (TCHAR) 0 ;
- lstrcpy(&szCustFiltInits[0][0], TEXT("Last Filter Used")) ;
- lstrcpy(&szCustFiltInits[1][0], TEXT("*.lst")) ;
- szCustFiltInits[2][0] = (TCHAR) 0 ;
- /*
- These two functions will create "strings" in the applications
- data area that are in the form
- "Filter Description"