home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
wint1_92
/
shaw
/
winapp.h
< prev
Wrap
C/C++ Source or Header
|
1991-11-04
|
11KB
|
340 lines
// winapp.h
#if !defined(WINAPP_H)
#define WINAPP_H
#include<windows.h>
#include<dos.h>
extern void *pWindow;
const unsigned WINAPP_RESERVED = sizeof(void *);
inline void *GetPointer(HWND hWnd)
{
#if defined(__SMALL__) || defined(__MEDIUM__)
return (void *)GetWindowWord(hWnd,0);
#elif defined(__LARGE__) || defined(__COMPACT__) || defined(__HUGE__)
return (void *)GetWindowLong(hWnd,0);
#else
#error Must use Small, Medium, Large, Compact or Huge models!
#endif
}
inline void SetPointer(HWND hWnd, void *ptr)
{
#if defined(__SMALL__) || defined(__MEDIUM__)
SetWindowWord(hWnd,0,(WORD)ptr);
#elif defined(__LARGE__) || defined(__COMPACT__) || defined(__HUGE__)
SetWindowLong(hWnd,0,(LONG)ptr);
#else
#error Must use Small, Medium, Large, Compact or Huge models!
#endif
}
/* WINAPPLICATION:
An object of this class is defined once for every application. It contains the
items passed to WinMain, as well as access functions for returning them. The
constructor initializes them.
*/
extern HANDLE _hInstance, _hPrev;
extern int _cmdShow;
extern unsigned int _psp, _pszCmdline;
class WinApplication // Windows application class
{
static HANDLE ApphInstance;
static HANDLE hPrevInstance;
static LPSTR lpszCmdLine;
static int nCmdShow;
public:
WinApplication(void)
{
ApphInstance = _hInstance;
hPrevInstance = _hPrev;
lpszCmdLine = (LPSTR)MK_FP(_psp,_pszCmdline);;
nCmdShow = _cmdShow;
}
static HANDLE GetInstance(void) { return ApphInstance; }
static HANDLE GetPrevInstance(void) { return hPrevInstance; }
static LPSTR GetCmdLine(void) { return lpszCmdLine; }
static int GetCmdShow(void) { return nCmdShow; }
static int Run(void); // default message loop processing
};
/* WINCLASS
*/
const NOSTYLE = 0;
const NOEXTRABYTES = 0;
class WinClass : WNDCLASS
{
BOOL class_registered;
public:
WinClass(void)
{
style = NOSTYLE;
lpfnWndProc = DefWindowProc;
cbClsExtra = NOEXTRABYTES;
cbWndExtra = NOEXTRABYTES;
hInstance = NULL;
hIcon = LoadIcon(NULL, IDI_APPLICATION);
hCursor = LoadCursor(NULL, IDC_ARROW);
hbrBackground = GetStockObject(WHITE_BRUSH);
lpszMenuName = NULL;
lpszClassName = NULL;
class_registered = FALSE;
}
// since 'this' is derived from WNDCLASS, it's passed as one
void ClassRegister(void)
{
RegisterClass(this);
class_registered = TRUE;
}
BOOL ClassRegistered(void)
{
if(class_registered)
return TRUE;
if(!lpszClassName || !hInstance)
return FALSE;
WNDCLASS wndclass;
if(GetClassInfo(hInstance,lpszClassName,&wndclass))
{
LPSTR menuName = lpszMenuName;
HANDLE hInst = hInstance;
LPSTR className = lpszClassName;
WNDCLASS *wc = this;
*wc = wndclass; // structure assignment
lpszClassName = className;
lpszMenuName = menuName;
hInstance = hInst;
return class_registered = TRUE;
}
return FALSE;
}
};
/* WINHANDLE
An object of this class is created with every instance of Window (below). It
contains data that controls Window creation handling, as well as the window
handle.
*/
class WinHandle
{
HWND hWnd;
LPSTR classname;
LPSTR windowname;
DWORD winstyle;
int upper_left_x;
int upper_left_y;
int winwidth;
int winheight;
HWND winParent;
HMENU menu;
HANDLE hInstance;
LPSTR lpParam;
public:
WinHandle(void)
{
hWnd = NULL;
classname = (LPSTR)"WinApplication";
windowname = (LPSTR)"WinApplication:Window";
winstyle = 0;
upper_left_x = upper_left_y = winwidth = winheight = 0;
winParent = NULL;
menu = NULL;
hInstance = 0;
lpParam = NULL;
}
BOOL Create(void)
{
if(hWnd) // if window's already created, return TRUE
return TRUE;
hWnd = CreateWindow(classname,windowname,winstyle,upper_left_x,
upper_left_y,winwidth,winheight,winParent,menu,hInstance,
lpParam);
return (hWnd ? TRUE : FALSE);
}
friend class Window; // allow Window to modify members
};
/* WINDOW
Every window is derived from this abstract class.
Every class derived from this one must have a WinClass object (or a pointer
to one created by a derived class) and pass it back to Window.
And each must supply its own WndProc for the class.
*/
class Window : public WinApplication, WinClass
{
public:
HWND WHandle;
private:
static long far pascal WndProc(HWND, WORD, WORD, LONG);
static long far pascal MDIChildWndProc(HWND, WORD, WORD, LONG);
WinHandle WHdl;
int current_display;
int previously_visible;
long (far pascal *UserWndProc)(HWND, WORD, WORD, LONG);
void Show(void)
{ previously_visible = ShowWindow(WHandle,current_display); }
void SetClassWinXbytes(int xtrabytes)
{ cbWndExtra = xtrabytes; }
public:
Window(char *winname)
{
SetClassInstance(); // insert instance handle
SetWinInstance(GetInstance());
SetClassName(winname);
SetWinName(winname);
DefaultDisplay();
previously_visible = FALSE;
WHandle = 0;
SetClassWinXbytes(WINAPP_RESERVED);
lpfnWndProc = Window::WndProc;
};
~Window(void)
{
if(WHandle)
DestroyWindow(WHandle);
}
// displays window and creates if not already created
BOOL Display(void)
{
Register();
if(!Create())
return FALSE;
Show();
Update();
return TRUE;
}
BOOL Display(int display_style)
{
current_display = display_style;
return Display();
}
BOOL ClassRegistered(void)
{
if(WinClass::ClassRegistered())
{
if(!UserWndProc)
SetClassWinProc(lpfnWndProc);
return TRUE;
}
return FALSE;
}
// should call SetClassInstance and SetClassName if not called
void RegisterMDIChild(void)
{
lpfnWndProc = Window::MDIChildWndProc;
Register();
}
void Register(void)
{
if(!ClassRegistered()) // if class not registered
{
if(!GetClassName()) // if class name not set
SetClassName("WinApplication");
ClassRegister(); // register the class
}
}
BOOL Create(void)
{
Register();
WHdl.hInstance = GetInstance();
WHdl.classname = lpszClassName;
WHdl.lpParam = (LPSTR)this;
return WHdl.Create();
}
HWND GetHandle(void) { return WHandle; }
void Hide(void) // hides the window
{ Display(SW_HIDE); }
void Minimize(void) // minimizes the window
{ Display(SW_MINIMIZE); }
void Maximize(void) // maximizes the window
{ Display(SW_SHOWMAXIMIZED); }
void Normalize(void) // displays window in original size and position
{ Display(SW_SHOWNORMAL); }
void DefaultDisplay(void)
{ current_display = GetCmdShow(); }
void Paint(void) // paints window
{
PAINTSTRUCT ps;
BeginPaint(WHandle, &ps);
EndPaint(WHandle, &ps);
}
void Update(void) // updates window
{ UpdateWindow(WHandle); }
// vocab of functions for modifying the registration info
void SetClassInstance(void)
{ hInstance = GetInstance(); }
void SetClassName(LPSTR classname)
{ lpszClassName = classname; }
void SetClassStyle(unsigned newstyle) { style = newstyle; }
void AddClassStyle(unsigned addstyle) { style |= addstyle; }
void SetClassWinProc(long (FAR PASCAL *lpfnWndProc)(HWND, unsigned,
WORD, LONG))
{ UserWndProc = lpfnWndProc; }
void AddClassWinXbytes(int xtrabytes)
{ cbWndExtra += xtrabytes; }
void SetClassIcon(LPSTR iconname)
{
hIcon = LoadIcon(GetInstance(),iconname);
// if(hInstance)
// hIcon = LoadIcon(hInstance,iconname);
}
void SetClassBackGround(HBRUSH handle)
{ hbrBackground = handle; }
LPSTR GetClassName(void) { return lpszClassName; }
void SetClassMenu(LPSTR menuname) { lpszMenuName = menuname; }
// vocab of functions for modifying the create info
void SetWinClassName(LPSTR classname) {WHdl.classname = classname; }
void SetWinName(LPSTR winname) { WHdl.windowname = winname;}
LPSTR GetWinName(void) { return WHdl.windowname;}
void SetWinStyle(DWORD dword) { WHdl.winstyle = dword; }
void AddWinStyle(DWORD dword) { WHdl.winstyle |= dword; }
void SetWinX(int x) { WHdl.upper_left_x = x; }
void SetWinY(int y) { WHdl.upper_left_y = y; }
void SetWinWidth(int width) { WHdl.winwidth = width; }
void SetWinHeight(int height) { WHdl.winheight = height; }
void SetWinParent(HWND hWnd) { WHdl.winParent = hWnd; }
void SetWinMenu(HMENU hmenu) { WHdl.menu = hmenu; }
void SetWinInstance(HANDLE hinst){ WHdl.hInstance = hinst; }
void SetWinParams(LPSTR param) { WHdl.lpParam = param; }
};
#endif