Category : Files from Magazines
Archive   : MSJV1-2.ZIP
Filename : WHATSIZE.ALL
Output of file : WHATSIZE.ALL contained in archive : MSJV1-2.ZIP
Volume 1; Issue 2; December, 1986
Code Listings For:
Whatsize
pp. 13-24
Author(s): Charles Petzold
Title: A Step-by-Step Guide to Building Your First Windows Application
==============================================================================
==============================================================================
Figure 2: WSZ.RC
==============================================================================
#include
#include "WSZ.h"
WhatSize ICON WSZ.ico
STRINGTABLE
BEGIN
IDS_NAME, "WhatSize"
IDS_ABOUT, "About..."
IDS_TITLE, "What Size is the Window?"
IDS_FORMAT, "%.3G %s by %.3G %s"
ID_MILLIM, "millimeters"
ID_INCHES, "inches"
ID_PIXELS, "pixels"
END
WhatSize MENU
BEGIN
POPUP "Colors"
BEGIN
MENUITEM "Black on White", ID_BLACK
MENUITEM "White on Black", ID_WHITE
END
MENUITEM "Units", IDM_UNITS
END
AboutBox DIALOG 20, 20, 144, 75
STYLE WS_POPUP | WS_DLGFRAME
BEGIN
CTEXT "What Size?", -1, 37, 5, 68, 8
ICON "WhatSize", -1, 9, 23, 0, 0
CTEXT "A Windows Application", -1, 0, 14, 144, 8
CTEXT "Version 1.00", -1, 38, 34, 64, 8
DEFPUSHBUTTON "Ok", IDOK, 53, 59, 32, 14, WS_GROUP
END
UnitsBox DIALOG 20, 20, 100, 90
STYLE WS_POPUP | WS_BORDER
CAPTION "What Size Units"
BEGIN
RADIOBUTTON "Pixels",ID_PIXELS,20,5,60,15,WS_GROUP | WS_TABSTOP
RADIOBUTTON "Millimeters", ID_MILLIM, 20, 25, 60, 15
RADIOBUTTON "Inches", ID_INCHES, 20, 45, 60, 15
DEFPUSHBUTTON "Ok", IDOK, 10, 70, 32, 15, WS_GROUP
PUSHBUTTON "Cancel", IDCANCEL, 58, 70, 32, 15
END
==============================================================================
==============================================================================
==============================================================================
Figure 4: WSZ.H Header File
==============================================================================
#define IDS_NAME 101
#define IDS_ABOUT 102
#define IDS_TITLE 103
#define IDS_FORMAT 104
#define ID_PIXELS 105
#define ID_MILLIM 106
#define ID_INCHES 107
#define ID_BLACK 108
#define ID_WHITE 109
#define IDM_ABOUT 110
#define IDM_UNITS 111
==============================================================================
==============================================================================
==============================================================================
Figure 5: WinMain Function for WSZ
==============================================================================
/* whatsize -- Windows application in C */
#include
#include "wsz.h"
long FAR PASCAL MainWndProc (HWND, unsigned, WORD, LONG) ;
BOOL FAR PASCAL AboutWndProc (HWND, unsigned, WORD, LONG) ;
BOOL FAR PASCAL UnitsWndProc (HWND, unsigned, WORD, LONG) ;
FARPROC lpAbout ;
FARPROC lpUnits ;
HANDLE hInst;
int CurrentUnits = ID_PIXELS ;
int CurrentColor = ID_BLACK ;
int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
HANDLE hInstance, hPrevInstance;
LPSTR lpszCmdLine;
int nCmdShow;
{
MSG msg;
if (!Initialize (hInstance, hPrevInstance, lpszCmdLine, nCmdShow))
return FALSE ;
while (GetMessage ((LPMSG)&msg, NULL, 0, 0))
{
TranslateMessage ((LPMSG)&msg) ;
DispatchMessage ((LPMSG)&msg) ;
}
return (msg.wParam) ;
}
==============================================================================
==============================================================================
==============================================================================
Figure 6: Initialization Function for WSZ
==============================================================================
BOOL Initialize (hInstance, hPrevInst, lpszCmdLine, nCmdShow)
HANDLE hInstance, hPrevInst ;
LPSTR lpszCmdLine ;
int nCmdShow ;
{
char *szAppName [10] ;
char *szAbout [10] ;
char *szTitle [30] ;
WNDCLASS wndclass ;
HWND hWnd;
HMENU hMenu;
LoadString (hInstance, IDS_NAME, (LPSTR) szAppName, 10) ;
LoadString (hInstance, IDS_ABOUT, (LPSTR) szAbout, 10) ;
LoadString (hInstance, IDS_TITLE, (LPSTR) szTitle, 30 ) ;
if (!hPrevInst)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, (LPSTR) szAppName);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = (LPSTR) szAppName;
wndclass.lpszClassName= (LPSTR) szAppName;
if (!RegisterClass ((LPWNDCLASS) &wndclass))
return FALSE;
}
else
{
GetInstanceData(hPrevInst,(NPSTR)&CurrentUnits,sizeof(int));
GetInstanceData(hPrevInst,(NPSTR)&CurrentColor,sizeof(int));
}
hWnd = CreateWindow (
(LPSTR) szAppName, /* class name */
(LPSTR) szTitle, /* caption title */
WS_TILEDWINDOW, /* windows style */
0, /* x (ignored) */
0, /* y (ignored) */
0, /* width (ignored) */
0, /* height (ignored) */
(HWND) NULL, /* parent (none) */
(HMENU) NULL, /* menu (use class) */
(HANDLE) hInstance, /* instance handle */
(LPSTR) NULL) ; /* parameters */
hInst = hInstance ;
lpAbout = MakeProcInstance ((FARPROC) AboutWndProc, hInstance) ;
lpUnits = MakeProcInstance ((FARPROC) UnitsWndProc, hInstance) ;
hMenu = GetSystemMenu (hWnd, FALSE) ;
ChangeMenu (hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR) ;
ChangeMenu (hMenu, 0, (LPSTR) szAbout, IDM_ABOUT,
MF_APPEND | MF_STRING) ;
hMenu = GetMenu (hWnd) ;
CheckMenuItem (hMenu, CurrentColor, MF_CHECKED) ;
ShowWindow (hWnd, nCmdShow) ;
UpdateWindow (hWnd) ;
return TRUE ;
}
==============================================================================
==============================================================================
==============================================================================
Figure 7: MainWndProc Procedure for WSZ
==============================================================================
long FAR PASCAL MainWndProc (hWnd, message, wParam, lParam)
HWND hWnd;
unsigned message;
WORD wParam;
LONG lParam;
{
HMENU hMenu ;
static int CharHorzPix, CharVertPix ;
static int ScrnHorzPix, ScrnVertPix ;
static int ScrnHorzMil, ScrnVertMil ;
static int WindHorzPix, WindVertPix ;
switch (message)
{
case WM_CREATE:
{
TEXTMETRIC tm ;
HDC hDC = GetDC (hWnd) ;
GetTextMetrics (hDC, (LPTEXTMETRIC) &tm) ;
CharHorzPix = tm.tmAveCharWidth ;
CharVertPix = tm.tmHeight ;
ScrnHorzPix = GetDeviceCaps (hDC, HORZRES) ;
ScrnVertPix = GetDeviceCaps (hDC, VERTRES) ;
ScrnHorzMil = GetDeviceCaps (hDC, HORZSIZE) ;
ScrnVertMil = GetDeviceCaps (hDC, VERTSIZE) ;
ReleaseDC (hWnd, hDC) ;
}
break ;
case WM_SIZE:
WindHorzPix = LOWORD (lParam) ;
WindVertPix = HIWORD (lParam) ;
break ;
case WM_ERASEBKGND:
return TRUE ;
case WM_PAINT:
{
PAINTSTRUCT ps ;
char szFormat [20] ;
char szUnits [20] ;
char szBuffer [60] ;
float nHorz = (float) WindHorzPix ;
float nVert = (float) WindVertPix ;
RECT rect ;
if (CurrentUnits != ID_PIXELS)
{
nHorz *= (float) ScrnHorzMil / ScrnHorzPix ;
nVert *= (float) ScrnVertMil / ScrnVertPix ;
}
if (CurrentUnits == ID_INCHES)
{
nHorz /= 25.4 ;
nVert /= 25.4 ;
}
BeginPaint (hWnd, (LPPAINTSTRUCT) &ps) ;
PatBlt (ps.hdc, 0, 0, WindHorzPix, WindVertPix,
(CurrentColor == ID_WHITE) ? BLACKNESS : WHITENESS) ;
if (CurrentColor == ID_WHITE)
{
SetTextColor (ps.hdc, RGB (255, 255, 255)) ;
SetBkColor (ps.hdc, RGB (0, 0, 0)) ;
}
LoadString (hInst, IDS_FORMAT, (LPSTR) szFormat, 20) ;
LoadString (hInst, CurrentUnits, (LPSTR) szUnits, 20) ;
rect.bottom = WindVertPix - (rect.top = CharVertPix) ;
rect.right = WindHorzPix - (rect.left = CharHorzPix) ;
DrawText (ps.hdc, (LPSTR) szBuffer,
sprintf(szBuffer,szFormat,nHorz,szUnits,nVert,szUnits),
(LPRECT) &rect, DT_CENTER | DT_WORDBREAK) ;
EndPaint (hWnd, (LPPAINTSTRUCT) &ps) ;
}
break;
case WM_SYSCOMMAND:
switch (wParam)
{
case IDM_ABOUT:
DialogBox(hInst,(LPSTR)"AboutBox",hWnd,lpAbout);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
break;
case WM_COMMAND :
switch (wParam)
{
case ID_BLACK:
case ID_WHITE:
hMenu = GetMenu (hWnd) ;
CheckMenuItem(hMenu,CurrentColor,MF_UNCHECKED);
CheckMenuItem (hMenu, CurrentColor = wParam,
MF_CHECKED) ;
InvalidateRect (hWnd, (LPRECT) NULL, TRUE) ;
break ;
case IDM_UNITS:
if (DialogBox (hInst, (LPSTR) "UnitsBox", hWnd,
lpUnits))
InvalidateRect (hWnd, (LPRECT) NULL, TRUE) ;
break ;
default :
return DefWindowProc (hWnd, message, wParam, lParam) ;
}
break ;
case WM_DESTROY:
PostQuitMessage (0) ;
break;
default:
return DefWindowProc (hWnd, message, wParam, lParam) ;
break;
}
return (0L) ;
}
==============================================================================
==============================================================================
==============================================================================
Figure 8: AboutWndProc Procedure for WSZ
==============================================================================
BOOL FAR PASCAL AboutWndProc (hDlg, message, wParam, lParam)
HWND hDlg ;
unsigned message ;
WORD wParam ;
LONG lParam ;
{
switch (message)
{
case WM_INITDIALOG :
return TRUE ;
case WM_COMMAND :
EndDialog (hDlg, TRUE) ;
return TRUE ;
default:
return FALSE ;
}
}
==============================================================================
==============================================================================
==============================================================================
Figure 9: UnitsWndProc Procedure for WSZ
==============================================================================
BOOL FAR PASCAL UnitsWndProc (hDlg, message, wParam, lParam)
HWND hDlg ;
unsigned message ;
WORD wParam ;
LONG lParam ;
{
static int NewUnits ;
switch (message)
{
case WM_INITDIALOG :
CheckRadioButton (hDlg,ID_PIXELS,ID_INCHES,CurrentUnits);
SetFocus (GetDlgItem (hDlg, NewUnits = CurrentUnits)) ;
return FALSE ;
case WM_COMMAND :
switch (wParam)
{
case ID_MILLIM:
case ID_INCHES:
case ID_PIXELS:
NewUnits = wParam ;
CheckRadioButton (hDlg,ID_PIXELS,ID_INCHES,NewUnits);
break ;
case IDOK:
CurrentUnits = NewUnits ;
EndDialog (hDlg, TRUE) ;
break ;
case IDCANCEL:
EndDialog (hDlg, FALSE) ;
break ;
default:
return FALSE ;
}
break ;
default:
return FALSE ;
}
return TRUE ;
}
==============================================================================
==============================================================================
==============================================================================
Figure 10: WSZ.DEF Definition File
==============================================================================
NAME WhatSize
DESCRIPTION 'A Windows Application'
STUB 'WINSTUB.EXE'
CODE MOVEABLE
DATA MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 4096
EXPORTS
MainWndProc @1
AboutWndProc @2
UnitsWndProc @3
==============================================================================
==============================================================================
==============================================================================
Figure 11: Make-File for WSZ
==============================================================================
wsz.res: wsz.rc wsz.ico wsz.h
rc -r wsz.rc
wsz.obj: wsz.c wsz.h
cl -d -c -W2 -AS -Gs -Gw -Oas -Zpd -FPa wsz.c
wsz.exe: wsz.obj wsz.res wsz.def
link4 wsz, wsz/align:16, wsz/map, slibw, wsz.def
mapsym wsz
rc wsz.res
==============================================================================
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/