File Archive

 
Output of file : HEXCALC.ALL contained in archive : MSJV3-1.ZIP

Microsoft Systems Journal
Volume 3; Issue 1; January, 1988

Code Listings For:

Hexcalc
pp. 39-48

Author(s): Charles Petzold
Title: HEXCALC: An Instructive Pop-Up Calculator for Microsoft Windows



Figure 2w
=========

#--------------------------------------
# HEXCALCW make file (Windows version)
#--------------------------------------

hexcalcw.obj: hexcalcw.c
cl -c -D LINT_ARGS -Gsw -Os -W2 -Zp hexcalcw.c

hexcalcw.res : hexcalcw.rc hexcalcw.ico
rc -r hexcalcw.rc

hexcalcw.exe: hexcalcw.obj hexcalcw.def hexcalcw.res
link4 hexcalcw, /align:16, /map, slibw, hexcalcw
rc hexcalcw.res



Figure 3w
=========


/*-----------------------------------------------
HEXCALCW.RC resource script (Windows version)
-----------------------------------------------*/

#include

HexCalcW ICON hexcalcw.ico

HexCalcW DIALOG 32768, 0, 102, 122
STYLE WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
CLASS "HexCalcW"
CAPTION "Hex Calculator"
BEGIN
PUSHBUTTON "D", 68, 8, 24, 14, 14
PUSHBUTTON "A", 65, 8, 40, 14, 14
PUSHBUTTON "7", 55, 8, 56, 14, 14
PUSHBUTTON "4", 52, 8, 72, 14, 14
PUSHBUTTON "1", 49, 8, 88, 14, 14
PUSHBUTTON "0", 48, 8, 104, 14, 14
PUSHBUTTON "0", 27, 26, 4, 50, 14
PUSHBUTTON "E", 69, 26, 24, 14, 14
PUSHBUTTON "B", 66, 26, 40, 14, 14
PUSHBUTTON "8", 56, 26, 56, 14, 14
PUSHBUTTON "5", 53, 26, 72, 14, 14
PUSHBUTTON "2", 50, 26, 88, 14, 14
PUSHBUTTON "Back", 8, 26, 104, 32, 14
PUSHBUTTON "C", 67, 44, 40, 14, 14
PUSHBUTTON "F", 70, 44, 24, 14, 14
PUSHBUTTON "9", 57, 44, 56, 14, 14
PUSHBUTTON "6", 54, 44, 72, 14, 14
PUSHBUTTON "3", 51, 44, 88, 14, 14
PUSHBUTTON "+", 43, 62, 24, 14, 14
PUSHBUTTON "-", 45, 62, 40, 14, 14
PUSHBUTTON "*", 42, 62, 56, 14, 14
PUSHBUTTON "/", 47, 62, 72, 14, 14
PUSHBUTTON "%", 37, 62, 88, 14, 14
PUSHBUTTON "Equals", 61, 62, 104, 32, 14
PUSHBUTTON "&&", 38, 80, 24, 14, 14
PUSHBUTTON "|", 124, 80, 40, 14, 14
PUSHBUTTON "^", 94, 80, 56, 14, 14
PUSHBUTTON "<", 60, 80, 72, 14, 14
PUSHBUTTON ">", 62, 80, 88, 14, 14
END


Figure 4w
=========


;-------------------------------------------------------
; HEXCALCW.DEF module definition file (Windows version)
;-------------------------------------------------------

NAME HexCalcW
DESCRIPTION 'Hexadecimal Calculator (C) Charles Petzold 1987'
STUB 'WINSTUB.EXE'
CODE MOVEABLE
DATA MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 4096
EXPORTS WndProc


Figure 6w
=========


/*--------------------------------------------------------
HEXCALCW.C -- Hexadecimal Calculator (Windows version)
--------------------------------------------------------*/

#include
#include
#include
#include
#include

long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;

int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
HANDLE hInstance, hPrevInstance;
LPSTR lpszCmdLine;
int nCmdShow;
{
static char szAppName [] = "HexCalcW" ;
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 = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = COLOR_WINDOW + 1 ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
return FALSE ;
}

hWnd = CreateDialog (hInstance, szAppName, 0, NULL) ;

ShowWindow (hWnd, nCmdShow) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

void ShowNumber (hWnd, dwNumber)
HWND hWnd ;
DWORD dwNumber ;
{
char szBuffer [20] ;

SetDlgItemText (hWnd, VK_ESCAPE,
strupr (ltoa (dwNumber, szBuffer, 16))) ;
}

DWORD CalcIt (dwFirstNum, nOperation, dwNum)
DWORD dwFirstNum, dwNum ;
short nOperation ;
{
switch (nOperation)
{
case '=' : return dwNum ;
case '+' : return dwFirstNum + dwNum ;
case '-' : return dwFirstNum - dwNum ;
case '*' : return dwFirstNum * dwNum ;
case '&' : return dwFirstNum & dwNum ;
case '|' : return dwFirstNum | dwNum ;
case '^' : return dwFirstNum ^ dwNum ;
case '<' : return dwFirstNum << dwNum ;
case '>' : return dwFirstNum >> dwNum ;
case '/' : return dwNum ? dwFirstNum / dwNum : ULONG_MAX ;
case '%' : return dwNum ? dwFirstNum % dwNum : ULONG_MAX ;
default : return 0L ;
}
}

