home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windoware
/
WINDOWARE_1_6.iso
/
source
/
resgauge
/
resgauge.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-04
|
8KB
|
283 lines
// =========================================================================
// resgauge.c -- a gauge of free system resources.
// =========================================================================
#include <windows.h>
#include "resgauge.h"
// -----------------
// manifest constant
// -----------------
#define TIMER_ID 1
// -------------------
// function prototypes
// -------------------
LONG FAR PASCAL WndProc(HWND, WORD, WORD, LONG);
BOOL FAR PASCAL About(HWND, WORD, WORD, LONG);
WORD NEAR PASCAL GetSystemResources(void);
// an undocumented Windows function ... one of many!
DWORD FAR PASCAL GetHeapSpaces(HANDLE);
// ------
// global
// ------
HANDLE hInst;
// =========================================================================
// > > > > > > > > > > > > > > > code begins < < < < < < < < < < < < < < <
// =========================================================================
// The "main()" for Windows: calls the initialization functions and enters
// the message processing loop. Returns the wParam element of the MSG when
// a WM_QUIT message has been processed by the window function.
// -------------------------------------------------------------------------
short
PASCAL
WinMain(
HANDLE hInstance, // current instance
HANDLE hPrevInstance, // previous instance
LPSTR lpszCmdLine, // command line
short nCmdShow) // window state (usually open or iconic)
{
static char szAppName[] = "Resource Gauge v1.0";
static char szClassName[] = "ResourceGauge";
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = NULL;
wndclass.lpszMenuName = szClassName;
wndclass.lpszClassName = szClassName;
RegisterClass(&wndclass);
}
else
{
return(FALSE);
}
hInst = hInstance;
hWnd = CreateWindow(szClassName, szAppName, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (!SetTimer(hWnd, TIMER_ID, 1000, NULL))
{
MessageBox(hWnd, "Too many timers in use!", szAppName,
MB_ICONEXCLAMATION | MB_OK);
return(FALSE);
}
ShowWindow(hWnd, SW_SHOWMINNOACTIVE);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
} // WinMain
// -------------------------------------------------------------------------
// Processes messages for the main window.
// -------------------------------------------------------------------------
LONG
FAR
PASCAL
WndProc(
HWND hWnd, // window handle
WORD wMessage, // type of message
WORD wParam, // 16 bits of information
LONG lParam) // 32 additional bits of information
{
static WORD wFree;
char szBuffer[4];
short x;
short xText;
short yText;
DWORD dwExtent;
HDC hDC;
HMENU hMenu;
LONG lReturn;
PAINTSTRUCT ps;
RECT rect;
RECT rectTmp;
WORD wTmp;
lReturn = 0L;
switch (wMessage)
{
case WM_CREATE:
hMenu = GetSystemMenu(hWnd, FALSE);
ChangeMenu(hMenu, 0, NULL, 0, MF_APPEND);
ChangeMenu(hMenu, 0, "A&bout...", IDM_ABOUT, MF_APPEND);
ChangeMenu(hMenu, SC_MINIMIZE, "Mi&nimize", SC_CLOSE,
MF_CHANGE | MF_DISABLED | MF_GRAYED);
ChangeMenu(hMenu, SC_MAXIMIZE, "Ma&ximize", SC_CLOSE,
MF_CHANGE | MF_DISABLED | MF_GRAYED);
ChangeMenu(hMenu, SC_RESTORE, "&Restore", SC_CLOSE,
MF_CHANGE | MF_DISABLED | MF_GRAYED);
wFree = GetSystemResources();
break;
case WM_TIMER:
wTmp = GetSystemResources();
if (wTmp != wFree)
{
wFree = wTmp;
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case WM_SYSCOMMAND:
if (wParam == IDM_ABOUT)
DialogBox(hInst, "About", hWnd, About);
else
lReturn = DefWindowProc(hWnd, wMessage, wParam, lParam);
break;
case WM_PAINT:
// The "gas gauge" is created by drawing a text string stating
// the percentage into the middle of the gas gauge rectangle and
// by separating that rectangle into two parts: rectTmp (the
// left part, filled in blue) and rectTmp (the right part,
// filled in white). x is the x coordinate that divides these
// two rectangles. The text in the blue rectangle is drawn
// white and vice versa. This is easy to do with ExtTextOut()!
hDC = BeginPaint(hWnd, &ps);
wsprintf(szBuffer, "%d%%", wFree);
GetClientRect(hWnd, &rect);
SelectObject(hDC, GetStockObject(ANSI_VAR_FONT));
dwExtent = GetTextExtent(hDC, szBuffer, lstrlen(szBuffer));
x = ((rect.right - rect.left) * (100 - wFree)) / 100;
xText = rect.left +
((rect.right - rect.left) - LOWORD(dwExtent)) / 2;
yText = rect.top +
((rect.bottom - rect.top) - HIWORD(dwExtent)) / 2;
// paint in the "used" rectangle of the gas gauge with blue
// background and white text
SetRect(&rectTmp, rect.left, rect.top, rect.left + x,
rect.bottom);
SetTextColor(hDC, RGB(255, 255, 255));
SetBkColor(hDC, RGB(0, 0, 255));
ExtTextOut(hDC, xText, yText, ETO_CLIPPED | ETO_OPAQUE,
&rectTmp, szBuffer, lstrlen(szBuffer), NULL);
// paint in the "free" rectangle of the gas gauge with white
// background and blue text
SetRect(&rectTmp, rect.left + x, rect.top,
rect.right, rect.bottom);
SetTextColor(hDC, RGB(0, 0, 255));
SetBkColor(hDC, RGB(255, 255, 255));
ExtTextOut(hDC, xText, yText, ETO_CLIPPED | ETO_OPAQUE,
&rectTmp, szBuffer, lstrlen(szBuffer), NULL);
EndPaint(hWnd, &ps);
break;
case WM_QUERYOPEN:
break;
case WM_DESTROY:
KillTimer(hWnd, TIMER_ID);
PostQuitMessage(0);
break;
default:
lReturn = DefWindowProc(hWnd, wMessage, wParam, lParam);
break;
}
return(lReturn);
} // WndProc
// -------------------------------------------------------------------------
// Processes messages for the "About ..." dialog box.
// -------------------------------------------------------------------------
BOOL
FAR
PASCAL
About(
HWND hDlg, // window handle of the dialog
WORD wMessage, // type of message
WORD wParam, // 16 bits of information
LONG lParam) // 32 additional bits of information
{
short x;
short y;
short xSize;
short ySize;
BOOL bReturn;
RECT rect;
bReturn = FALSE;
switch (wMessage)
{
case WM_INITDIALOG:
// center the dialog
GetWindowRect(hDlg, &rect);
xSize = rect.right - rect.left;
ySize = rect.bottom - rect.top;
x = GetSystemMetrics(SM_CXSCREEN);
y = GetSystemMetrics(SM_CYSCREEN);
MoveWindow(hDlg, (x - xSize) / 2, (y - ySize) / 2,
xSize, ySize, TRUE);
// set the font of the text boxes
SendMessage(GetDlgItem(hDlg, IDOK), WM_SETFONT,
GetStockObject(ANSI_VAR_FONT), FALSE);
for (x = IDD_ABOUT1; x <= LAST_ABOUT; ++x)
{
SendMessage(GetDlgItem(hDlg, x), WM_SETFONT,
GetStockObject(ANSI_VAR_FONT), FALSE);
}
bReturn = TRUE;
break;
case WM_COMMAND:
if ((wParam == IDOK) || (wParam == IDCANCEL))
{
EndDialog(hDlg, TRUE);
bReturn = TRUE;
}
break;
}
return(bReturn);
} // About
// -------------------------------------------------------------------------
// Uses an undocumented system call and the algorithm given in TIPS.TXT to
// calculate the percentage of free system resources.
// -------------------------------------------------------------------------
WORD
NEAR
PASCAL
GetSystemResources(void)
{
WORD wFree;
WORD wGDI;
WORD wSize;
WORD wUser;
DWORD dwInfo;
// get the information on the GDI module
dwInfo = GetHeapSpaces(GetModuleHandle("GDI"));
wSize = HIWORD(dwInfo);
wFree = LOWORD(dwInfo);
wGDI = LOWORD(((DWORD) wFree) * 100 / wSize);
// get the information on the User module
dwInfo = GetHeapSpaces(GetModuleHandle("User"));
wSize = HIWORD(dwInfo);
wFree = LOWORD(dwInfo);
wUser = LOWORD(((DWORD) wFree) * 100 / wSize);
return(min(wGDI, wUser));
} // GetSystemResources
// =================
// end of resgauge.c
// =================