📄 smartmon.c
字号:
/**************************************************************************/
/*** ***/
/*** SMARTMON.C - SmartDrv 4.0 monitor application main module ***/
/*** ***/
/*** ***/
/*** ***/
/*** ***/
/**************************************************************************/
/**************************************************************************/
/*** ***/
/*** Global variable declarations and initializations ***/
/*** ***/
/**************************************************************************/
#include <windows.h>
#include <shellapi.h>
#include "smartmon.h"
HANDLE hInst; // application instance handle
HWND hWndMain; // application's main window
char szBuffer[BUFLEN]; // buffer for stringtable stuff
char szTitle[20]; // from resource
DWORD DosCacheSize; // Cache size (Kbyte) under DOS
DWORD WinCacheSize; // Cache size (Kbyte) under Windows
WORD wWinVer; // Windows version
BOOL fIconic; // flag to indicate if we're iconized
RECT rcIconic; // window size while iconic
BOOL fUpdateIconBackground;
BOOL fAlreadyIdle; // cache idle during previous cycle
char szStartLog[20]; // "Start Log" button label
char szStopLog[20]; // "Stop Log" button label
BOOL fLogging; // logging status
HICON hIcoDrag; // icon for dragging
HBITMAP hbmpIdle; // idle drive bitmap for icon
HBITMAP hbmpBusy; // busy drive bitmap for icon
HCURSOR hCurWait; // wait cursor
DWORD LogStartTime; // logging start time
DWORD LogStopTime; // logging auto-stop time
BOOL fUpdateAuto; // change autoexec.bat or not
BOOL fDriveInfoChanged; // drive cache setting changed
BOOL fProfileChanged; // profile modified since start
BOOL fTopMost; // run SmartMon as topmost window
BOOL fTopMostChanged; // save topmost info
char szBatchFile[MAXFILENAMELEN];
HMENU hSysMenu;
char *szProSec = "SmartMon"; // profile section in WIN.INI
char *szInterval = "Interval"; // profile key
char *szFreq = "Frequency"; // profile key
char *szfStop = "AutoStop"; // profile key
char *szLogTime = "LogTime"; // profile key
char *szUpdateDOS = "UpdateBatch"; // profile key
char *szLog = "LogFile"; // profile key
char *szBatch = "BatchFile"; // profile key
char *szTopMost = "TopMost"; // profile key
extern HBITMAP hbmpIconDrv;// either hbmpIdle or hbmpBusy
extern WORD Frequency; // sampling frequency
extern HFONT hfontStatus; // font used for text of status bar
extern char szLogFile[]; // log file name
extern WORD AutoLogTime;
extern BOOL fAutoStop;
extern WORD SpecInterval;
extern DWORD BaseHits;
extern DWORD BaseTotal;
extern WORD AveHitRate;
long FAR PASCAL WambiWndProc( HWND, unsigned, WORD, LONG );
BOOL FAR PASCAL OptionsDlgProc( HWND, WORD, WPARAM, LPARAM );
void DoDriveControl( HWND, WORD, WORD );
void UpdateMemorySize( HWND );
void DoHelp( HWND, UINT, DWORD );
void ForceIconRedraw( HWND );
void CheckAutoStop( HWND );
void GetProfileSettings( void );
void SaveProfileSettings( void );
BOOL CheckForStacker( WORD );
void DoTopMost( HWND );
extern void MeasureItem( HANDLE, LPMEASUREITEMSTRUCT );
extern BOOL wNoRedraw;
extern void DrawItem( HWND, WPARAM, LPDRAWITEMSTRUCT, BOOL );
extern void DrawRateBox( HWND, int );
extern void InitRateBox( HWND );
extern void ForceRateBoxRedraw( HWND );
extern void InitStatusBar( HWND );
extern void UpdateStatusBar( HWND, HDC );
extern int GetDriveIndex( WORD, WORD );
extern void DrawIconBackground( HDC );
extern void UpdateRateIcon( HWND, HDC );
extern BOOL GetHitRate( void );
extern BOOL StartLog( void );
extern void StopLog( void );
extern BOOL WriteLog( void );
extern void UpdateHelpMessage( int );
extern void CleanupRate( void );
extern void ResetRateChart( HWND );
extern void CheckDriveList( HWND );
extern void ResetDriveBox( HWND );
extern void InitDriveBitmap( void );
extern BOOL MungeBatchFile( void );
extern void ResetRateChartColor( BOOL );
/**************************************************************************/
/*** ***/
/*** WinMain ***/
/*** ***/
/**************************************************************************/
int NEAR PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, nCmdShow )
HANDLE hInstance; // handle for this instance
HANDLE hPrevInstance; // handle for possible previous instances
LPSTR lpszCmdLine; // long pointer to exec command line
int nCmdShow; // Show code for main window display
{
MSG msg;
WNDCLASS wndclass;
FARPROC lpfnWndProc;
//
// Initialize state information.
//
hInst = hInstance;
if ( !WambiInit( hPrevInstance ) )
return FALSE;
//
// Register a window class.
//
if(!hPrevInstance) {
wndclass.style = NULL;
wndclass.lpfnWndProc = DefDlgProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = DLGWINDOWEXTRA;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Wambi";
if (!RegisterClass(&wndclass) )
return FALSE;
} else {
return FALSE; // Allow only one instance.
}
//
// Create and show main dialog window.
//
lpfnWndProc = MakeProcInstance( (FARPROC)WambiWndProc, hInst );
hWndMain = CreateDialog( hInst, "main", 0, lpfnWndProc );
SetWindowPos( hWndMain, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW |
(fTopMost ? 0 : SWP_NOZORDER) );
fIconic = nCmdShow == SW_SHOWMINIMIZED;
ShowWindow( hWndMain, nCmdShow );
//
// Main message loop
//
while ( GetMessage( &msg, NULL, 0, 0 ) ) {
if ( !IsDialogMessage( hWndMain, &msg ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
UnregisterClass( szTitle, hInst );
return msg.wParam;
}
/**************************************************************************/
/*** ***/
/*** WambiWndProc ***/
/*** ***/
/**************************************************************************/
long FAR PASCAL WambiWndProc( hWnd, Message, wParam, lParam )
HWND hWnd;
unsigned Message;
WORD wParam;
LONG lParam;
{
extern HBRUSH hbrBkgnd;
extern HBRUSH hbrFrame;
extern RECT rcStatusLine; // Bounding rect for status bar.
HDC hDC;
PAINTSTRUCT ps;
FARPROC lpfnOptionWndProc;
switch (Message)
{
case WM_INITDIALOG:
EnableWindow( GetDlgItem(hWnd, IDD_STOPLOG), FALSE );
wsprintf( szBuffer, "%u msec", Frequency );
SetDlgItemText( hWnd, IDD_SAMPFREQ, szBuffer );
InitDriveBox( hWnd );
InitRateBox( hWnd );
InitStatusBar( hWnd );
//
// Insert "About" into system menu. "About SmartMon" would
// stretch the system menu box too wide. Also insert
// "Always on Top".
//
hSysMenu = GetSystemMenu( hWnd, FALSE );
AppendMenu( hSysMenu, MF_SEPARATOR, 0, (LPSTR)NULL );
AppendMenu( hSysMenu, MF_STRING, IDD_ABOUT, "&About...");
AppendMenu( hSysMenu, MF_STRING, IDD_TOPMOST, "Always on &Top" );
CheckMenuItem( hSysMenu, IDD_TOPMOST,
fTopMost ? MF_CHECKED : MF_UNCHECKED );
SetTimer( hWnd, 0, Frequency, NULL );
break;
case WM_TIMER:
if ( GetHitRate() ) {
hbmpIconDrv = hbmpBusy;
ForceRateBoxRedraw( hWnd );
if ( fLogging ) {
WriteLog();
UpdateHelpMessage( IDS_LOGGING );
} else
UpdateHelpMessage( IDS_CACHEACTIVE );
if ( fIconic ) {
InvalidateRect( hWnd, (LPRECT)NULL, FALSE );
}
fAlreadyIdle = FALSE;
} else {
UpdateHelpMessage( IDS_CACHEIDLE );
hbmpIconDrv = hbmpIdle;
if ( fIconic )
if ( !fAlreadyIdle ) {
InvalidateRect( hWnd, (LPRECT)NULL, FALSE );
fAlreadyIdle = TRUE;
}
}
CheckAutoStop( hWnd );
break;
case WM_ACTIVATE:
//
// We never know when a network share could be disconnected,
// or a CD-ROM dismounted. So be safe, reset all the drives.
//
if ( wParam != WA_INACTIVE )
ResetDriveBox( hWnd );
break;
case WM_MOVE:
if ( fIconic )
fUpdateIconBackground = TRUE;
break;
case WM_SIZE:
if ( (wParam == SIZE_MINIMIZED) ) {
if ( !fIconic ) {
fIconic = fUpdateIconBackground = TRUE;
SetRect((LPRECT)&rcIconic, 0, 0, LOWORD(lParam), HIWORD(lParam));
}
} else
fIconic = FALSE;
break;
case WM_PAINT:
hDC = BeginPaint( hWnd, &ps );
if ( fIconic ) {
if ( fUpdateIconBackground ) {
fUpdateIconBackground = FALSE;
DrawIconBackground( hDC );
} else
ValidateRect( hWnd, (LPRECT)NULL );
UpdateRateIcon( hWnd, hDC );
} else {
UpdateMemorySize( hWnd );
UpdateStatusBar( hWnd, hDC );
}
EndPaint( hWnd, &ps );
return TRUE;
case WM_MEASUREITEM:
MeasureItem( hWnd, (LPMEASUREITEMSTRUCT)lParam );
return TRUE;
case WM_DRAWITEM:
DrawItem( hWnd, wParam, (LPDRAWITEMSTRUCT)lParam, FALSE );
return TRUE;
case WM_SYSCOMMAND:
if ( wParam == IDD_ABOUT )
ShellAbout( hWnd, szTitle, NULL, hIcoDrag );
else if ( wParam == IDD_TOPMOST )
DoTopMost( hWnd );
break;
case WM_KEYDOWN:
if ( wParam == VK_F1 ) {
PostMessage( hWnd, WM_COMMAND, IDD_HELP, 0L );
return TRUE;
}
break;
case WM_COMMAND:
switch( wParam )
{
case IDCANCEL:
SendDlgItemMessage( hWnd, IDD_DRIVEID, CB_SHOWDROPDOWN, FALSE, 0 );
break;
case IDD_READONLY:
case IDD_READWRITE:
case IDD_NOCACHING:
DoDriveControl( hWnd, SET, wParam );
break;
case IDD_FLUSH:
commit_cache();
UpdateHelpMessage( IDS_FLUSHED );
break;
case IDD_RESET:
reset_cache();
//
// Reset lifetime average hit rate baseline
//
BaseHits = get_cache_hits();
BaseTotal = get_cache_misses() + BaseHits;
AveHitRate = 0;
UpdateHelpMessage( IDS_RESET );
break;
case IDD_DRIVEID:
if ( HIWORD(lParam) == CBN_SELCHANGE )
DoDriveControl( hWnd, GET, 0 );
break;
case IDD_HELP:
DoHelp( hWnd, HELP_INDEX, 0L );
break;
case IDD_STARTLOG:
if ( StartLog() ) {
EnableWindow( GetDlgItem(hWnd, IDD_STARTLOG), FALSE );
EnableWindow( GetDlgItem(hWnd, IDD_STOPLOG), TRUE );
fLogging = TRUE;
UpdateHelpMessage( IDS_LOGSTARTED );
LogStartTime = GetTickCount();
if ( fAutoStop )
LogStopTime = LogStartTime + AutoLogTime * 60000;
} else
MessageBox( hWnd, (LPSTR)"Can not start logging.",
(LPSTR)szTitle, MB_ICONINFORMATION );
break;
case IDD_STOPLOG:
StopLog();
EnableWindow( GetDlgItem(hWnd, IDD_STOPLOG), FALSE );
EnableWindow( GetDlgItem(hWnd, IDD_STARTLOG), TRUE );
fLogging = FALSE;
UpdateHelpMessage( IDS_LOGSTOPPED );
break;
case IDD_OPTION:
lpfnOptionWndProc = MakeProcInstance( OptionsDlgProc, hInst );
DialogBox( hInst, "options", hWndMain, lpfnOptionWndProc );
FreeProcInstance( lpfnOptionWndProc );
break;
}
return TRUE;
case WM_CLOSE:
if ( fLogging )
StopLog();
if ( fTopMostChanged ) {
wsprintf( szBuffer, "%u", fTopMost );
WriteProfileString( szProSec, szTopMost, szBuffer );
}
if ( fDriveInfoChanged && fUpdateAuto ) {
if ( !MungeBatchFile() ) {
LoadString( hInst, IDS_SAVEAUTO, szBuffer, BUFLEN );
if ( MessageBox( hWnd, (LPSTR)szBuffer, (LPSTR)szTitle,
MB_ICONINFORMATION | MB_OKCANCEL ) == IDCANCEL )
return TRUE;
}
}
CleanupRate();
DestroyIcon( hIcoDrag );
DeleteObject( hbmpIdle );
DeleteObject( hbmpBusy );
if ( hfontStatus )
DeleteObject( hfontStatus );
DoHelp( hWnd, HELP_QUIT, 0L );
DestroyWindow( hWnd );
return TRUE;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SYSCOLORCHANGE:
ResetRateChartColor( FALSE );
ResetRateChart( GetDlgItem( hWndMain, IDD_CHARTBOX ) );
ResetDriveBox( hWnd );
break;
case WM_QUERYDRAGICON:
return hIcoDrag;
}
return FALSE;
}
/**************************************************************************/
/*** ***/
/*** WambiInit ***/
/*** ***/
/**************************************************************************/
BOOL WambiInit( HANDLE hPrevInstance )
{
extern char CurDriveList[];
extern WORD CurDriveCount;
WORD BlockSize, DosBlocks, WinBlocks;
char cBootDrv;
int i, iDrv, iDrvIndex;
//
// Load basic string resources.
//
LoadString( hInst, IDS_TITLE, szTitle, 20 );
LoadString( hInst, IDS_STARTLOG, szStartLog, 20 );
LoadString( hInst, IDS_STOPLOG, szStopLog, 20 );
LoadString( hInst, IDS_LOGFILE, szLogFile, MAXFILENAMELEN );
hIcoDrag = LoadIcon( hInst, MAKEINTRESOURCE(1) );
hbmpIdle = LoadBitmap( hInst, MAKEINTRESOURCE(2) );
hbmpBusy = LoadBitmap( hInst, MAKEINTRESOURCE(3) );
hCurWait = LoadCursor( NULL, IDC_WAIT );
//
// Allow single instance to run.
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -