/* Demonstrate the basic graphics functions. */
#include <windows.h>
#include <math.h>static int line_wide=1;
static float r=0.0;
int round(float x)
{
int y;
if(x-int(x)>=0.5)
{ y=int(x)+1;
return y;
}
else{
y=int(x);
return y;
}
}void SetPixel(HDC hdc,int x0,int y0,int color,int flag)
{
switch(flag)
{
int i,j;
case 1:SetPixel(hdc,x0,y0,color);break;
case 3:for (i=-1;i<=1;i++)
{
for (j=-1;j<=1;j++)
{
SetPixel(hdc,x0+i,y0+j,color);
}
}
break;
case 5:for (i=-2;i<=2;i++)
   {
   for (j=-2;j<=2;j++)
   {
   SetPixel(hdc,x0+i,y0+j,color);
   }
   }
   break;
}
}
void circlePlotPoints(HDC hdc,int xc,int yc,int x,int y)
{
SetPixel(hdc,xc+x,yc+y,RGB(0,0,255));
SetPixel(hdc,xc-x,yc+y,RGB(0,0,255));
SetPixel(hdc,xc+x,yc-y,RGB(0,0,255));
SetPixel(hdc,xc-x,yc-y,RGB(0,0,255));
SetPixel(hdc,xc+y,yc+x,RGB(0,0,255));
SetPixel(hdc,xc-y,yc+x,RGB(0,0,255));
SetPixel(hdc,xc+y,yc-x,RGB(0,0,255));
SetPixel(hdc,xc-y,yc-x,RGB(0,0,255));
}
//void circleMidpoint(HDC hdc,int xc,int yc,int r)
void circleMidpoint(HDC hdc,int xc,int yc,int xp,int yp)
{
 
 r=sqrt((float)((xc-xp)*(xc-xp)+(yc-yp)*(yc-yp)));
float x=0.0;
float y=r;
float d=1-r;
circlePlotPoints(hdc,xc,yc,x,y);
while(x<y)
{
x++;
if(d<0)
d+=2*x+1;
else
{
y--;
d+=2*(x-y)+1;
}
circlePlotPoints(hdc,xc,yc,x,y);
}
}
void linedda(HDC hdc,int x1,int y1,int x2,int y2)
{
float increx,increy,x,y,length;
int i;
if(abs(x2-x1)>abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
increx=(x2-x1)/length;
increy=(y2-y1)/length;
x=x1;
y=y1;
for(i=1;i<=length;i++)
{
SetPixel(hdc,round(x),round(y),RGB(255,0,0),line_wide);
x=x+increx;
y=y+increy;
}
}
int nwidth=1;void MidPointLine(HDC hdc,int x0, int y0,int x1, int y1)
{
int a,b,delta1,delta2,d,x,y,dx,dy,i,s1,s2,width,count;//
width=nwidth/2;
a=y0-y1;
b=x1-x0; x=x0;
y=y0;
SetPixel(hdc,x,y,RGB(0,255,0),line_wide);
dx=abs(b);
dy=abs(a);
if(b>0)
s1=1;
else
s1=-1;
if(a<0)
s2=1;
else
s2=-1;
if(s1<0)
a=-a;
if(s2<0)
b=-b; if(dy<=dx)
{
d=a+a+b;
delta1=a+a;
delta2=delta1+b+b;
if(s1==s2)
{
for(i=0;i<dx;i++)
{
if(d<0)
{
x+=s1;
y+=s2;
d+=delta2;
}
else
{
x+=s1;
d+=delta1;
}
for(count=0;count<=width;count++)
{
SetPixel(hdc,x,y+count,RGB(0,255,0),line_wide);
SetPixel(hdc,x,y-count,RGB(0,255,0),line_wide);
}
}
}
else
{
for(i=0;i<dx;i++)
{
if(d>=0)
{
x+=s1;
y+=s2;
d+=delta2;
}
else
{
x+=s1;
d+=delta1;
}
for(count=0;count<=width;count++)
{
SetPixel(hdc,x,y+count,RGB(0,255,0),line_wide);
SetPixel(hdc,x,y-count,RGB(0,255,0),line_wide);
}
}
}
}
else
{
d=a+b+b;
delta1=b+b;
delta2=delta1+a+a;
if(s1==s2)
{
for(i=0;i<dy;i++)
{
if(d>0)
{
x+=s1;
y+=s2;
d+=delta2;
}
else
{
y+=s2;
d+=delta1;
}
for(count=0;count<=width;count++)
{
SetPixel(hdc,x+count,y,RGB(0,255,0),line_wide);
SetPixel(hdc,x-count,y,RGB(0,255,0),line_wide);
}
}

解决方案 »

  1.   

    }
    else
    {
    for(i=0;i<dy;i++)
    {
    if(d<0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    y+=s2;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x+count,y,RGB(0,255,0),line_wide);
    SetPixel(hdc,x-count,y,RGB(0,255,0),line_wide);
    }
    }
    }
    }
    }
    /*void MidPointLine(HDC hdc,int x1,int y1,int x2,int y2)
    {
    int dx,dy,n,k,i,f;
    int x,y;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    n=dx+dy;
    if(x2>x1){k=y2>=y1?1:4;x=x1;y=y1;}
    else {k=y2>=y1?2:3;x=x1;y=y1;}
    SetPixel(hdc,x,y,RGB(0,255,0),line_wide);
    for(i=0,f=0;i<n;i++)
    if(f>=0)
    switch(k){
    case 1:SetPixel(hdc,x++,y,RGB(0,255,0),line_wide);f-=dy;break;
    case 2:SetPixel(hdc,x,y++,RGB(0,255,0),line_wide);f-=dx;break;
    case 3:SetPixel(hdc,x--,y,RGB(0,255,0),line_wide);f-=dy;break;
    case 4:SetPixel(hdc,x,y--,RGB(0,255,0),line_wide);f-=dx;break;
    }
    else
    switch(k){
    case 1:SetPixel(hdc,x,y++,RGB(0,255,0),line_wide);f+=dx;break;
    case 2:SetPixel(hdc,x--,y,RGB(0,255,0),line_wide);f+=dy;break;
    case 3:SetPixel(hdc,x,y--,RGB(0,255,0),line_wide);f+=dx;break;
    case 4:SetPixel(hdc,x++,y,RGB(0,255,0),line_wide);f+=dy;break;
    }
    }*/
    #include "graph.h"
    #include "MappingMode.h"
    static int flag=1;LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);char szWinName[] = "MyWin"; /* name of window class */char str[255]; /* holds output strings */int maxX,maxY; /* screen dimensions */
    int orgX=0,orgY=0; /* viewport orgin */HDC hmemdc; /* store the virtual device handle */
    HBITMAP hbit; /* store the virtual bitmap */
    HBRUSH hbrush; /* store the brush handle */
    HGDIOBJ hOldBit,hOldbrush;HDC hdc;
    /* create pens */
    HPEN hOldpen; /* handle of old pen */
    //HPEN hWhitePen;
    HWND hwnd;HDC hmemdc2;
    HBITMAP hbit2,hOldBit2;
    int xP,yP,xS,yS;
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs,int nWinMode)
    {
    MSG msg;
    WNDCLASSEX wcl;
    HACCEL hAccel; /* Define a window class. */
    wcl.cbSize = sizeof(WNDCLASSEX);
    wcl.hInstance = hThisInst; /* handle to this instance */
    wcl.lpszClassName = szWinName; /* window class name */
    wcl.lpfnWndProc = WindowFunc; /* window function */
    wcl.style = 0; /* default style */ wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION); /* standard icon */
    wcl.hIconSm = LoadIcon(NULL,IDI_WINLOGO); /* SMALL ICON */
    wcl.hCursor = LoadCursor(NULL,IDC_ARROW); /* cursor style */ /* Specify name of menu resource. This will be overridden. */
    wcl.lpszMenuName = "MYMENU"; /* main menu */
    wcl.cbClsExtra = 0; /* no extra information needed */
    wcl.cbWndExtra = 0; /* no extra information needed */

    /* Make the windows backgraoud white */
    wcl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); /* Register the window class. */
    if(!RegisterClassEx(&wcl))
    return 0; /* Now that a window class has been registered, a window can be created. */
    hwnd = CreateWindow(
    szWinName, /* name of window class */
    "Changing Mapping Modes", /* title */
    WS_VISIBLE|WS_CAPTION|WS_BORDER|WS_MINIMIZEBOX|WS_SYSMENU, /* window style - normal */
    CW_USEDEFAULT, /* X coordinate - let Winodws decide */
    CW_USEDEFAULT, /* Y coordinate - let Windows decide */
    CW_USEDEFAULT, /* width - let Windows decide */
    CW_USEDEFAULT, /* height - let Windows decide */
    HWND_DESKTOP, /* no parent window */
    NULL,  /*  no menu */
    hThisInst, /* handle of this instance of the program */
    NULL /* no additional arguments */
    ); /* Load accelerators. */
    hAccel = LoadAccelerators(hThisInst,"MyMenu"); /* Display the windows. */
    ShowWindow(hwnd, nWinMode);
    UpdateWindow(hwnd); /* Createthe message loop. */
    while(GetMessage(&msg, NULL, 0,0 )) {
    if(!TranslateAccelerator(hwnd,hAccel,&msg)) {
    TranslateMessage(&msg); /* translate keyboard messages */
    DispatchMessage(&msg); /* return control to Windows 98 */
    }
    }
    return msg.wParam;
    }
      

  2.   

    /* This function is called by Windows 98 and is passed 
    messages from the message gueue.
    */
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
    {
    PAINTSTRUCT ps;
    RECT *pr;
    int response;
    static StartDraw = 0;
    int nTop,nLeft,nRight,nBottom,nPositionF,nPositionS;
    switch(message) {
    case WM_CREATE: { /*load the bitmap */

    /* get screen coordinates */
    maxX = GetSystemMetrics(SM_CXSCREEN);
    maxY = GetSystemMetrics(SM_CYSCREEN); /* make a compatible memory image */
    hdc = GetDC(hwnd);
    hmemdc = CreateCompatibleDC(hdc);

    hbit = CreateCompatibleBitmap(hdc,maxX,maxY);
    hOldBit = SelectObject(hmemdc,hbit);
    hbrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
    hOldbrush = SelectObject(hmemdc,hbrush);
    PatBlt(hmemdc,0,0,maxX,maxY,PATCOPY);//非常重要,要么屏幕会黑
    // hWhitePen = (HPEN) GetStockObject(WHITE_PEN);//利用双缓冲区
    hmemdc2 = CreateCompatibleDC(hdc);
    hbit2 = CreateCompatibleBitmap(hdc,maxX,maxY);
    hOldBit2 = (HBITMAP)SelectObject(hmemdc2,hbit2);


    PatBlt(hmemdc2,0,0,maxX,maxY,PATCOPY);//非常重要,要么屏幕会黑
    /* save default pen */
    // hOldpen = (HPEN)SelectObject(memdc,hRedpen);
    // SelelctObject(memdc,hOldpen);
    ReleaseDC(hwnd,hdc);
    break;
    }
    case WM_COMMAND:
    switch(LOWORD(wParam)) {
    case IDM_LINES1:
    flag=1;
    break;
    case IDM_LINES2:
    flag=2;
    break;
    case IDM_CIRCLE:
    flag=3;
    break; 
    /* increment size by 10 each time */
    // case IDM_SIZE:
    case IDM_RESET:
    MoveToEx(hmemdc,0,0,NULL);
    MoveToEx(hmemdc2,0,0,NULL);
    PatBlt(hmemdc,0,0,maxX,maxY,PATCOPY);
    PatBlt(hmemdc2,0,0,maxX,maxY,PATCOPY);
    InvalidateRect(hwnd,NULL,1);
    break;
    // case IDM_ELLIPSES: { // break;
    // }
    }
    break;
    case WM_MOUSEMOVE:

    if(flag==3){
    if (!StartDraw) 
    break;
    nLeft=xS-r;
    nTop=yS-r;
    nRight=xS+r;
    nBottom=yS+r;

    BitBlt(hmemdc,nLeft,nTop,nRight-nLeft+1,nBottom-nTop+1,hmemdc2,nLeft,nTop,SRCCOPY);
    }
    else
    {

    if (!StartDraw) 
    break;
    if ( xS<=xP ) {
    nLeft = xS;
    nRight = xP;
    } else {
    nLeft = xP;
    nRight = xS;
    }
    if ( yS<=yP ) {
    nTop = yS;
    nBottom = yP;
    } else {
    nTop = yP;
    nBottom = yS;
    }
    BitBlt(hmemdc,nLeft,nTop,nRight -nLeft+1,nBottom-nTop+1,hmemdc2,nLeft,nTop,SRCCOPY);
    }


    xP = LOWORD(lParam);  // horizontal position of cursor 
    yP = HIWORD(lParam);  // vertical position of cursor 
    if(flag==3){
    nLeft=xS-r;
    nTop=yS-r;
    nRight=xS+r;
    nBottom=yS+r;
    BitBlt(hmemdc2,nLeft,nTop,nRight-nLeft+1,nBottom-nTop+1,hmemdc,nLeft,nTop,SRCCOPY);
    }
    else
    {

    if ( xS<=xP ) {
    nLeft = xS;
    nRight = xP;
    } else {
    nLeft = xP;
    nRight = xS;
    }
    if ( yS<=yP ) {
    nTop = yS;
    nBottom = yP;
    } else {
    nTop = yP;
    nBottom = yS;
    }
    BitBlt(hmemdc2,nLeft,nTop,nRight-nLeft+1,nBottom-nTop+1,hmemdc,nLeft,nTop,SRCCOPY);
    }
    switch(flag){ case 1:
    linedda(hmemdc,xS,yS,xP,yP);
    break;
    case 2:
    MidPointLine(hmemdc,xS,yS,xP,yP);
    break;
    case 3:
    //MidPointLine(hmemdc,xS,yS,xP,yP);
    circleMidpoint(hmemdc,xS,yS,xP,yP);
    break; }


    InvalidateRect(hwnd,NULL,0); break;

    case WM_LBUTTONDOWN:
    xP = xS = LOWORD(lParam);  // horizontal position of cursor 
    yP = yS = HIWORD(lParam);  // vertical position of cursor 
    StartDraw = 1;
    break;
    case WM_LBUTTONUP:
    StartDraw = 0;
    if(flag==3)
    {
    r=0;
    }
    break;
    case IDM_EXIT:
    response = MessageBox(hwnd,"Quit the Program?","Ext",MB_YESNO);
    if(response == IDYES)
    PostQuitMessage(0);
    break;
    case WM_PAINT:
    hdc = BeginPaint(hwnd,&ps); /* get DC */
    // pr = &ps.rcPaint; /* set mapping mode, window and viewport extents */
    SetMapMode(hdc,MM_ANISOTROPIC);
    SetWindowExtEx(hdc,100,100,NULL);
    SetViewportExtEx(hdc,100,100,NULL);
    SetViewportOrgEx(hdc,orgX,orgY,NULL); /* now,copy memory image onto screen */
    //BitBlt(hdc,pr->left,pr->top,pr->right-pr->left,pr->bottom-pr->top,hmemdc,pr->left,pr->top,SRCCOPY);
    BitBlt(hdc,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    EndPaint(hwnd,&ps); /* release DC */
    break;
    case WM_DESTROY: /* terminate the program */ SelectObject(hmemdc,hOldbrush);
    DeleteObject(hbrush);
    // DeleteObject(hWhitePen);
    SelectObject(hmemdc,hOldBit);
    DeleteObject(hbit);
    DeleteDC(hmemdc); SelectObject(hmemdc2,hOldBit2);
    DeleteDC(hmemdc2);
    DeleteObject(hbit2); PostQuitMessage(0);
    break;
    default:
    /* Let Windows 98 process any messages not specified in the preceding switch statement. */
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
    return 0;
    }用双缓冲画圆有问题,有什么办法解决吗,用我的思路来解决.............
    谢谢各位..................
      

  3.   

    程序在下面链接:
    http://fyr000000.ys168.com/
    密码:000000 
    名字:复件 DrawLine2.rar
      

  4.   

    各位大虾,不好意思,我又改过一点,
    程序在下面链接:
    http://fyr000000.ys168.com/
    密码:000000 
    名字:02122898(原始版).rar老问题还是有,现在又出现了新问题,为什么线加粗,就会出现奇异情况,错在哪里????实在看不懂啊。
      

  5.   

    我改后的MappingMode.cpp文件,覆盖到文件夹下,思路等一下再写#include <windows.h>
    #include <math.h>static int line_wide=1;
    static int line_shape=1;
    static float r=0.0;
    int round(float x)
    {
    int y;
    if(x-int(x)>=0.5)
    { y=int(x)+1;
    return y;
    }
    else{
    y=int(x);
    return y;
    }
    }
    void SetPixel(HDC hdc,int x0,int y0,int color,int flag)
    {
    switch(flag)
    {
    int i,j;
    case 1:SetPixel(hdc,x0,y0,color);break;
    case 3:for (i=-1;i<=1;i++)
    {
    for (j=-1;j<=1;j++)
    {
    SetPixel(hdc,x0+i,y0+j,color);
    }
    }
    break;
    case 5:for (i=-2;i<=2;i++)
       {
       for (j=-2;j<=2;j++)
       {
       SetPixel(hdc,x0+i,y0+j,color);
       }
       }
       break;
    }
    }void SetPixel(HDC hdc,int x0,int y0,int color,int flag,int n)
    {
    static int i=1;
    if(i%n==0)
    {
    SetPixel(hdc,x0,y0,color,flag);
    }
    i++;
    }
    void circlePlotPoints(HDC hdc,int xc,int yc,int x,int y)
    {
    SetPixel(hdc,xc+x,yc+y,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc-x,yc+y,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc+x,yc-y,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc-x,yc-y,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc+y,yc+x,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc-y,yc+x,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc+y,yc-x,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc-y,yc-x,RGB(0,0,255),line_wide,line_shape);
    }
    //void circleMidpoint(HDC hdc,int xc,int yc,int r)
    void circleMidpoint(HDC hdc,int xc,int yc,int xp,int yp)
    {
     
     r=sqrt((float)((xc-xp)*(xc-xp)+(yc-yp)*(yc-yp)));
    float x=0.0;
    float y=r;
    float d=1-r;
    circlePlotPoints(hdc,xc,yc,x,y);
    while(x<y)
    {
    x++;
    if(d<0)
    d+=2*x+1;
    else
    {
    y--;
    d+=2*(x-y)+1;
    }
    circlePlotPoints(hdc,xc,yc,x,y);
    }
    }
    void linedda(HDC hdc,int x1,int y1,int x2,int y2)
    {
    float increx,increy,x,y,length;
    int i;
    if(abs(x2-x1)>abs(y2-y1))
    length=abs(x2-x1);
    else
    length=abs(y2-y1);
    increx=(x2-x1)/length;
    increy=(y2-y1)/length;
    x=x1;
    y=y1;
    for(i=1;i<=length;i++)
    {
    SetPixel(hdc,round(x),round(y),RGB(255,0,0),line_wide,line_shape);
    x=x+increx;
    y=y+increy;
    }
    }
    int nwidth=1;void MidPointLine(HDC hdc,int x0, int y0,int x1, int y1)
    {
    int a,b,delta1,delta2,d,x,y,dx,dy,i,s1,s2,width,count;//
    width=nwidth/2;
    a=y0-y1;
    b=x1-x0; x=x0;
    y=y0;
    SetPixel(hdc,x,y,RGB(0,255,0),line_wide,line_shape);
    dx=abs(b);
    dy=abs(a);
    if(b>0)
    s1=1;
    else
    s1=-1;
    if(a<0)
    s2=1;
    else
    s2=-1;
    if(s1<0)
    a=-a;
    if(s2<0)
    b=-b; if(dy<=dx)
    {
    d=a+a+b;
    delta1=a+a;
    delta2=delta1+b+b;
    if(s1==s2)
    {
    for(i=0;i<dx;i++)
    {
    if(d<0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    x+=s1;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x,y+count,RGB(0,255,0),line_wide,line_shape);
    SetPixel(hdc,x,y-count,RGB(0,255,0),line_wide,line_shape);
    }
    }
    }
    else
    {
    for(i=0;i<dx;i++)
    {
    if(d>=0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    x+=s1;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x,y+count,RGB(0,255,0),line_wide,line_shape);
    SetPixel(hdc,x,y-count,RGB(0,255,0),line_wide,line_shape);
    }
    }
    }
    }
    else
    {
    d=a+b+b;
    delta1=b+b;
    delta2=delta1+a+a;
    if(s1==s2)
    {
    for(i=0;i<dy;i++)
    {
    if(d>0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    y+=s2;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x+count,y,RGB(0,255,0),line_wide,line_shape);
    SetPixel(hdc,x-count,y,RGB(0,255,0),line_wide,line_shape);
    }
    }
    }
    else
    {
    for(i=0;i<dy;i++)
    {
    if(d<0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    y+=s2;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x+count,y,RGB(0,255,0),line_wide,line_shape);
    SetPixel(hdc,x-count,y,RGB(0,255,0),line_wide,line_shape);
    }
    }
    }
    }
    }
    /*void MidPointLine(HDC hdc,int x1,int y1,int x2,int y2)
    {
    int dx,dy,n,k,i,f;
    int x,y;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    n=dx+dy;
    if(x2>x1){k=y2>=y1?1:4;x=x1;y=y1;}
    else {k=y2>=y1?2:3;x=x1;y=y1;}
    SetPixel(hdc,x,y,RGB(0,255,0),line_wide);
    for(i=0,f=0;i<n;i++)
    if(f>=0)
    switch(k){
    case 1:SetPixel(hdc,x++,y,RGB(0,255,0),line_wide);f-=dy;break;
    case 2:SetPixel(hdc,x,y++,RGB(0,255,0),line_wide);f-=dx;break;
    case 3:SetPixel(hdc,x--,y,RGB(0,255,0),line_wide);f-=dy;break;
    case 4:SetPixel(hdc,x,y--,RGB(0,255,0),line_wide);f-=dx;break;
    }
    else
    switch(k){
    case 1:SetPixel(hdc,x,y++,RGB(0,255,0),line_wide);f+=dx;break;
    case 2:SetPixel(hdc,x--,y,RGB(0,255,0),line_wide);f+=dy;break;
    case 3:SetPixel(hdc,x,y--,RGB(0,255,0),line_wide);f+=dx;break;
    case 4:SetPixel(hdc,x++,y,RGB(0,255,0),line_wide);f+=dy;break;
    }
    }*/
    #include "graph.h"
    #include "MappingMode.h"
    static int flag=1;LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);char szWinName[] = "MyWin"; /* name of window class */char str[255]; /* holds output strings */int maxX,maxY; /* screen dimensions */
    int orgX=0,orgY=0; /* viewport orgin */HDC hmemdc; /* store the virtual device handle */
    HBITMAP hbit; /* store the virtual bitmap */
    HBRUSH hbrush; /* store the brush handle */
    HGDIOBJ hOldBit,hOldbrush;HDC hdc;
    /* create pens */
    HPEN hOldpen; /* handle of old pen */
    //HPEN hWhitePen;
    HWND hwnd;HDC hmemdc2;
    HDC hmemdc3;HBITMAP hbit2,hOldBit2;
    int xP,yP,xS,yS;
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs,int nWinMode)
    {
    MSG msg;
    WNDCLASSEX wcl;
    HACCEL hAccel; /* Define a window class. */
    wcl.cbSize = sizeof(WNDCLASSEX);
    wcl.hInstance = hThisInst; /* handle to this instance */
    wcl.lpszClassName = szWinName; /* window class name */
    wcl.lpfnWndProc = WindowFunc; /* window function */
    wcl.style = 0; /* default style */ wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION); /* standard icon */
    wcl.hIconSm = LoadIcon(NULL,IDI_WINLOGO); /* SMALL ICON */
    wcl.hCursor = LoadCursor(NULL,IDC_ARROW); /* cursor style */ /* Specify name of menu resource. This will be overridden. */
    wcl.lpszMenuName = "MYMENU"; /* main menu */
    wcl.cbClsExtra = 0; /* no extra information needed */
    wcl.cbWndExtra = 0; /* no extra information needed */

    /* Make the windows backgraoud white */
    wcl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); /* Register the window class. */
    if(!RegisterClassEx(&wcl))
    return 0; /* Now that a window class has been registered, a window can be created. */
    hwnd = CreateWindow(
    szWinName, /* name of window class */
    "Changing Mapping Modes", /* title */
    WS_VISIBLE|WS_CAPTION|WS_BORDER|WS_MINIMIZEBOX|WS_SYSMENU, /* window style - normal */
    CW_USEDEFAULT, /* X coordinate - let Winodws decide */
    CW_USEDEFAULT, /* Y coordinate - let Windows decide */
    CW_USEDEFAULT, /* width - let Windows decide */
    CW_USEDEFAULT, /* height - let Windows decide */
    HWND_DESKTOP, /* no parent window */
    NULL,  /*  no menu */
    hThisInst, /* handle of this instance of the program */
    NULL /* no additional arguments */
    ); /* Load accelerators. */
    hAccel = LoadAccelerators(hThisInst,"MyMenu"); /* Display the windows. */
    ShowWindow(hwnd, nWinMode);
    UpdateWindow(hwnd);
      

  6.   


    /* Createthe message loop. */
    while(GetMessage(&msg, NULL, 0,0 )) {
    if(!TranslateAccelerator(hwnd,hAccel,&msg)) {
    TranslateMessage(&msg); /* translate keyboard messages */
    DispatchMessage(&msg); /* return control to Windows 98 */
    }
    }
    return msg.wParam;
    }
    /* This function is called by Windows 98 and is passed 
    messages from the message gueue.
    */
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
    {
    PAINTSTRUCT ps;
    RECT *pr;
    int response;
    static StartDraw = 0;
    int nTop,nLeft,nRight,nBottom,nPositionF,nPositionS;
    switch(message) {
    // case WM_HELP: /* user pressed F1 or used ? button */
    //
    // WinHelp((HWND)((LPHELPINFO) lParam) -> hItemHandle,
    // "ABC.hlp",HELP_WM_HELP,
    // (DWORD)IDM_HELP);
    // return 1;
    // case IDM_HELPTHIS:
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,
    // (DWORD)IDH_MENUABOUT);
    // break;
    // case IDM_EXIT:
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,
    // (DWORD)IDH_MENUEXIT);
    // break;
    // default:
    /* menu bar selected,but no option heghighted */
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,(DWORD)IDH_MENUMAIN);
    // }
    // }
    // else
    /* standard help for main window */
    // WinHelp(hwnd,"helptest.hlp",HELP_KEY,(DWORD)"Main Window");
    /// break;
    // case WM_CONTEXTMENU: /* user right - clicked mouse */
    /* context help about main window */
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,IDH_MAIN);
    // break; case WM_CREATE: { /*load the bitmap */

    /* get screen coordinates */
    maxX = GetSystemMetrics(SM_CXSCREEN);
    maxY = GetSystemMetrics(SM_CYSCREEN); /* make a compatible memory image */
    hdc = GetDC(hwnd);
    hmemdc = CreateCompatibleDC(hdc);

    hbit = CreateCompatibleBitmap(hdc,maxX,maxY);
    hOldBit = SelectObject(hmemdc,hbit);
    hbrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
    hOldbrush = SelectObject(hmemdc,hbrush);
    PatBlt(hmemdc,0,0,maxX,maxY,PATCOPY);//非常重要,要么屏幕会黑
    // hWhitePen = (HPEN) GetStockObject(WHITE_PEN);//利用双缓冲区
    hmemdc2 = CreateCompatibleDC(hdc);
    // hmemdc3 = CreateCompatibleDC(hdc);
    hbit2 = CreateCompatibleBitmap(hdc,maxX,maxY);
    hOldBit2 = (HBITMAP)SelectObject(hmemdc2,hbit2);


    PatBlt(hmemdc2,0,0,maxX,maxY,PATCOPY);//非常重要,要么屏幕会黑
    /* save default pen */
    // hOldpen = (HPEN)SelectObject(memdc,hRedpen);
    // SelelctObject(memdc,hOldpen);
    ReleaseDC(hwnd,hdc);
    break;
    }
    case WM_COMMAND: switch(LOWORD(wParam)) {
    case IDM_HELP:
    WinHelp(hwnd,"abc.hlp",HELP_FINDER,0);
    break;
    case ID_1:
    line_wide=1;
    break;
    case ID_2:
    line_wide=3;
    break;
    case ID_3:
    line_wide=5;
    break;
    case ID_4:
    line_shape=1;
    break;
    case ID_5:
    line_shape=18;
    break;
    case ID_6:
    line_shape=35;
    break;
    case IDM_LINES1:
    flag=1;
    break;
    case IDM_LINES2:
    flag=2;
    break;
    case IDM_CIRCLE:
    flag=3;
    break; 

    /* increment size by 10 each time */
    // case IDM_SIZE:
    case IDM_RESET:
    MoveToEx(hmemdc,0,0,NULL);
    MoveToEx(hmemdc2,0,0,NULL);
    PatBlt(hmemdc,0,0,maxX,maxY,PATCOPY);
    PatBlt(hmemdc2,0,0,maxX,maxY,PATCOPY);
    InvalidateRect(hwnd,NULL,1);
    break;
    case IDM_EXIT:
    response = MessageBox(hwnd,"Quit the Program?","Exit",MB_YESNO);
    if(response == IDYES)
    PostQuitMessage(0);
    break;
    // case IDM_ELLIPSES: { // break;
    // }
    }
    break;
    case WM_MOUSEMOVE:

    if(flag==3)
    {
    if (!StartDraw) 
    break;
    nLeft=xS-r;
    nTop=yS-r;
    nRight=xS+r;
    nBottom=yS+r;

    //BitBlt(hmemdc,nLeft,nTop,nRight-nLeft+1,nBottom-nTop+1,hmemdc2,nLeft,nTop,SRCCOPY);
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);

    }
    else
    {

    if (!StartDraw) 
    break;
    if ( xS<=xP ) 
    {
    nLeft = xS;
    nRight = xP;
    } else
    {
    nLeft = xP;
    nRight = xS;
    }
    if ( yS<=yP ) 
    {
    nTop = yS;
    nBottom = yP;

    else 
    {
    nTop = yP;
    nBottom = yS;
    }
    // BitBlt(hmemdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    }


    xP = LOWORD(lParam);  // horizontal position of cursor 
    yP = HIWORD(lParam);  // vertical position of cursor 
    if(flag==3)
    {
    nLeft=xS-r;
    nTop=yS-r;
    nRight=xS+r;
    nBottom=yS+r;
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    }
    else
    {

    if ( xS<=xP ) 
    {
    nLeft = xS;
    nRight = xP;

    else
    {
    nLeft = xP;
    nRight = xS;
    }
    if ( yS<=yP )
    {
    nTop = yS;
    nBottom = yP;

    else 
    {
    nTop = yP;
    nBottom = yS;
    }
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    }

    if (!StartDraw) 
    break;
    BitBlt(hmemdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    switch(flag){ case 1:
    linedda(hmemdc,xS,yS,xP,yP);
    break;
    case 2:
    MidPointLine(hmemdc,xS,yS,xP,yP);
    break;
    case 3:
    //MidPointLine(hmemdc,xS,yS,xP,yP);
    circleMidpoint(hmemdc,xS,yS,xP,yP);
    break; }


    InvalidateRect(hwnd,NULL,0); break;

    case WM_LBUTTONDOWN:
    xP = xS = LOWORD(lParam);  // horizontal position of cursor 
    yP = yS = HIWORD(lParam);  // vertical position of cursor 
    StartDraw = 1;
    BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);//xjj break;
    case WM_LBUTTONUP:
    BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);//xjj StartDraw = 0;
    if(flag==3)
    {
    r=0;
    }
    break;

    case WM_PAINT:
    hdc = BeginPaint(hwnd,&ps); /* get DC */
    // pr = &ps.rcPaint; /* set mapping mode, window and viewport extents */
    SetMapMode(hdc,MM_ANISOTROPIC);
    SetWindowExtEx(hdc,100,100,NULL);
    SetViewportExtEx(hdc,100,100,NULL);
    SetViewportOrgEx(hdc,orgX,orgY,NULL); /* now,copy memory image onto screen */
    //BitBlt(hdc,pr->left,pr->top,pr->right-pr->left,pr->bottom-pr->top,hmemdc,pr->left,pr->top,NOTSRCERASE);
    // BitBlt(hdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    BitBlt(hdc,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    EndPaint(hwnd,&ps); /* release DC */
    break;
    case WM_DESTROY: /* terminate the program */ SelectObject(hmemdc,hOldbrush);
    DeleteObject(hbrush);
    // DeleteObject(hWhitePen);
    SelectObject(hmemdc,hOldBit);
    DeleteObject(hbit);
    DeleteDC(hmemdc); SelectObject(hmemdc2,hOldBit2);
    DeleteDC(hmemdc2);
    DeleteObject(hbit2); PostQuitMessage(0);
    break;
    default:
    /* Let Windows 98 process any messages not specified in the preceding switch statement. */
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
    return 0;
    }
      

  7.   


    /* Createthe message loop. */
    while(GetMessage(&msg, NULL, 0,0 )) {
    if(!TranslateAccelerator(hwnd,hAccel,&msg)) {
    TranslateMessage(&msg); /* translate keyboard messages */
    DispatchMessage(&msg); /* return control to Windows 98 */
    }
    }
    return msg.wParam;
    }
    /* This function is called by Windows 98 and is passed 
    messages from the message gueue.
    */
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
    {
    PAINTSTRUCT ps;
    RECT *pr;
    int response;
    static StartDraw = 0;
    int nTop,nLeft,nRight,nBottom,nPositionF,nPositionS;
    switch(message) {
    // case WM_HELP: /* user pressed F1 or used ? button */
    //
    // WinHelp((HWND)((LPHELPINFO) lParam) -> hItemHandle,
    // "ABC.hlp",HELP_WM_HELP,
    // (DWORD)IDM_HELP);
    // return 1;
    // case IDM_HELPTHIS:
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,
    // (DWORD)IDH_MENUABOUT);
    // break;
    // case IDM_EXIT:
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,
    // (DWORD)IDH_MENUEXIT);
    // break;
    // default:
    /* menu bar selected,but no option heghighted */
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,(DWORD)IDH_MENUMAIN);
    // }
    // }
    // else
    /* standard help for main window */
    // WinHelp(hwnd,"helptest.hlp",HELP_KEY,(DWORD)"Main Window");
    /// break;
    // case WM_CONTEXTMENU: /* user right - clicked mouse */
    /* context help about main window */
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,IDH_MAIN);
    // break; case WM_CREATE: { /*load the bitmap */

    /* get screen coordinates */
    maxX = GetSystemMetrics(SM_CXSCREEN);
    maxY = GetSystemMetrics(SM_CYSCREEN); /* make a compatible memory image */
    hdc = GetDC(hwnd);
    hmemdc = CreateCompatibleDC(hdc);

    hbit = CreateCompatibleBitmap(hdc,maxX,maxY);
    hOldBit = SelectObject(hmemdc,hbit);
    hbrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
    hOldbrush = SelectObject(hmemdc,hbrush);
    PatBlt(hmemdc,0,0,maxX,maxY,PATCOPY);//非常重要,要么屏幕会黑
    // hWhitePen = (HPEN) GetStockObject(WHITE_PEN);//利用双缓冲区
    hmemdc2 = CreateCompatibleDC(hdc);
    // hmemdc3 = CreateCompatibleDC(hdc);
    hbit2 = CreateCompatibleBitmap(hdc,maxX,maxY);
    hOldBit2 = (HBITMAP)SelectObject(hmemdc2,hbit2);


    PatBlt(hmemdc2,0,0,maxX,maxY,PATCOPY);//非常重要,要么屏幕会黑
    /* save default pen */
    // hOldpen = (HPEN)SelectObject(memdc,hRedpen);
    // SelelctObject(memdc,hOldpen);
    ReleaseDC(hwnd,hdc);
    break;
    }
    case WM_COMMAND: switch(LOWORD(wParam)) {
    case IDM_HELP:
    WinHelp(hwnd,"abc.hlp",HELP_FINDER,0);
    break;
    case ID_1:
    line_wide=1;
    break;
    case ID_2:
    line_wide=3;
    break;
    case ID_3:
    line_wide=5;
    break;
    case ID_4:
    line_shape=1;
    break;
    case ID_5:
    line_shape=18;
    break;
    case ID_6:
    line_shape=35;
    break;
    case IDM_LINES1:
    flag=1;
    break;
    case IDM_LINES2:
    flag=2;
    break;
    case IDM_CIRCLE:
    flag=3;
    break; 

    /* increment size by 10 each time */
    // case IDM_SIZE:
    case IDM_RESET:
    MoveToEx(hmemdc,0,0,NULL);
    MoveToEx(hmemdc2,0,0,NULL);
    PatBlt(hmemdc,0,0,maxX,maxY,PATCOPY);
    PatBlt(hmemdc2,0,0,maxX,maxY,PATCOPY);
    InvalidateRect(hwnd,NULL,1);
    break;
    case IDM_EXIT:
    response = MessageBox(hwnd,"Quit the Program?","Exit",MB_YESNO);
    if(response == IDYES)
    PostQuitMessage(0);
    break;
    // case IDM_ELLIPSES: { // break;
    // }
    }
    break;
    case WM_MOUSEMOVE:

    if(flag==3)
    {
    if (!StartDraw) 
    break;
    nLeft=xS-r;
    nTop=yS-r;
    nRight=xS+r;
    nBottom=yS+r;

    //BitBlt(hmemdc,nLeft,nTop,nRight-nLeft+1,nBottom-nTop+1,hmemdc2,nLeft,nTop,SRCCOPY);
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);

    }
    else
    {

    if (!StartDraw) 
    break;
    if ( xS<=xP ) 
    {
    nLeft = xS;
    nRight = xP;
    } else
    {
    nLeft = xP;
    nRight = xS;
    }
    if ( yS<=yP ) 
    {
    nTop = yS;
    nBottom = yP;

    else 
    {
    nTop = yP;
    nBottom = yS;
    }
    // BitBlt(hmemdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    }


    xP = LOWORD(lParam);  // horizontal position of cursor 
    yP = HIWORD(lParam);  // vertical position of cursor 
    if(flag==3)
    {
    nLeft=xS-r;
    nTop=yS-r;
    nRight=xS+r;
    nBottom=yS+r;
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    }
    else
    {

    if ( xS<=xP ) 
    {
    nLeft = xS;
    nRight = xP;

    else
    {
    nLeft = xP;
    nRight = xS;
    }
    if ( yS<=yP )
    {
    nTop = yS;
    nBottom = yP;

    else 
    {
    nTop = yP;
    nBottom = yS;
    }
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    }

    if (!StartDraw) 
    break;
    BitBlt(hmemdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    switch(flag){ case 1:
    linedda(hmemdc,xS,yS,xP,yP);
    break;
    case 2:
    MidPointLine(hmemdc,xS,yS,xP,yP);
    break;
    case 3:
    //MidPointLine(hmemdc,xS,yS,xP,yP);
    circleMidpoint(hmemdc,xS,yS,xP,yP);
    break; }


    InvalidateRect(hwnd,NULL,0); break;

    case WM_LBUTTONDOWN:
    xP = xS = LOWORD(lParam);  // horizontal position of cursor 
    yP = yS = HIWORD(lParam);  // vertical position of cursor 
    StartDraw = 1;
    BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);//xjj break;
    case WM_LBUTTONUP:
    BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);//xjj StartDraw = 0;
    if(flag==3)
    {
    r=0;
    }
    break;

    case WM_PAINT:
    hdc = BeginPaint(hwnd,&ps); /* get DC */
    // pr = &ps.rcPaint; /* set mapping mode, window and viewport extents */
    SetMapMode(hdc,MM_ANISOTROPIC);
    SetWindowExtEx(hdc,100,100,NULL);
    SetViewportExtEx(hdc,100,100,NULL);
    SetViewportOrgEx(hdc,orgX,orgY,NULL); /* now,copy memory image onto screen */
    //BitBlt(hdc,pr->left,pr->top,pr->right-pr->left,pr->bottom-pr->top,hmemdc,pr->left,pr->top,NOTSRCERASE);
    // BitBlt(hdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    BitBlt(hdc,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    EndPaint(hwnd,&ps); /* release DC */
    break;
    case WM_DESTROY: /* terminate the program */ SelectObject(hmemdc,hOldbrush);
    DeleteObject(hbrush);
    // DeleteObject(hWhitePen);
    SelectObject(hmemdc,hOldBit);
    DeleteObject(hbit);
    DeleteDC(hmemdc); SelectObject(hmemdc2,hOldBit2);
    DeleteDC(hmemdc2);
    DeleteObject(hbit2); PostQuitMessage(0);
    break;
    default:
    /* Let Windows 98 process any messages not specified in the preceding switch statement. */
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
    return 0;
    }
      

  8.   

    这个程序已经改的很不错,但是还有一个BUG,就是Line_shape=18时候用粗线茂密画圆会出现只有右半圆情况。经过几次测试,Line_shape为奇数时不会出现此情况,为偶数就有这种情况。
    感谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    希望能帮我再看一下,分数我会再多给你。
      

  9.   

    #include <windows.h>
    #include <math.h>static int line_wide=1;
    static int line_shape=1;
    static float r=0.0;
    extern  int flag;int round(float x)
    {
    int y;
    if(x-int(x)>=0.5)
    { y=int(x)+1;
    return y;
    }
    else{
    y=int(x);
    return y;
    }
    }
    void SetPixel(HDC hdc,int x0,int y0,int color,int flag)
    {
    switch(flag)
    {
    int i,j;
    case 1:SetPixel(hdc,x0,y0,color);break;
    case 3:for (i=-1;i<=1;i++)
    {
    for (j=-1;j<=1;j++)
    {
    SetPixel(hdc,x0+i,y0+j,color);
    }
    }
    break;
    case 5:for (i=-2;i<=2;i++)
       {
       for (j=-2;j<=2;j++)
       {
       SetPixel(hdc,x0+i,y0+j,color);
       }
       }
       break;
    }
    }
    static int i=1;void SetPixel(HDC hdc,int x0,int y0,int color,int flag,int n)
    {
    if(i%n==0)
    {
    SetPixel(hdc,x0,y0,color,flag);
    }
    if(::flag!=3)
    {
    i++;
    }
    }
    void circlePlotPoints(HDC hdc,int xc,int yc,int x,int y)
    {
    SetPixel(hdc,xc+x,yc+y,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc-x,yc+y,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc+x,yc-y,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc-x,yc-y,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc+y,yc+x,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc-y,yc+x,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc+y,yc-x,RGB(0,0,255),line_wide,line_shape);
    SetPixel(hdc,xc-y,yc-x,RGB(0,0,255),line_wide,line_shape);
    if(::flag==3)
    {
    i++;
    }}
    //void circleMidpoint(HDC hdc,int xc,int yc,int r)
    void circleMidpoint(HDC hdc,int xc,int yc,int xp,int yp)
    {
     
     r=sqrt((float)((xc-xp)*(xc-xp)+(yc-yp)*(yc-yp)));
    float x=0.0;
    float y=r;
    float d=1-r;
    circlePlotPoints(hdc,xc,yc,x,y);
    while(x<y)
    {
    x++;
    if(d<0)
    d+=2*x+1;
    else
    {
    y--;
    d+=2*(x-y)+1;
    }
    circlePlotPoints(hdc,xc,yc,x,y);
    }
    }
    void linedda(HDC hdc,int x1,int y1,int x2,int y2)
    {
    float increx,increy,x,y,length;
    int i;
    if(abs(x2-x1)>abs(y2-y1))
    length=abs(x2-x1);
    else
    length=abs(y2-y1);
    increx=(x2-x1)/length;
    increy=(y2-y1)/length;
    x=x1;
    y=y1;
    for(i=1;i<=length;i++)
    {
    SetPixel(hdc,round(x),round(y),RGB(255,0,0),line_wide,line_shape);
    x=x+increx;
    y=y+increy;
    }
    }
    int nwidth=1;void MidPointLine(HDC hdc,int x0, int y0,int x1, int y1)
    {
    int a,b,delta1,delta2,d,x,y,dx,dy,i,s1,s2,width,count;//
    width=nwidth/2;
    a=y0-y1;
    b=x1-x0; x=x0;
    y=y0;
    SetPixel(hdc,x,y,RGB(0,255,0),line_wide,line_shape);
    dx=abs(b);
    dy=abs(a);
    if(b>0)
    s1=1;
    else
    s1=-1;
    if(a<0)
    s2=1;
    else
    s2=-1;
    if(s1<0)
    a=-a;
    if(s2<0)
    b=-b; if(dy<=dx)
    {
    d=a+a+b;
    delta1=a+a;
    delta2=delta1+b+b;
    if(s1==s2)
    {
    for(i=0;i<dx;i++)
    {
    if(d<0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    x+=s1;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x,y+count,RGB(0,255,0),line_wide,line_shape);
    SetPixel(hdc,x,y-count,RGB(0,255,0),line_wide,line_shape);
    }
    }
    }
    else
    {
    for(i=0;i<dx;i++)
    {
    if(d>=0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    x+=s1;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x,y+count,RGB(0,255,0),line_wide,line_shape);
    SetPixel(hdc,x,y-count,RGB(0,255,0),line_wide,line_shape);
    }
    }
    }
    }
    else
    {
    d=a+b+b;
    delta1=b+b;
    delta2=delta1+a+a;
    if(s1==s2)
    {
    for(i=0;i<dy;i++)
    {
    if(d>0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    y+=s2;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x+count,y,RGB(0,255,0),line_wide,line_shape);
    SetPixel(hdc,x-count,y,RGB(0,255,0),line_wide,line_shape);
    }
    }
    }
    else
    {
    for(i=0;i<dy;i++)
    {
    if(d<0)
    {
    x+=s1;
    y+=s2;
    d+=delta2;
    }
    else
    {
    y+=s2;
    d+=delta1;
    }
    for(count=0;count<=width;count++)
    {
    SetPixel(hdc,x+count,y,RGB(0,255,0),line_wide,line_shape);
    SetPixel(hdc,x-count,y,RGB(0,255,0),line_wide,line_shape);
    }
    }
    }
    }
    }
    /*void MidPointLine(HDC hdc,int x1,int y1,int x2,int y2)
    {
    int dx,dy,n,k,i,f;
    int x,y;
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    n=dx+dy;
    if(x2>x1){k=y2>=y1?1:4;x=x1;y=y1;}
    else {k=y2>=y1?2:3;x=x1;y=y1;}
    SetPixel(hdc,x,y,RGB(0,255,0),line_wide);
    for(i=0,f=0;i<n;i++)
    if(f>=0)
    switch(k){
    case 1:SetPixel(hdc,x++,y,RGB(0,255,0),line_wide);f-=dy;break;
    case 2:SetPixel(hdc,x,y++,RGB(0,255,0),line_wide);f-=dx;break;
    case 3:SetPixel(hdc,x--,y,RGB(0,255,0),line_wide);f-=dy;break;
    case 4:SetPixel(hdc,x,y--,RGB(0,255,0),line_wide);f-=dx;break;
    }
    else
    switch(k){
    case 1:SetPixel(hdc,x,y++,RGB(0,255,0),line_wide);f+=dx;break;
    case 2:SetPixel(hdc,x--,y,RGB(0,255,0),line_wide);f+=dy;break;
    case 3:SetPixel(hdc,x,y--,RGB(0,255,0),line_wide);f+=dx;break;
    case 4:SetPixel(hdc,x++,y,RGB(0,255,0),line_wide);f+=dy;break;
    }
    }*/
      

  10.   

    #include "graph.h"
    #include "MappingMode.h"
    static int flag=1;LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);char szWinName[] = "MyWin"; /* name of window class */char str[255]; /* holds output strings */int maxX,maxY; /* screen dimensions */
    int orgX=0,orgY=0; /* viewport orgin */HDC hmemdc; /* store the virtual device handle */
    HBITMAP hbit; /* store the virtual bitmap */
    HBRUSH hbrush; /* store the brush handle */
    HGDIOBJ hOldBit,hOldbrush;HDC hdc;
    /* create pens */
    HPEN hOldpen; /* handle of old pen */
    //HPEN hWhitePen;
    HWND hwnd;HDC hmemdc2;
    HDC hmemdc3;HBITMAP hbit2,hOldBit2;
    int xP,yP,xS,yS;
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs,int nWinMode)
    {
    MSG msg;
    WNDCLASSEX wcl;
    HACCEL hAccel; /* Define a window class. */
    wcl.cbSize = sizeof(WNDCLASSEX);
    wcl.hInstance = hThisInst; /* handle to this instance */
    wcl.lpszClassName = szWinName; /* window class name */
    wcl.lpfnWndProc = WindowFunc; /* window function */
    wcl.style = 0; /* default style */ wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION); /* standard icon */
    wcl.hIconSm = LoadIcon(NULL,IDI_WINLOGO); /* SMALL ICON */
    wcl.hCursor = LoadCursor(NULL,IDC_ARROW); /* cursor style */ /* Specify name of menu resource. This will be overridden. */
    wcl.lpszMenuName = "MYMENU"; /* main menu */
    wcl.cbClsExtra = 0; /* no extra information needed */
    wcl.cbWndExtra = 0; /* no extra information needed */

    /* Make the windows backgraoud white */
    wcl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); /* Register the window class. */
    if(!RegisterClassEx(&wcl))
    return 0; /* Now that a window class has been registered, a window can be created. */
    hwnd = CreateWindow(
    szWinName, /* name of window class */
    "Changing Mapping Modes", /* title */
    WS_VISIBLE|WS_CAPTION|WS_BORDER|WS_MINIMIZEBOX|WS_SYSMENU, /* window style - normal */
    CW_USEDEFAULT, /* X coordinate - let Winodws decide */
    CW_USEDEFAULT, /* Y coordinate - let Windows decide */
    CW_USEDEFAULT, /* width - let Windows decide */
    CW_USEDEFAULT, /* height - let Windows decide */
    HWND_DESKTOP, /* no parent window */
    NULL,  /*  no menu */
    hThisInst, /* handle of this instance of the program */
    NULL /* no additional arguments */
    ); /* Load accelerators. */
    hAccel = LoadAccelerators(hThisInst,"MyMenu"); /* Display the windows. */
    ShowWindow(hwnd, nWinMode);
    UpdateWindow(hwnd);/* Createthe message loop. */
    while(GetMessage(&msg, NULL, 0,0 )) {
    if(!TranslateAccelerator(hwnd,hAccel,&msg)) {
    TranslateMessage(&msg); /* translate keyboard messages */
    DispatchMessage(&msg); /* return control to Windows 98 */
    }
    }
    return msg.wParam;
    }
    /* This function is called by Windows 98 and is passed 
    messages from the message gueue.
    */
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
    {
    PAINTSTRUCT ps;
    RECT *pr;
    int response;
    static StartDraw = 0;
    int nTop,nLeft,nRight,nBottom,nPositionF,nPositionS;
    switch(message) {
    // case WM_HELP: /* user pressed F1 or used ? button */
    //
    // WinHelp((HWND)((LPHELPINFO) lParam) -> hItemHandle,
    // "ABC.hlp",HELP_WM_HELP,
    // (DWORD)IDM_HELP);
    // return 1;
    // case IDM_HELPTHIS:
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,
    // (DWORD)IDH_MENUABOUT);
    // break;
    // case IDM_EXIT:
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,
    // (DWORD)IDH_MENUEXIT);
    // break;
    // default:
    /* menu bar selected,but no option heghighted */
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,(DWORD)IDH_MENUMAIN);
    // }
    // }
    // else
    /* standard help for main window */
    // WinHelp(hwnd,"helptest.hlp",HELP_KEY,(DWORD)"Main Window");
    /// break;
    // case WM_CONTEXTMENU: /* user right - clicked mouse */
    /* context help about main window */
    // WinHelp(hwnd,"helptest.hlp",HELP_CONTEXTPOPUP,IDH_MAIN);
    // break; case WM_CREATE: { /*load the bitmap */

    /* get screen coordinates */
    maxX = GetSystemMetrics(SM_CXSCREEN);
    maxY = GetSystemMetrics(SM_CYSCREEN); /* make a compatible memory image */
    hdc = GetDC(hwnd);
    hmemdc = CreateCompatibleDC(hdc);

    hbit = CreateCompatibleBitmap(hdc,maxX,maxY);
    hOldBit = SelectObject(hmemdc,hbit);
    hbrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
    hOldbrush = SelectObject(hmemdc,hbrush);
    PatBlt(hmemdc,0,0,maxX,maxY,PATCOPY);//非常重要,要么屏幕会黑
    // hWhitePen = (HPEN) GetStockObject(WHITE_PEN);//利用双缓冲区
    hmemdc2 = CreateCompatibleDC(hdc);
    // hmemdc3 = CreateCompatibleDC(hdc);
    hbit2 = CreateCompatibleBitmap(hdc,maxX,maxY);
    hOldBit2 = (HBITMAP)SelectObject(hmemdc2,hbit2);


    PatBlt(hmemdc2,0,0,maxX,maxY,PATCOPY);//非常重要,要么屏幕会黑
    /* save default pen */
    // hOldpen = (HPEN)SelectObject(memdc,hRedpen);
    // SelelctObject(memdc,hOldpen);
    ReleaseDC(hwnd,hdc);
    break;
    }
    case WM_COMMAND: switch(LOWORD(wParam)) {
    case IDM_HELP:
    WinHelp(hwnd,"abc.hlp",HELP_FINDER,0);
    break;
    case ID_1:
    line_wide=1;
    break;
    case ID_2:
    line_wide=3;
    break;
    case ID_3:
    line_wide=5;
    break;
    case ID_4:
    line_shape=1;
    break;
    case ID_5:
    line_shape=18;
    break;
    case ID_6:
    line_shape=35;
    break;
    case IDM_LINES1:
    flag=1;
    break;
    case IDM_LINES2:
    flag=2;
    break;
    case IDM_CIRCLE:
    flag=3;
    break; 

    /* increment size by 10 each time */
    // case IDM_SIZE:
    case IDM_RESET:
    MoveToEx(hmemdc,0,0,NULL);
    MoveToEx(hmemdc2,0,0,NULL);
    PatBlt(hmemdc,0,0,maxX,maxY,PATCOPY);
    PatBlt(hmemdc2,0,0,maxX,maxY,PATCOPY);
    InvalidateRect(hwnd,NULL,1);
    break;
    case IDM_EXIT:
    response = MessageBox(hwnd,"Quit the Program?","Exit",MB_YESNO);
    if(response == IDYES)
    PostQuitMessage(0);
    break;
    // case IDM_ELLIPSES: { // break;
    // }
    }
    break;
    case WM_MOUSEMOVE:

    if(flag==3)
    {
    if (!StartDraw) 
    break;
    nLeft=xS-r;
    nTop=yS-r;
    nRight=xS+r;
    nBottom=yS+r;

    //BitBlt(hmemdc,nLeft,nTop,nRight-nLeft+1,nBottom-nTop+1,hmemdc2,nLeft,nTop,SRCCOPY);
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
      

  11.   

    }
    else
    {

    if (!StartDraw) 
    break;
    if ( xS<=xP ) 
    {
    nLeft = xS;
    nRight = xP;
    } else
    {
    nLeft = xP;
    nRight = xS;
    }
    if ( yS<=yP ) 
    {
    nTop = yS;
    nBottom = yP;

    else 
    {
    nTop = yP;
    nBottom = yS;
    }
    // BitBlt(hmemdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    }


    xP = LOWORD(lParam);  // horizontal position of cursor 
    yP = HIWORD(lParam);  // vertical position of cursor 
    if(flag==3)
    {
    nLeft=xS-r;
    nTop=yS-r;
    nRight=xS+r;
    nBottom=yS+r;
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    }
    else
    {

    if ( xS<=xP ) 
    {
    nLeft = xS;
    nRight = xP;

    else
    {
    nLeft = xP;
    nRight = xS;
    }
    if ( yS<=yP )
    {
    nTop = yS;
    nBottom = yP;

    else 
    {
    nTop = yP;
    nBottom = yS;
    }
    // BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    }

    if (!StartDraw) 
    break;
    BitBlt(hmemdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    switch(flag){ case 1:
    linedda(hmemdc,xS,yS,xP,yP);
    break;
    case 2:
    MidPointLine(hmemdc,xS,yS,xP,yP);
    break;
    case 3:
    //MidPointLine(hmemdc,xS,yS,xP,yP);
    circleMidpoint(hmemdc,xS,yS,xP,yP);
    break; }


    InvalidateRect(hwnd,NULL,0); break;

    case WM_LBUTTONDOWN:
    xP = xS = LOWORD(lParam);  // horizontal position of cursor 
    yP = yS = HIWORD(lParam);  // vertical position of cursor 
    StartDraw = 1;
    BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);//xjj break;
    case WM_LBUTTONUP:
    BitBlt(hmemdc2,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);//xjj StartDraw = 0;
    if(flag==3)
    {
    r=0;
    }
    break;

    case WM_PAINT:
    hdc = BeginPaint(hwnd,&ps); /* get DC */
    // pr = &ps.rcPaint; /* set mapping mode, window and viewport extents */
    SetMapMode(hdc,MM_ANISOTROPIC);
    SetWindowExtEx(hdc,100,100,NULL);
    SetViewportExtEx(hdc,100,100,NULL);
    SetViewportOrgEx(hdc,orgX,orgY,NULL); /* now,copy memory image onto screen */
    //BitBlt(hdc,pr->left,pr->top,pr->right-pr->left,pr->bottom-pr->top,hmemdc,pr->left,pr->top,NOTSRCERASE);
    // BitBlt(hdc,0,0,maxX,maxY,hmemdc2,0,0,SRCCOPY);
    BitBlt(hdc,0,0,maxX,maxY,hmemdc,0,0,SRCCOPY);
    EndPaint(hwnd,&ps); /* release DC */
    break;
    case WM_DESTROY: /* terminate the program */ SelectObject(hmemdc,hOldbrush);
    DeleteObject(hbrush);
    // DeleteObject(hWhitePen);
    SelectObject(hmemdc,hOldBit);
    DeleteObject(hbit);
    DeleteDC(hmemdc); SelectObject(hmemdc2,hOldBit2);
    DeleteDC(hmemdc2);
    DeleteObject(hbit2); PostQuitMessage(0);
    break;
    default:
    /* Let Windows 98 process any messages not specified in the preceding switch statement. */
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
    return 0;
    }////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////
    /*
    我的方法是,在MappingMode.cpp中static float r=0.0;行的后面加一条语句extern  int flag;把void SetPixel(HDC hdc,int x0,int y0,int color,int flag,int n)中的static int i=1;定义为//全局静态变量,即把static int i=1;这一句移出void SetPixel(HDC hdc,int x0,int y0,int color,int flag,int n),把其中的i++;改为
    if(::flag!=3)
    {
    i++;
    }
    在void circlePlotPoints(HDC hdc,int xc,int yc,int x,int y)最后加上一句
    if(::flag==3)
    {
    i++;
    }*/