long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
HWND hWnd;
unsigned iMessage;
WORD wParam;
LONG lParam;
{
static BOOL bNewNumber = TRUE ;
static DWORD dwNumber, dwFirstNum ;
static short nOperation = '=' ;
HWND hButton ;

switch (iMessage)
{
case WM_KEYDOWN: /* left arrow --> backspace */
if (wParam != VK_LEFT)
break ;
wParam = VK_BACK ;
/* fall through */
case WM_CHAR:
if ((wParam = toupper (wParam)) == VK_RETURN)
wParam = '=' ;

if (hButton = GetDlgItem (hWnd, wParam))
{
SendMessage (hButton, BM_SETSTATE, 1, 0L) ;
SendMessage (hButton, BM_SETSTATE, 0, 0L) ;
}
else
{
MessageBeep (0) ;
break ;
}
/* fall through */
case WM_COMMAND:
SetFocus (hWnd) ;

if (wParam == VK_BACK) /* backspace */
ShowNumber (hWnd, dwNumber /= 16) ;

else if (wParam == VK_ESCAPE) /* escape */
ShowNumber (hWnd, dwNumber = 0L) ;

else if (isxdigit (wParam)) /* hex digit */
{
if (bNewNumber)
{
dwFirstNum = dwNumber ;
dwNumber = 0L ;
}
bNewNumber = FALSE ;

if (dwNumber <= ULONG_MAX >> 4)
ShowNumber (hWnd, dwNumber =
16 * dwNumber + wParam -
(isdigit (wParam) ? '0' : 'A' - 10)) ;
else
MessageBeep (0) ;
}
else /* operation */
{
if (!bNewNumber)
ShowNumber (hWnd, dwNumber =
CalcIt (dwFirstNum, nOperation,
dwNumber)) ;
bNewNumber = TRUE ;
nOperation = wParam ;
}
break ;

case WM_DESTROY:
PostQuitMessage (0) ;
break ;

default :
return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
}
return 0L ;
}


Figure 2pm
=========


#------------------------------------------------
# HEXCALCP make file (OS/2 Presentation Manager)
#------------------------------------------------

hexcalcp.obj: hexcalcp.c hexcalcp.h
cl -c -D LINT_ARGS -G2sw -W2 -Zp hexcalcp.c

hexcalcp.res : hexcalcp.rc hexcalcp.h
rc -r hexcalcp.rc

hexcalcp.exe: hexcalcp.obj hexcalcp.def hexcalcp.res
link hexcalcp, /align:16, /map, slibc5 os2, hexcalcp
rc hexcalcp.res


Figure 3pm
=========


/*---------------------------------------------------------
HEXCALCP.RC resource script (OS/2 Presentation Manager)
---------------------------------------------------------*/

#include
#include "hexcalcp.h"

WINDOWTEMPLATE ID_HEXCALC
{
FRAME "Hex Calculator", 1, 110, 40, 102, 122, WS_VISIBLE |
FS_TITLEBAR | FS_SYSMENU | FS_MINMAX | FS_BORDER
{
WINDOW "", FID_CLIENT, 0, 0, 102, 122, "HexCalcP", WS_VISIBLE
{
PUSHBUTTON "D", 68, 8, 88, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "A", 65, 8, 72, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "7", 55, 8, 56, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "4", 52, 8, 40, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "1", 49, 8, 24, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "0", 48, 8, 4, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "0", 27, 26, 104, 50, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "E", 69, 26, 88, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "B", 66, 26, 72, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "8", 56, 26, 56, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "5", 53, 26, 40, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "2", 50, 26, 24, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "Back", 8, 26, 4, 32, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "F", 70, 44, 88, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "C", 67, 44, 72, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "9", 57, 44, 56, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "6", 54, 44, 40, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "3", 51, 44, 24, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "+", 43, 62, 88, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "-", 45, 62, 72, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "*", 42, 62, 56, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "/", 47, 62, 40, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "%", 37, 62, 24, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "Equals", 61, 62, 4, 32, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "&", 38, 80, 88, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "|", 124, 80, 72, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "^", 94, 80, 56, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON "<", 60, 80, 40, 14, 14, BS_NOMOUSEFOCUS
PUSHBUTTON ">", 62, 80, 24, 14, 14, BS_NOMOUSEFOCUS
}
}
}



Figure 4pm
=========


;-----------------------------------------------------------------
; HEXCALCP.DEF module definition file (OS/2 Presentation Manager)
;-----------------------------------------------------------------

NAME HEXCALCP
DESCRIPTION 'Hexadecimal Calculator (C) Charles Petzold 1987'
STUB 'OS2STUB.EXE'
HEAPSIZE 1024
STACKSIZE 4096
EXPORTS ClientWndProc



