/////////////////////////////////////////////////////////// // Gtiny.CPP Demo program for gwin class in { gwtiny.h } /////////////////////////////////////////////////////////// /* In a small program used for mathematical tasks one would need to perform computations and plot graphs. Gtiny is a program that does just that. The computations are in the function num, and a graph is in the function plot. These two functions can easily be modified to perform similar new tasks which may be needed. A set of basic display and graphics functions are declared in gwtiny.h and defined in gwtiny.cpp. Typically one would start a new Win32 project, include the three files {gtiny.cpp, gtiny.rc, gwtiny.cpp}, build, and run. In cases where the set of basic functions in gwtiny is rich enough, one would simply copy {gtiny.h, gtiny.rc, gtiny.cpp} to a set with new names, say {myprog.h, myprog.rc, myprog.cpp}, and then make the modifications to suit the current task. At a later stage, new functions could be added to the class gwin in gwtiny. For example, one could add the function gwin::tics(int n1,int n2), which would put tics on a graph frame. Suggestion: get something very simple working smoothly before venturing off into the unknown. There are some more notes in the file gwtiny.cpp. Richard Hall */ #include "gwtiny.h" #include "gtiny.h" #include "gtiny.rc" char prog[] = "Gtiny" ; // -------------------------------------------------------------------------- inline double g(double x) // test functions for gwin::xplot { return exp(-x*x); } char gstr[80] = "g(x) = exp(-x^2)"; inline double gm(double x) { return -exp(-x*x); } inline double f(double x) { return g(x)*cos(15*x); } // ------------------------------------------------------------- void num(gwin &w, int p = 0); // program items void plot(gwin &w, int p = 0); // default screen p = 0 // -------------------------------------------------------------------------------- LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); // Win32 // NB For Win16 replace the above line by the 2 lines below //LRESULT FAR PASCAL _export WndProc(HWND hWnd, UINT message, // Win16 // WPARAM wParam, LPARAM lParam); // Other function prototypes. LRESULT PerformMenuCommand(HWND hWnd, WPARAM wParam,gwin &w); // Global instance handle. HINSTANCE hInstance; /////////////////////////////////////////////////////////// // WinMain /////////////////////////////////////////////////////////// int PASCAL WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR /*lpszCmdLine */, int nCmdShow) { WNDCLASS wndClass; HWND hwnd; MSG msg; // If there's no previous instance of this application, // define and register the window class. if (hPreviousInst == NULL) { wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WndProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hCurrentInst; wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndClass.lpszMenuName = MAKEINTRESOURCE(MENU_1); wndClass.lpszClassName = prog; RegisterClass(&wndClass); } hInstance = hCurrentInst; // Get the size of the screen. UINT width = GetSystemMetrics(SM_CXSCREEN) / 2; UINT height = GetSystemMetrics(SM_CYSCREEN) / 2; // Create a window of the previously defined class. hwnd = CreateWindow( prog, // Window class's name. "Gtiny Application", // Title bar text. WS_OVERLAPPEDWINDOW, // The window's style. 10, // X position. 10, // Y position. width, // Width. height, // Height. NULL, // Parent window's handle. NULL, // Menu handle. hCurrentInst, // Instance handle. NULL); // No additional data. // Display the window on the screen. ShowWindow(hwnd, nCmdShow); // Force the window to repaint itself. UpdateWindow(hwnd); // Start the message loop. while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } /////////////////////////////////////////////////////////// // WndProc() // // This is the main window procedure, which is called by // Windows. /////////////////////////////////////////////////////////// LRESULT CALLBACK WndProc (HWND hwnd, UINT message, // Win 32 WPARAM wParam, LPARAM lParam) // NB For Win16 replace the above 2 lines by the two lines below //LRESULT FAR PASCAL _export WndProc(HWND hwnd, UINT message, // WPARAM wParam, LPARAM lParam) { static gwin w(hwnd); // Handle the messages to which the application // must respond. switch(message) { case WM_COMMAND: return PerformMenuCommand(hwnd, wParam,w); case WM_DESTROY: PostQuitMessage(0); return 0; } // Make sure all unhandled messages get returned to Windows. return DefWindowProc(hwnd, message, wParam, lParam); } /////////////////////////////////////////////////////////// // PerformMenuCommand() /////////////////////////////////////////////////////////// LRESULT PerformMenuCommand(HWND hwnd, WPARAM wParam,gwin &w) { switch(wParam) { case CM_EXIT : close(hwnd); return 0 ; case CM_CLEAR : clear(hwnd); return 0; case CM_NUM : num(w); return 0; case CM_NUMP : num(w,1); return 0; case CM_PLOT: plot(w); return 0; case CM_PLOTP: plot(w,1); return 0; case CM_HELP : MessageBox (hwnd, "Num Text\n\n\ Plot Graphics\n\n\n\ (c) Richard Hall", "Help", MB_OK); return 0 ; } // end of switch return 0; } // ------------------------------------------------------- void num(gwin &w, int p) { w.open(p); w.locate(10,90,10,90); w.preframe(); w.scale(0,10,10,0,100); w.p(1,1,"Hello World"); w.p(1,2,"This is my first program"); w.p(1,3,gstr); double x = 1, y = g(x); w.pf(1,4,"g(%5.3g) = %.6f",x,y); w.close(); } // ------------------------------------------------------------------ void plot(gwin &w, int p) { w.open(p); int wb = 20; // screen window bottom if (p == 1) wb = 40; // higher for printer w.locate(20,80,wb,80); w.preframe(); w.scale(-3,3,-1.2,1.2,200); w.move(-3,0); w.line(3,0); w.move(0,-1.2); w.line(0,1.2); w.xplot(g); w.xplot(gm); w.xplot(f); double x = 0.5, y = g(x), d = 0.1; w.move(x,y); w.line(x+d,y+d); w.pf(x+d,y+2*d,"g(%.6g) = %.6g",x,y); w.scale(0,100,0,100,10); w.unclip(); w.p(5,110,"Wave Packet"); w.close(); } // ------------------------------------------------------------------