home *** CD-ROM | disk | FTP | other *** search
/ CICA 1992 November / CICA_MS_Windows_CD-ROM_Walnut_Creek_November_1992.iso / win3 / programr / listings / blx12 / winapp.h < prev    next >
Text File  |  1991-05-02  |  9KB  |  270 lines

  1. // winapp.h
  2.  
  3. #if !defined(WINAPP_H)
  4.  
  5. /* WINAPP:
  6. An object of this class is defined once for every application. It contains the
  7. items passed to WinMain, as well as access functions for returning them. The
  8. constructor initializes them.
  9. */
  10.  
  11. class WinApp    // Windows application class
  12.     {
  13.         static HANDLE hInstance;
  14.         static HANDLE hPrevInstance;
  15.         static LPSTR lpszCmdLine;
  16.         static int nCmdShow;
  17.     public:
  18.         WinApp(HANDLE hinst, HANDLE hpinst, LPSTR cmdline, int cmdshow)
  19.             {
  20.             hInstance = hinst;
  21.             hPrevInstance = hpinst;
  22.             lpszCmdLine = cmdline;
  23.             nCmdShow = cmdshow;
  24.             }
  25.         static HANDLE GetInstance(void) { return hInstance; }
  26.         static HANDLE GetPrevInstance(void) { return hPrevInstance; }
  27.         static LPSTR GetCmdLine(void) { return lpszCmdLine; }
  28.         static int GetCmdShow(void) { return nCmdShow; }
  29.         static int MessageLoop(void);    // default message loop processing
  30.    };
  31.  
  32.  
  33. /* WINCLASS
  34. */
  35. const NOSTYLE = 0;
  36. const NOEXTRABYTES = 0;
  37.  
  38. class WinClass : WNDCLASS
  39.     {
  40.         BOOL    class_registered;
  41.     public:
  42.         WinClass(void)
  43.             {
  44.             style = NOSTYLE;
  45.             lpfnWndProc = DefWindowProc;
  46.             cbClsExtra = NOEXTRABYTES;
  47.             cbWndExtra = NOEXTRABYTES;
  48.             hInstance = NULL;
  49.             hIcon = LoadIcon(NULL, IDI_APPLICATION);
  50.             hCursor = LoadCursor(NULL, IDC_ARROW);
  51.             hbrBackground = GetStockObject(WHITE_BRUSH);
  52.             lpszMenuName = NULL;
  53.             lpszClassName = NULL;
  54.             class_registered = FALSE;
  55.             }
  56.             // since 'this' is derived from WNDCLASS, it's passed as one
  57.         void Register(void)
  58.             {
  59.             RegisterClass(this);
  60.             class_registered = TRUE;
  61.             }
  62.         BOOL Registered(void)   { return class_registered; }
  63.         friend class Window;    // allow Window to modify members
  64.     };
  65.  
  66. /* WINHANDLE
  67. An object of this class is created with every instance of Window (below). It
  68. contains data that controls Window creation handling, as well as the window
  69. handle.
  70.  */
  71.  
  72. class WinHandle
  73.     {
  74.         HWND hWnd;
  75.         LPSTR classname;
  76.         LPSTR windowname;
  77.         DWORD winstyle;
  78.         int upper_left_x;
  79.         int upper_left_y;
  80.         int winwidth;
  81.         int winheight;
  82.         HWND winParent;
  83.         HMENU menu;
  84.         HANDLE hInstance;
  85.         LPSTR lpParam;
  86.     public:
  87.         WinHandle(void)
  88.             {
  89.             hWnd = NULL;
  90.             classname = (LPSTR)"WinApp";
  91.             windowname = (LPSTR)"WinApp:Window";
  92.             winstyle = 0;
  93.             upper_left_x = upper_left_y = winwidth = winheight = 0;
  94.             winParent = NULL;
  95.             menu = NULL;
  96.             hInstance = 0;
  97.             lpParam = NULL;
  98.             }
  99.  
  100.         HWND GetHandle(void) { return hWnd; }
  101.         HWND Create(void)
  102.             {
  103.             if(hWnd)    // if window's already created, return TRUE
  104.                 return TRUE;
  105.  
  106.             hWnd = CreateWindow(classname,windowname,winstyle,upper_left_x,
  107.                 upper_left_y,winwidth,winheight,winParent,menu,hInstance,
  108.                 lpParam);
  109.  
  110.             return (hWnd ? TRUE : FALSE);
  111.             }
  112.  
  113.         friend class Window;    // allow Window to modify members
  114.     };
  115.  
  116.  
  117. /* WINDOW
  118.  
  119. Every window is derived from this abstract class.
  120.  
  121. Every class derived from this one must have a WinClass object (or a pointer
  122. to one created by a derived class) and pass it back to Window.
  123.  
  124. And each must supply its own WndProc for the class.
  125. */
  126. class Window
  127.     {
  128.         WinHandle WHdl;
  129.         int     current_display;
  130.         int     previously_visible;
  131.         void    Show(void)
  132.             { previously_visible = ShowWindow(GetHandle(),current_display); }
  133.     protected:
  134.         WinClass *MyWc;
  135.         WinApp *MyApp;
  136.  
  137.     public:
  138.         Window(WinApp *app, WinClass *wc)
  139.             {
  140.             MyApp = app;
  141.             MyWc = wc;
  142.             DefaultDisplay();
  143.             previously_visible = FALSE;
  144.             };
  145.  
  146.             // displays window and creates if not already created
  147.         BOOL Display(void)
  148.             {
  149.             Register();
  150.  
  151.             if(!Create())
  152.                 return FALSE;
  153.  
  154.             Show();
  155.  
  156.             Update();
  157.  
  158.             return TRUE;
  159.             }
  160.  
  161.         BOOL Display(int display_style)
  162.             {
  163.             current_display = display_style;
  164.             return Display();
  165.             }
  166.             // should call SetClassInstance and SetClassName if not called
  167.         void Register(void)
  168.             {
  169.             if(!MyWc->Registered())         // if class not registered
  170.                 {
  171.                 SetClassWinXbytes(sizeof(Window *));
  172.                 SetClassInstance();         // insert instance handle
  173.                 if(!GetClassName())         // if class name not set
  174.                     SetClassName("WinApp");
  175.                 MyWc->Register();           // register the class
  176.                 }
  177.             }
  178.  
  179.         BOOL Create(void)
  180.             {
  181.             Register();
  182.             WHdl.hInstance = MyApp->GetInstance();
  183.             WHdl.classname = MyWc->lpszClassName;
  184.             WHdl.lpParam = (LPSTR)this;
  185.             return WHdl.Create();
  186.             }
  187.  
  188.         HWND GetHandle(void) { return WHdl.hWnd; }
  189.  
  190.         void Hide(void)  // hides the window
  191.             { Display(SW_HIDE); }
  192.         void Minimize(void)  // minimizes the window
  193.             { Display(SW_MINIMIZE); }
  194.         void Maximize(void)  // maximizes the window
  195.             { Display(SW_SHOWMAXIMIZED); }
  196.         void Normalize(void) // displays window in original size and position
  197.             { Display(SW_SHOWNORMAL); }
  198.         void DefaultDisplay(void)
  199.             { current_display = MyApp->GetCmdShow(); }
  200.  
  201.         void Paint(void)     // paints window
  202.             {
  203.             PAINTSTRUCT ps;
  204.             RECT    rect;
  205.  
  206.             BeginPaint(GetHandle(), &ps);
  207.             EndPaint(GetHandle(), &ps);
  208.             }
  209.  
  210.         void Update(void)   // updates window
  211.             { UpdateWindow(GetHandle()); }
  212.  
  213.              // vocab of functions for modifying the registration info
  214.         void SetClassInstance(void)
  215.             { MyWc->hInstance = MyApp->GetInstance(); }
  216.         void SetClassName(LPSTR classname)
  217.             { MyWc->lpszClassName = classname; }
  218.         void SetClassStyle(unsigned newstyle)    { MyWc->style = newstyle;  }
  219.         void AddClassStyle(unsigned addstyle)    { MyWc->style |= addstyle; }
  220.         void SetClassWinProc(long (FAR PASCAL *lpfnWndProc)(HWND, unsigned,
  221.             WORD, LONG))
  222.             { MyWc->lpfnWndProc = lpfnWndProc; }
  223.         void SetClassWinXbytes(int xtrabytes)
  224.             { MyWc->cbWndExtra = xtrabytes; }
  225.         void SetClassIcon(LPSTR iconname)
  226.             {
  227.             if(MyWc->hInstance)
  228.                 MyWc->hIcon = LoadIcon(MyWc->hInstance,iconname);
  229.             }
  230.  
  231.         LPSTR GetClassName(void)    { return MyWc->lpszClassName; }
  232.  
  233.         // vocab of functions for modifying the create info
  234.         void SetWinName(LPSTR winname)  { WHdl.windowname = winname; }
  235.         void SetWinStyle(DWORD dword)    { WHdl.winstyle = dword; }
  236.         void AddWinStyle(DWORD dword)    { WHdl.winstyle |= dword;  }
  237.         void SetWinX(int x)              { WHdl.upper_left_x = x; }
  238.         void SetWinY(int y)              { WHdl.upper_left_y = y; }
  239.         void SetWinWidth(int width)      { WHdl.winwidth = width; }
  240.         void SetWinHeight(int height)    { WHdl.winheight = height; }
  241.         void SetWinInstance(HANDLE hinst){ WHdl.hInstance = hinst; }
  242.     };
  243.  
  244.  
  245. inline void *GetPointer(HWND hWnd)
  246.     {
  247. #if defined(__SMALL__) || defined(__MEDIUM__)
  248.     return (void *)GetWindowWord(hWnd,0);
  249. #elif defined(__LARGE__) || defined(__COMPACT__)
  250.     return (void *)GetWindowLong(hWnd,0);
  251. #else
  252.     #error Must use Small, Medium, Large or Compact models!
  253. #endif
  254.     }
  255.  
  256. inline void SetPointer(HWND hWnd, void *ptr)
  257.     {
  258. #if defined(__SMALL__) || defined(__MEDIUM__)
  259.     SetWindowWord(hWnd,0,(WORD)ptr);
  260. #elif defined(__LARGE__) || defined(__COMPACT__)
  261.     SetWindowLong(hWnd,0,(LONG)ptr);
  262. #else
  263.     #error Must use Small, Medium, Large or Compact models!
  264. #endif
  265.     }
  266.  
  267.  
  268. #define WINAPP_H
  269. #endif
  270.