Figure 5pm
=========


/*----------------------------------------------------
HEXCALCP.H header file (OS/2 Presentation Manager)
----------------------------------------------------*/

#define ID_HEXCALC 1



Figure 6pm
=========


/*------------------------------------------------------------------
HEXCALCP.C -- Hexadecimal Calculator (OS/2 Presentation Manager)
------------------------------------------------------------------*/

#include
#include
#include
#include
#include
#include "hexcalcp.h"

ULONG EXPENTRY ClientWndProc (HWND, USHORT, ULONG, ULONG) ;

main ()
{
static CHAR szClassName [] = "HexCalcP" ;
HAB hAB ;
HMQ hMQ ;
HWND hWnd ;
QMSG qmsg ;

hAB = WinInitialize () ;
hMQ = WinCreateMsgQueue (hAB, 0) ;

WinRegisterClass (hAB, szClassName, ClientWndProc, 0L, 0, NULL) ;

hWnd = WinLoadDlg (HWND_DESKTOP, HWND_DESKTOP,
NULL, NULL, ID_HEXCALC, NULL) ;

WinSetFocus (HWND_DESKTOP, WinWindowFromID (hWnd, FID_CLIENT)) ;

while (WinGetMsg (hAB, &qmsg, NULL, 0, 0))
WinDispatchMsg (hAB, &qmsg) ;

WinDestroyWindow (hWnd) ;
WinDestroyMsgQueue (hMQ) ;
WinTerminate (hAB) ;

return 0 ;
}

void ShowNumber (hWnd, lNumber)
HWND hWnd ;
ULONG lNumber ;
{
CHAR szBuffer [20] ;
HWND hWndResult ;

hWndResult = WinWindowFromID (hWnd, VK_ESCAPE) ;

WinSetWindowText (hWndResult,
strupr (ltoa (lNumber, szBuffer, 16))) ;
}

ULONG CalcIt (lFirstNum, iOperation, lNum)
ULONG lFirstNum, lNum ;
SHORT iOperation ;
{
switch (iOperation)
{
case '=' : return lNum ;
case '+' : return lFirstNum + lNum ;
case '-' : return lFirstNum - lNum ;
case '*' : return lFirstNum * lNum ;
case '&' : return lFirstNum & lNum ;
case '|' : return lFirstNum | lNum ;
case '^' : return lFirstNum ^ lNum ;
case '<' : return lFirstNum << lNum ;
case '>' : return lFirstNum >> lNum ;
case '/' : return lNum ? lFirstNum / lNum : ULONG_MAX ;
case '%' : return lNum ? lFirstNum % lNum : ULONG_MAX ;
default : return 0L ;
}
}

ULONG EXPENTRY ClientWndProc (hWnd, nMessage, lParam1, lParam2)
HWND hWnd ;
USHORT nMessage ;
ULONG lParam1 ;
ULONG lParam2 ;
{
static BOOL bNewNumber = TRUE ;
static ULONG lNumber, lFirstNum ;
static SHORT iOperation = '=' ;
HWND hWndButton ;
SHORT idButton ;

switch (nMessage)
{
case WM_CHAR:
if (lParam1 & KC_KEYUP)
break ;

if (HIUSHORT (lParam2) == VK_LEFT) /* left arrow to */
LOUSHORT (lParam2) = VK_BACK ; /* backspace */

if (HIUSHORT (lParam2) == VK_RETURN) /* return to */
LOUSHORT (lParam2) = '=' ; /* equals */

if (LOUSHORT (lParam2) == 0)
break ;

LOUSHORT (lParam2) = toupper (LOUSHORT (lParam2)) ;

if (hWndButton =
WinWindowFromID (hWnd, LOUSHORT (lParam2)))
WinSendMsg (hWndButton, BM_CLICK, 0L, 0L) ;
else
WinAlarm (HWND_DESKTOP, WA_ERROR) ;

return 1L ;

case WM_COMMAND:
idButton = LOUSHORT (lParam1) ;

if (idButton == VK_BACK) /* backspace */
ShowNumber (hWnd, lNumber /= 16) ;

else if (idButton == VK_ESCAPE) /* escape */
ShowNumber (hWnd, lNumber = 0L) ;

else if (isxdigit (idButton)) /* hex digit */
{
if (bNewNumber)
{
lFirstNum = lNumber ;
lNumber = 0L ;
}
bNewNumber = FALSE ;

if (lNumber <= ULONG_MAX >> 4)
ShowNumber (hWnd, lNumber =
16 * lNumber + idButton -
(isdigit (idButton) ? '0' : 'A' - 10)) ;
else
WinAlarm (HWND_DESKTOP, WA_ERROR) ;
}
else /* operation */
{
if (!bNewNumber)
ShowNumber (hWnd, lNumber =
CalcIt (lFirstNum, iOperation, lNumber)) ;
bNewNumber = TRUE ;
iOperation = idButton ;
}
break ;

default :
return WinDefWindowProc (hWnd, nMessage, lParam1,
lParam2) ;
}
return 0L ;
}