📄 cmndlg32.c
字号:
OpenFileName.lpstrDefExt = "*.txt";
OpenFileName.lCustData = 0;
switch( wMode )
{
case IDM_STANDARD:
OpenFileName.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
break;
case IDM_HOOK:
OpenFileName.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
OpenFileName.lpfnHook = (LPOFNHOOKPROC)FileOpenHookProc;
break;
case IDM_CUSTOM:
OpenFileName.Flags = OFN_SHOWHELP | OFN_ENABLEHOOK |
OFN_HIDEREADONLY | OFN_ENABLETEMPLATE;
OpenFileName.lpfnHook = (LPOFNHOOKPROC)FileOpenHookProc;
if (bNewShell)
{
OpenFileName.Flags |= OFN_EXPLORER;
OpenFileName.lpTemplateName = (LPTSTR)MAKEINTRESOURCE(IDD_OPENSAVE);
}
else
OpenFileName.lpTemplateName = (LPTSTR)MAKEINTRESOURCE(FILEOPENORD);
break;
}
if (GetOpenFileName(&OpenFileName))
{
// open the file
if ((hFile = CreateFile((LPCTSTR)OpenFileName.lpstrFile,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE)NULL)) == (HANDLE)-1)
{
MessageBox( hWnd, "File open failed.", NULL, MB_OK );
return FALSE;
}
// read it's contents into a buffer
ReadFile(hFile,(LPVOID)FileBuf, FILE_LEN, &dwBytesRead, NULL);
lpBufPtr = FileBuf;
if (dwBytesRead == 0)
{
MessageBox( hWnd, "Zero bytes read.", NULL, MB_OK );
return FALSE;
}
// close the file
CloseHandle(hFile);
dwFileSize = dwBytesRead / 2;
}
else
{
ProcessCDError(CommDlgExtendedError(), hWnd );
return FALSE;
}
return TRUE;
}
/****************************************************************************
*
* FUNCTION: SaveToFile( HWND )
*
* PURPOSE: Saves the current buffer to the current file.
*
* COMMENTS:
*
* This function will save the current text buffer into the file
* specified from the GetSaveFileName() common dialog function.
*
* RETURN VALUES:
* TRUE - The file was saved successfully.
* FALSE - The buffer was not saved to a file.
*
****************************************************************************/
BOOL SaveToFile( HWND hWnd )
{
HANDLE hFile;
DWORD dwOpen, dwBytesWritten;
TCHAR buf[256];
dwOpen = CREATE_ALWAYS;
// open the file
if ((hFile = CreateFile((LPCTSTR)OpenFileName.lpstrFile,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
dwOpen,
FILE_ATTRIBUTE_NORMAL,
(HANDLE)NULL)) == (HANDLE)-1)
{
sprintf( buf, "Could not create file %s", OpenFileName.lpstrFile );
MessageBox( hWnd, buf, NULL, MB_OK );
return FALSE;
}
// write it's contents into a file
if (WriteFile( hFile, (LPCVOID)FileBuf, dwFileSize*2,
&dwBytesWritten, NULL) == FALSE)
{
MessageBox( hWnd, "Error writing file.", NULL, MB_OK );
return FALSE;
}
// close the file
CloseHandle(hFile);
return TRUE;
}
/****************************************************************************
*
* FUNCTION: FileSaveHookProc(HWND, UINT, UINT, LONG)
*
* PURPOSE: Processes messages for FileSave common dialog box
*
* COMMENTS:
*
* This hook procedure prompts the user if they want to save the
* current file. If they choose YES, the file is saved and the dialog
* is dismissed. If they choose NO, they are returned to the
* GetSaveFileName() common dialog.
*
* If the current mode calls for a customized template, this function
* will test the 'Create File?' checkbox. If the user choses no, the
* OFN_FILEMUSTEXIST flag is set.
*
* RETURN VALUES:
* TRUE - User chose 'Yes' from the "Are you sure message box".
* FALSE - User chose 'No'; return to the dialog box.
*
*
****************************************************************************/
BOOL APIENTRY FileSaveHookProc(
HWND hDlg, /* window handle of the dialog box */
UINT message, /* type of message */
UINT wParam, /* message-specific information */
LONG lParam)
{
TCHAR szTempText[256];
TCHAR szString[256];
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
GetDlgItemText( hDlg, edt1, szTempText,
sizeof( szTempText ) - 1);
if ( OpenFileName.Flags & OFN_ENABLETEMPLATE )
{
// check to see if the Create File box has been checked
if ( (BOOL)(SendMessage( GetDlgItem(hDlg, chx2),
BM_GETCHECK, 0, 0L )) == FALSE )
OpenFileName.Flags |= OFN_FILEMUSTEXIST;
break;
}
else
{
sprintf( szString, "Are you sure you want to save %s?",
szTempText);
if ( MessageBox( hDlg, szString, "Information",
MB_YESNO ) == IDYES )
break;
return(TRUE);
}
}
break;
}
return (FALSE);
// avoid compiler warnings at W3
lParam;
}
/****************************************************************************
*
* FUNCTION: SaveAs(HWND)
*
* PURPOSE: Invokes the common dialog function to save the current
* buffer to a file.
* COMMENTS:
*
* This function initializes the OPENFILENAME structure for any
* mode selected by the user: standard, using a hook or using a
* customized template. It then calls the GetSaveFileName()
* common dialog function.
*
* RETURN VALUES:
* TRUE - The file was saved successfully.
* FALSE - The buffer was not saved to a file.
*
****************************************************************************/
BOOL SaveAs( HWND hWnd )
{
strcpy( szFile, "");
strcpy( szFileTitle, "");
OpenFileName.lStructSize = sizeof(OPENFILENAME);
OpenFileName.hwndOwner = hWnd;
OpenFileName.hInstance = (HANDLE) hInst;
OpenFileName.lpstrFilter = szFilter;
OpenFileName.lpstrCustomFilter = (LPTSTR) NULL;
OpenFileName.nMaxCustFilter = 0L;
OpenFileName.nFilterIndex = 1L;
OpenFileName.lpstrFile = szFile;
OpenFileName.nMaxFile = sizeof(szFile);
OpenFileName.lpstrFileTitle = szFileTitle;
OpenFileName.nMaxFileTitle = sizeof(szFileTitle);
OpenFileName.lpstrInitialDir = NULL;
OpenFileName.lpstrTitle = "Save File As";
OpenFileName.nFileOffset = 0;
OpenFileName.nFileExtension = 0;
OpenFileName.lpstrDefExt = "txt";
OpenFileName.lCustData = 0;
switch( wMode )
{
case IDM_STANDARD:
OpenFileName.Flags = 0L;
OpenFileName.lpfnHook = (LPOFNHOOKPROC)(FARPROC)NULL;
OpenFileName.lpTemplateName = (LPTSTR)NULL;
break;
case IDM_HOOK:
OpenFileName.Flags = OFN_ENABLEHOOK;
OpenFileName.lpfnHook = (LPOFNHOOKPROC)FileSaveHookProc;
OpenFileName.lpTemplateName = (LPTSTR)NULL;
break;
case IDM_CUSTOM:
OpenFileName.Flags = OFN_ENABLEHOOK | OFN_ENABLETEMPLATE;
OpenFileName.lpfnHook = (LPOFNHOOKPROC)FileSaveHookProc;
if (bNewShell)
{
OpenFileName.Flags |= OFN_EXPLORER;
OpenFileName.lpTemplateName = (LPTSTR)MAKEINTRESOURCE(IDD_OPENSAVE);
}
else
OpenFileName.lpTemplateName = (LPTSTR)MAKEINTRESOURCE(FILEOPENORD);
break;
}
if ( GetSaveFileName( &OpenFileName ))
return(SaveToFile( hWnd ));
else
{
ProcessCDError(CommDlgExtendedError(), hWnd );
return FALSE;
}
return (FALSE);
}
/****************************************************************************
*
* FUNCTION: ChooseColorHookProc(HWND, UINT, UINT, LONG)
*
* PURPOSE: Processes messages for ChooseColor common dialog box
*
* COMMENTS:
*
* This hook procedure simply prompts the user whether or not they
* want to change the color. if they choose YES, the color of the
* text will be changed and the dialog will be dismissed. If they
* choose NO, the color will not be changed and the user will be
* returned to the dialog
*
* RETURN VALUES:
* TRUE - User chose 'Yes' from the "Are you sure message box".
* FALSE - User chose 'No'; return to the dialog box.
*
****************************************************************************/
BOOL APIENTRY ChooseColorHookProc(
HWND hDlg, /* window handle of the dialog box */
UINT message, /* type of message */
UINT wParam, /* message-specific information */
LONG lParam)
{
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
if (MessageBox( hDlg, "Are you sure you want to change the color?",
"Information", MB_YESNO ) == IDYES )
break;
return (TRUE);
}
break;
}
return (FALSE);
// avoid compiler warnings at W3
lParam;
}
/****************************************************************************
*
* FUNCTION: ChooseNewColor(HWND)
*
* PURPOSE: Invokes common dialog function to chose a new color.
*
* COMMENTS:
* This function initializes the CHOOSECOLOR structure for any
* mode the user chooses: standard, using a hook or using a
* customized template. It then calls the ChooseColor()
* common dialog function.
*
* RETURN VALUES:
* TRUE - A new color was chosen.
* FALSE - No new color was chosen.
*
****************************************************************************/
BOOL ChooseNewColor( HWND hWnd )
{
DWORD dwColor;
DWORD dwCustClrs[16];
BOOL fSetColor = FALSE;
int i;
for (i=0; i < 15; i++)
dwCustClrs[i] = RGB( 255, 255, 255);
dwColor = RGB( 0, 0, 0 );
chsclr.lStructSize = sizeof(CHOOSECOLOR);
chsclr.hwndOwner = hWnd;
chsclr.hInstance = (HANDLE)hInst;
chsclr.rgbResult = dwColor;
chsclr.lpCustColors = (LPDWORD)dwCustClrs;
chsclr.lCustData = 0L;
switch( wMode )
{
case IDM_HOOK:
chsclr.Flags = CC_PREVENTFULLOPEN | CC_ENABLEHOOK;
chsclr.lpfnHook = (LPCCHOOKPROC)ChooseColorHookProc;
chsclr.lpTemplateName = (LPTSTR)NULL;
break;
case IDM_CUSTOM:
chsclr.Flags = CC_PREVENTFULLOPEN | CC_ENABLEHOOK | CC_ENABLETEMPLATE;
chsclr.lpfnHook = (LPCCHOOKPROC)ChooseColorHookProc;
chsclr.lpTemplateName = "ChooseColor";
break;
case IDM_STANDARD:
chsclr.Flags = CC_PREVENTFULLOPEN;
chsclr.lpfnHook = (LPCCHOOKPROC)(FARPROC)NULL;
chsclr.lpTemplateName = (LPTSTR)NULL;
break;
}
if ( fSetColor = ChooseColor( &chsclr ))
{
crColor = chsclr.rgbResult;
return (TRUE);
}
else
{
ProcessCDError(CommDlgExtendedError(), hWnd );
return FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -