// ----------------------------------------------------------------------- // gwtiny.h Tiny version of the graphics class gwin; and some globals. // ----------------------------------------------------------------------- // (c) Richard Hall May 1999 // ----------------------------------------------------------------------- /* Suppose one wished to write a graphics class and 'link' it to Windows, how might that be done? The class gwin declared in this file, and defined in gwtiny.cpp, can be so used. For a demo of this 'link', use the files { gtiny.h, gtiny.cpp, gtiny.rc} with { gwtiny.h, gwtiny.cpp } in a project. The declaration and definition of WndProc in gtiny.cpp have two versions: Win32 (eg VC >= 4, or Borland TCW >= 5): use the Win32 option Win16 (eg Borland TCW 4): use the Win16 option There are many other alternatives to the approach illustrated. windows.h already has a large number of powerful graphics and text functions. My gwin is capricious, using some, replacing others. In addition there are higher-level structures such as MFC and OWL which contain many additional powerful tools. Which way to go depends on needs, time, and personal taste. Some useful references for designing replacements or extensions to the gwin scheme are the books: Programming Windows 95 by C.Petzold (Microsoft 1996) Win32 Programming API Bible Book 1 by R. Simon (Waite 1996) Inside Visual C++ by David Kruglinsky (Microsoft 1995) All MFC ! If you use the Win16 options in gtiny.cpp, it works `as is' with TCW 4.5. It does also compile and run `as is' under Win32 using VC 4, although VC warns that some function calls are out-of-date. Naturally it it is better to use the Win32 options in a 32-bit environment. Advice: For programs which only produce text and numerical output, use generic code (for example iostream::cout) so that the programs will compile and run under any contemporary C++ environment. Although the advocates of Java promise us a new era of OS-independent graphics, the current C++ graphics capabilities remain strongly linked to each particular OS and windowing code. For MS Windows programming, try to move to the 32-bit environment, for example Microsoft VC 6 or Borland TCW 5. Don't spend much time studying the details of Win16: it's finished. Under Unix one typically uses the gnu compiler and xwindows for graphics. Xwindows is another `big story': there is much helpful code `out there', but it also has to be found and learnt. Try to choose a stable environment for you work before investing a great deal of time developing code. If you use gwin, please remember to acknowledge its original author, Richard Hall. */ #include #include #include #include #include #include #include #ifndef gwtiny_h #define gwtiny_h #ifndef fptype_1 #define fptype_1 typedef double (*fptype1)( double); #endif // Global WinXX utilities ------------------------------------ inline void clear(HWND hwnd) { InvalidateRect(hwnd, NULL, TRUE); UpdateWindow(hwnd); } inline void close(HWND hwnd) { SendMessage (hwnd, WM_CLOSE, 0, 0L); } // ------------------------------------------------------------ class gwin { protected: HWND hwnd; HDC hdc,hdcprn, hdct; // screen, printer, temp DC int prn; // printer flag RECT rect; // hwnd RECT nrect; // new rect chosen by locate HRGN hrgn, hnrgn; // m = m0 + cx*(x - x0) // cx = pm/px, similarly (n,y) double x0,x1,y0,y1,px,py,cx,cy; // scale variables int m0,m1,n0,n1,pn,pm; // pixel versions of scales double x,y; // current position int m,n; // current integer position int mw,nw; // screen window size int mt,nt; // current window size int scan; // number of points for x plotting int clipped; // clip flag TEXTMETRIC tm; // font data int cw, ch; // character sizes public: gwin(HWND hwndi); void setprn(HDC hi, int xi, int yi); // after gwin::open void endprn(); // wind up printer void open(int p = 0); // default screen void locate(int a,int b,int c,int d, int cl = 1); // selects % of client rect (Cartesian) void preframe(); void scale(double x0i, double x1i, double y0i, double y1i, int scani); double getscale(int k); int getscan(); int mx(double x); int ny(double y); void move(double xi,double yi); void line(double xi,double yi); void clip(); void unclip(); void box(double s,double t,double u,double v); void frame(); void xplotd(fptype1 fun, double xa, double xb, int nn); void xplot(fptype1 fun) { xplotd(fun, x0, x1, scan);} void p(double x, double y, char *c); // print to screen void pf(double x, double y, char *fmt, ...); //format strings void respond(WPARAM wParam, LPARAM lParam); void close(); void cls(); // end declaration of class gwin }; #endif