/***************************************************************************** PROGRAM: Mhello.c PURPOSE: "Pienin Windows-ohjelma". Tulostaa näyttöön tekstin Hello World Editor: Vesa Lappalainen typistänyt malliohjelmista. Project: mhello.c, mhello.def *****************************************************************************/ #include /* Tarvitaan kaikissa Windows C-ohjelmissa */ #include #define KOKO 500 char *WindowName="Sini käyrä"; #define LEVEYS 600 #define KORKEUS 400 #define MX(x) ( (x)*(k) + (xs) ) #define MY(y) ( -(y)*(k) + (ys) ) #define RMove(x,y) MoveTo(hDC,MX(x),MY(y)) #define RLine(x,y) LineTo(hDC,MX(x),MY(y)) void koordinaatisto( HDC hDC, double x, double y, double xstep, double ystep, double x1, double y1, double x2, double y2) { double xi,yi; double xk = LEVEYS/(x2-x1); double xs = LEVEYS/2; double yk = KORKEUS/(y2-y1); double k = xk < yk ? xk : yk; double ys = KORKEUS/2; double tx = (2.0/k); double ty = (2.0/k); /* x-akseli */ RMove(x1,y); RLine(x2,y); for (xi=x; xi>=x1; xi-=xstep) { RMove(xi,y-ty); RLine(xi,y+ty); } for (xi=x; xi<=x2; xi+=xstep) { RMove(xi,y-ty); RLine(xi,y+ty); } /* y-akseli */ RMove(x,y1); RLine(x,y2); for (yi=y; yi>=y1; yi-=ystep) { RMove(x-tx,yi); RLine(x+tx,yi); } for (yi=y; yi<=y2; yi+=ystep) { RMove(x-tx,yi); RLine(x+tx,yi); } } void piirra_sini(HWND hWnd,HDC hDC) { POINT aPoints[KOKO+1]; int i; double x1 = -2*M_PI, x2 = 2*M_PI; double y1 = -1, y2 = 1; /* min ja max sin(x) [x1,x2] */ double dx = (x2-x1)/KOKO; double xk = LEVEYS/(x2-x1); double xs = LEVEYS/2; double yk = KORKEUS/(y2-y1); double k = xk < yk ? xk : yk; double ys = KORKEUS/2; double x,y; (void)hWnd; for(i=0,x=x1; x<=x2; i++,x+=dx) { y = sin(x); aPoints[i].x = MX(x); aPoints[i].y = MY(y); } Polyline(hDC,aPoints,i); koordinaatisto(hDC,0,0,1,1,x1,y1,x2,y2); } LONG CALLBACK _export MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; switch (message) { case WM_PAINT: /* Viesti: Piirrä ikkuna uudelleen */ if ( BeginPaint(hWnd,&ps) ) piirra_sini(hWnd, ps.hdc); EndPaint(hWnd,&ps); return NULL; case WM_DESTROY: /* Viesti: ikkuna hävitetään */ PostQuitMessage(0); return NULL; default: /* Antaa Windowsin käsitellä muut */ break; } return DefWindowProc(hWnd, message, wParam, lParam); } int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; /* Ikkunaluokka */ HWND hWnd; /* Pääikkunan kahva */ MSG msg; /* Viesti */ (void)lpCmdLine; /* Hämäystä, jottei valitusta param. käytt. */ if (!hPrevInstance) { /* Onko muita esiintymiä käynnisssä? */ wc.style = NULL; wc.lpfnWndProc = MainWndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "WHelloWClass"; if (!RegisterClass(&wc)) return 1; } hWnd = CreateWindow("WHelloWClass","SINI KÄYRÄ",WS_OVERLAPPEDWINDOW, 0,0,LEVEYS+20,KORKEUS+40, NULL,NULL,hInstance,NULL); if ( !hWnd ) return 1; ShowWindow(hWnd, nCmdShow); /* Näytetään ikkuna */ UpdateWindow(hWnd); /* Lähetetään WM_PAINT viesti */ while (GetMessage(&msg,NULL,NULL,NULL)) { TranslateMessage(&msg); /* Tulkitaan virtuaaliset näp. koodit */ DispatchMessage(&msg); /* Lähetetään viesti ikkunalle */ } return msg.wParam; /* Palautetaan PostQuitMessage-funktion arvo */ }