Fourth. Windows graphics Device interface and Windows drawing P83 4-6

Source: Internet
Author: User
Tags sin

Experiment (Internship) name graphic programming and its Application Experiment (Internship) Date

First, Experimental Purpose

1. Familiar with the concept of graphics device interface and its application.

Ii. contents of the experiment and Steps

Experimental Tasks

1. Familiar with graphic application-based programming;

2. Mastering the Refresh technology and its application;

3. Mastering the application of mapping mode;

4. Master the use of drawing tools.

Experimental content

Write a program that displays a solid circle on the screen that moves along the sinusoidal trajectory , and every One-fourth cycles, the circle's fill color and the circle's surrounding color change, At the same time, the radius of the circle increases linearly from 0.2 times to 0.6 times times the amplitude of the sinusoidal curve within One-fourth cycles.

Program code:

#include <windows.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#define PI 3.1415926

Long WINAPI WndProc (HWND hwnd,uint imessage,uint wparam,long lParam);

Double dftheta=0,dfrange=100.0;

Long i=0,j=0;

Long lcentrex=0,lcentrey=0,lradious= (Long) (0.2*dfrange);

Point lpsin[100];

int WINAPI WinMain (hinstance hinstance,hinstance hprevinst,lpstr lpszcmdline,int ncmdshow)

{

HWND hwnd;

MSG Message;

Wndclass Wndclass;

wndclass.cbclsextra=0; Window class no extension

wndclass.cbwndextra=0; Window instance has no extension

Wndclass.hinstance=hinstance; Current instance Handle

Wndclass.hcursor=loadcursor (Null,idc_arrow); Window with arrow cursor

Wndclass.hbrbackground= (Hbrush) getstockobject (White_brush); Window background is white

Wndclass.lpszmenuname=null; No menu in Window

Wndclass.lpszclassname= "SIN"; Window class is named "Window"

Wndclass.hicon=loadicon (null,idi_application); The minimized icon for the window is the default icon

Wndclass.lpfnwndproc=wndproc; defining window processing functions

wndclass.style=0;

if (! RegisterClass (&wndclass))//Warn if registration fails

{messagebeep (0);

return FALSE;

}

----------------Create a Window-------------------

Hwnd=createwindow (

"SIN",//window class name

"4_6",//Title name of window instance

Ws_overlappedwindow,//Window style

Cw_usedefault,

0,//the top left corner of the window coordinates the default value

Cw_usedefault,

0,//Height and width of window are default values

NULL,//This window has no parent window

NULL,//This window has no main menu

HINSTANCE,//The current handle of the application that created this window

NULL//Do not use this value

);

---------------Display window----------------------

ShowWindow (hwnd, ncmdshow);

--------------Draw the user area---------------------

UpdateWindow (HWND);

---------------message Loop----------------------

for (int j=0;j<100;j++)//generating point coordinates for a sinusoidal curve

{

lpsin[j].x= (Long) (J*2*PI/100*60);

Lpsin[j].y= (Long) (Dfrange*sin (j*2*pi/100));

}

while (GetMessage (&message, 0, 0, 0))

{

TranslateMessage (&message);

DispatchMessage (&message);

}

return message.wparam; Returns information back to the system when the message loop ends and the program terminates

}

Long WINAPI WndProc (HWND hwnd,uint imessage,uint wparam,long lParam)

{

HDC hdc; To define a handle to a device

Hbrush Hbrush; To define a handle to a paint brush

Hpen Hpen; To define a handle to a brush

Paintstruct ptstr;//defines a struct-body variable that contains drawing information

Switch (iMessage)//processing messages

{

Case WM_PAINT://Processing drawing messages

Hdc=beginpaint (HWND,&PTSTR);

Setwindoworgex (Hdc,-200,-200,null); Set Image mode

Hpen=createpen (Ps_dash,1,rgb (255,0,0)); Black Brush

SelectObject (Hdc,hpen); Select a brush

Polyline (hdc,lpsin,100);

if (i<=25)//First 1/4 cycles

{

Hpen=createpen (Ps_dash,1,rgb (255,0,0));

Hbrush=createhatchbrush (Hs_bdiagonal,rgb (255,0,0));

Lradious= (Long) (DFRANGE*0.2+I%25*DFRANGE*0.4/25);//COMPUTE radius

}

else if (i<=50)

{

Hpen=createpen (Ps_dash,1,rgb (0,255,0));

Hbrush=createhatchbrush (Hs_bdiagonal,rgb (0,255,0));

Lradious= (Long) (DFRANGE*0.2+I%25*DFRANGE*0.4/25);//COMPUTE radius

}

else if (i<=75)

{

Hpen=createpen (Ps_dash,1,rgb (0,0,255));

Hbrush=createhatchbrush (Hs_bdiagonal,rgb (0,0,255));

Lradious= (Long) (DFRANGE*0.2+I%25*DFRANGE*0.4/25);//COMPUTE radius

}

Else

{

Hpen=createpen (Ps_dash,1,rgb (255,255,0));

Hbrush=createhatchbrush (Hs_bdiagonal,rgb (255,255,0));

Lradious= (Long) (DFRANGE*0.2+I%25*DFRANGE*0.4/25);//COMPUTE radius

}

SelectObject (Hdc,hbrush); Select Paint Brush

SelectObject (Hdc,hpen);

lcentrex=lpsin[i].x; Center X coordinate

LCENTREY=LPSIN[I].Y; Center Y coordinate

Ellipse (Hdc,lcentrex-lradious,lcentrey-lradious,

lcentrex+lradious,lcentrey+lradious); Draw a Circle

i++;

DeleteObject (Hpen); Delete a brush

DeleteObject (Hbrush); Delete a paint brush

EndPaint (HWND,&PTSTR); Remove device Environment pointer

Sleep (100); Stop for 0.1 seconds.

if (i<100) InvalidateRect (hwnd,null,1); Refresh User Area

return 0;

Case Wm_destroy://Close window

PostQuitMessage (0);

return 0;

Default

Return (DefWindowProc (Hwnd,imessage,wparam,lparam));

}

}

Program Run Result:

Third, the experimental results

Through this experiment, I learned the program design based on graphics application, mastered the refresh technology and its application, learned the application of mapping pattern, and basic understanding of the use of drawing tools, and further deepened the understanding of visualization.

Fourth. Windows graphics Device interface and Windows drawing P83 4-6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.