📄 about.c
字号:
#include "StdSDK.h"
#include "about.h"
#include "resource.h"
// Function prototypes for About dialog box
static BOOL CALLBACK
aboutDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) ;
// Function prototypes for message handlers
static BOOL aboutDlg_OnContextMenu (HWND hwnd, HWND hwndCtl, int xPos,
int yPos) ;
static void aboutDlg_OnHelp (HWND hwnd, LPHELPINFO lphi) ;
static BOOL aboutDlg_OnInitDialog (HWND hwnd, HWND hwndFocus, LPARAM lParam) ;
static void aboutDlg_OnCommand (HWND hwnd, int id, HWND hwndCtl,
UINT codeNotify) ;
static int getNTVersion () ;
// Function prototypes for static functions
static void displayExecutableVersionInfo (HWND hwnd) ;
static void displayOperatingSystemVersionInfo (HWND hwnd) ;
static void displayProcessorVersionInfo (HWND hwnd) ;
static DWORD formatMessageFromString (LPCTSTR Format, LPTSTR Buffer,
DWORD nSize, ...) ;
typedef enum { typeDefault = 0,
typeAdvancedServer = 1,
typeWorkstation = 2,
typeServer = 3 } NTTYPE ;
//
// BOOL CALLBACK
// aboutDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
//
// PURPOSE:
// Dialog function for the About dialog box.
// Display version information for the application,
// the operating system, and the processor.
//
// MESSAGES:
//
//
//
static BOOL CALLBACK
aboutDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_CONTEXTMENU: // User clicked the right mouse button
return HANDLE_WM_CONTEXTMENU (hwnd, wParam, lParam, aboutDlg_OnContextMenu) ;
case WM_HELP: // User pressed the F1 key
return HANDLE_WM_HELP (hwnd, wParam, lParam, aboutDlg_OnHelp) ;
case WM_INITDIALOG: // Initialization of controls complete
return HANDLE_WM_INITDIALOG (hwnd, wParam, lParam, aboutDlg_OnInitDialog) ;
case WM_COMMAND: // Notification from a control
return HANDLE_WM_COMMAND (hwnd, wParam, lParam, aboutDlg_OnCommand) ;
}
return FALSE ;
}
// Array of control identifiers and help context identifier pairs.
#define IDH_ABOUT_FILEDESCRIPTION IDC_ABOUT_FILEDESCRIPTION
#define IDH_ABOUT_VERSION IDC_ABOUT_VERSION
#define IDH_ABOUT_LEGALCOPYRIGHT IDC_ABOUT_LEGALCOPYRIGHT
#define IDH_ABOUT_COMMENTS IDC_ABOUT_COMMENTS
#define IDH_ABOUT_OSVERSION IDC_ABOUT_OSVERSION
#define IDH_ABOUT_PROCESSORVERSION IDC_ABOUT_PROCESSORVERSION
#define IDH_ABOUT_LEGALTRADEMARKS IDC_ABOUT_LEGALTRADEMARKS
static DWORD aContextIds [] = {
IDC_ABOUT_FILEDESCRIPTION, IDH_ABOUT_FILEDESCRIPTION,
IDC_ABOUT_VERSION, IDH_ABOUT_VERSION,
IDC_ABOUT_LEGALCOPYRIGHT, IDH_ABOUT_LEGALCOPYRIGHT,
IDC_ABOUT_COMMENTS, IDH_ABOUT_COMMENTS,
IDC_ABOUT_OSVERSION, IDH_ABOUT_OSVERSION,
IDC_ABOUT_PROCESSORVERSION, IDH_ABOUT_PROCESSORVERSION,
IDC_ABOUT_LEGALTRADEMARKS, IDH_ABOUT_LEGALTRADEMARKS,
0, 0
} ;
//
// void aboutDlg_OnContextMenu (HWND hwnd, HWND hwndCtl. int xPos, int yPos)
//
// hwnd Handle of window to which this message applies
// hwndCtl Handle of the window in which the user right clicked the
// mouse
// xPos Horizontal position of the cursor, in screen coordinates
// yPos Vertical position of the cursor, in screen coordinates
//
// PURPOSE: Notification that the user clicked the right
// mouse button in the window.
//
// COMMENTS: Normally a window processes this message by
// displaying a context menu using the TrackPopupMenu
// or TrackPopupMenuEx functions.
//
// However it's convenient to allow the user to right
// click on a control in a dialog box to display a popup
// help topic about the control. This function implements
// the latter technique.
//
static BOOL
aboutDlg_OnContextMenu (HWND hwnd, HWND hwndCtl, int xPos, int yPos)
{
// Display popup help for the control
WinHelp (hwndCtl, getHelpFileName (), HELP_CONTEXTMENU,
(DWORD) (LPVOID) aContextIds) ;
return TRUE ;
}
//
// void aboutDlg_OnHelp (HWND hwnd, LPHELPINFO lphi)
//
// PURPOSE: Notification that the user pressed the F1 key
//
// COMMENTS: Normally a window processes this message by
// displaying a context menu using the TrackPopupMenu
// or TrackPopupMenuEx functions. If a window does not
// display a context menu it should pass this message
// to the DefWindowProc function.
//
static void
aboutDlg_OnHelp (HWND hwnd, LPHELPINFO lphi)
{
ASSERT (HELPINFO_WINDOW == lphi->iContextType) ;
if (HELPINFO_WINDOW == lphi->iContextType) { // Must be for a control!
int nID ;
int nIndex ;
// Get the control's window handle
HWND hwndCtl = lphi->hItemHandle ;
ASSERT (NULL != hwndCtl) ;
ASSERT (IsWindow (hwndCtl)) ;
// Get this control's ID
nID = GetWindowID (hwndCtl) ;
// Don't bother running WinHelp unless we have help for the control
for (nIndex = 0 ; nIndex < DIM (aContextIds) - 2; nIndex += 2) {
if (aContextIds [nIndex] == (DWORD) nID) {
WinHelp (hwndCtl, getHelpFileName (),
HELP_WM_HELP, (DWORD) (LPVOID) aContextIds) ;
return ;
}
}
}
}
//
// void aboutDlg_OnInitDialog (HWND hwnd, HWND hwndFocus, LPARAM lParam)
//
// hwnd Window handle for the dialog box window
// hwndFocus Handle of the control which will get the initial focus
// lParam Parameter to DialogBoxParam (not used for About dialog)
//
// PURPOSE:
// Initializes all the controls by extracting information
// from the VERSIONINFO structure
//
static BOOL
aboutDlg_OnInitDialog (HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
// Center the dialog window
centerWindow (hwnd, NULL) ;
// Update controls with application version info
displayExecutableVersionInfo (hwnd) ;
// Update controls with operating system version info
displayOperatingSystemVersionInfo (hwnd) ;
// Update controls with processor version info
displayProcessorVersionInfo (hwnd) ;
return TRUE ;
}
//
// 040904B0 means US English, Unicode code page
// 040904E4 means US English, Windows MultiLingual code page
static const TCHAR ValueNameBase [] = TEXT("\\StringFileInfo\\040904B0\\") ;
static const TCHAR ProductName [] = TEXT("ProductName") ;
// Number of characters in the base portion of the value name string
#define BASECHARS (DIM(ValueNameBase) - 1)
//
// void displayExecutableVersionInfo (HWND hwnd)
//
// hwnd Window handle for the dialog box window
//
// PURPOSE: Retrieves the application's version resource
// infomation and displays the info in the
// appropriate dialog box controls.
//
// COMMENTS: This implementation uses the initial control
// window text as a version info value key. This
// function displays the value retrieved for a
// key in the respective control.
//
static void
displayExecutableVersionInfo (HWND hwnd)
{
BOOL Result ; // Result of Boolean functions
DWORD dwVerInfoSize ; // Size of version information
DWORD dwHandle ; // Extraneous but required
HMODULE hmod ; // Application's module handle
HWND hwndControl ; // Control window handle
LPVOID pVerInfo ; // File version info pointer
LPVOID pValue ; // Value from version info
TCHAR FullPath [_MAX_PATH] ; // Application executable path
TCHAR ValueName [256] ; // Name of value to retrieve
UINT uLength ; // Length of retrieved value
// Get the full path to this executable file
hmod = GetModuleHandle (NULL) ;
GetModuleFileName (hmod, FullPath, DIM(FullPath)) ;
// Determine the size buffer needed to store the version information:
dwVerInfoSize = GetFileVersionInfoSize (FullPath, &dwHandle) ;
if (0 == dwVerInfoSize)
return ;
// Allocate a buffer for the version info block
pVerInfo = malloc (dwVerInfoSize) ;
ASSERT (NULL != pVerInfo) ;
if (NULL == pVerInfo)
return ;
// Read the version info block into the buffer
VERIFY(GetFileVersionInfo (FullPath, dwHandle, dwVerInfoSize, pVerInfo)) ;
// Build value name base string...
_tcscpy (ValueName, ValueNameBase) ;
// Build the \StringFileInfo\040904E4\ProductName value name
_tcscpy (ValueName + BASECHARS, ProductName) ;
// Retrieve the value
Result = VerQueryValue (pVerInfo, ValueName, &pValue, &uLength) ;
ASSERT (Result) ;
// Format the output for the dialog caption
// Get the current caption then append to it the ProductName value
GetWindowText (hwnd, ValueName, DIM (ValueName)) ;
_tcscat (ValueName, pValue) ;
// Change the dialog caption - normally "About <ProductName>"
SetWindowText (hwnd, ValueName) ;
// For each control in the dialog...
// fetch the version info name from the control's initial window text.
// retrieve the value with that name,
// change the control's window text to the retrieved value.
// Technique derived from GENERIC.C.
hwndControl = GetFirstChild (hwnd) ;
while (NULL != hwndControl) {
// Build value name base string...
_tcscpy (ValueName, ValueNameBase) ;
// Build the \StringFileInfo\040904B0\<ControlText> value name
// The Win32 API contains the following predefined version information
// strings:
// CompanyName LegalCopyright
// FileDescription OriginalFilename
// FileVersion ProductName
// InternalName ProductVersion
// Get the control's text...
GetWindowText (hwndControl,
ValueName + BASECHARS,
DIM(ValueName) - BASECHARS) ;
// Retrieve the value
Result = VerQueryValue (pVerInfo, ValueName, &pValue, &uLength) ;
// If version information is available and the version information
// name exists...
if (Result)
// If a value exists for the version information name...
if (0 != uLength && NULL != pValue)
// Change the control's text to the version information value
SetWindowText (hwndControl, pValue) ;
hwndControl = GetNextSibling (hwndControl) ;
}
// Free the memory for the version information block
free (pVerInfo) ;
}
//
// DWORD displayOperatingSystemVersionInfo (HWND hwnd)
//
// hwnd Window handle for the dialog box window
//
// PURPOSE: Displays the operating system's version
//
// COMMENTS:
//
static void
displayOperatingSystemVersionInfo (HWND hwnd)
{
BOOL Result ;
HINSTANCE hinst ;
NTTYPE NtOsType ;
TCHAR OSVer [256] ;
TCHAR FormatString [256] ;
// Get OS version information
OSVERSIONINFO osver ;
osver.dwOSVersionInfoSize = sizeof (osver) ;// Must initialize size member!
Result = GetVersionEx (&osver) ; // Retrieve version info
ASSERT (FALSE != Result) ;
if (FALSE == Result)
return ;
hinst = GetWindowInstance (hwnd) ; // Get instance for LoadString
switch (osver.dwPlatformId) {
case VER_PLATFORM_WIN32_NT: // Windows NT
NtOsType = getNTVersion () ;
LoadString (hinst, IDS_PLATFORM_WIN32_NT + NtOsType,
FormatString, DIM (FormatString)) ;
break ;
case VER_PLATFORM_WIN32s: // Win32s on Windows 3.1
LoadString (hinst, IDS_PLATFORM_WIN32s,
FormatString, DIM (FormatString)) ;
break ;
case VER_PLATFORM_WIN32_WINDOWS: // Windows 95
LoadString (hinst, IDS_PLATFORM_WIN32_WINDOWS,
FormatString, DIM (FormatString)) ;
// Windows 95 encodes extra info in HIWORD(dwBuildNumber)
// Remove unwanted junk
osver.dwBuildNumber = LOWORD (osver.dwBuildNumber) ;
break ;
default: // Unknown operating system
LoadString (hinst, IDS_PLATFORM_UNKNOWN,
FormatString, DIM (FormatString)) ;
break ;
}
wsprintf (OSVer, FormatString,
osver.dwMajorVersion,
osver.dwMinorVersion,
osver.dwBuildNumber) ;
SetDlgItemText (hwnd, IDC_ABOUT_OSVERSION, OSVer) ;
}
//
// void displayProcessorVersionInfo (HWND hwnd, DWORD dwPlatformId)
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -