旋轉的風車VC++執行個體代碼(Win32應用程式)

來源:互聯網
上載者:User

旋轉的風車-VC++執行個體代碼(Win32應用程式)

//來源程式如下
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PI 3.1415926

int nNum=0,nMaxNum=20;

LRESULT CALLBACK WindowProc(
HWND hwnd,      // handle to window
UINT uMsg,      // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam   // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance,      // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine,          // command line
int nCmdShow              // show state
)

{
    HWND hwnd;
    MSG Msg;
    WNDCLASS wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=WindowProc;
    wndclass.lpszClassName="abc";
    wndclass.lpszMenuName=NULL;
    wndclass.style=0;

    RegisterClass(&wndclass);

    hwnd=CreateWindow("abc","旋轉的風車",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,600,450,NULL,NULL,hInstance,NULL);

    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg,hwnd,NULL,0))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return 0;
}

LRESULT CALLBACK WindowProc(
HWND hwnd,      // handle to window
UINT uMsg,      // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam   // second message parameter
)
{
    HDC hdc;
    HBRUSH hBrush;
    HPEN hp;
    PAINTSTRUCT ps;
    int nCenterX,nCenterY;
    double fAngle;

    switch(uMsg)
    {
    case WM_PAINT:
        hdc=BeginPaint(hwnd,&ps);
        SetMapMode(hdc,MM_ANISOTROPIC);
        SetWindowExtEx(hdc,400,300,NULL);
        SetViewportExtEx(hdc,600,450,NULL);
        SetViewportOrgEx(hdc,300,200,NULL);

//        繪製外圓
        hp=CreatePen(PS_SOLID,1,RGB(255,0,255));
        SelectObject(hdc,hp);
        Ellipse(hdc,-100,-100,100,100);

//        繪製風車的葉片
//        繪製紅色的葉片
        hBrush=CreateSolidBrush(RGB(255,0,0));
        SelectObject(hdc,hBrush);
        fAngle=2*PI/nMaxNum*nNum;
        nCenterX=(int)(50*cos(fAngle));
        nCenterY=(int)(50*sin(fAngle));
        Pie(hdc,nCenterX-50,nCenterY-50,nCenterX+50,nCenterY+50,(int)(nCenterX+50*cos(fAngle)),(int)(nCenterY+50*sin(fAngle)),(int)(nCenterX+50*cos(fAngle+PI)),(int)(nCenterY+50*sin(fAngle+PI)));

//        繪製藍色的葉片
        hBrush=CreateSolidBrush(RGB(255,255,0));
        SelectObject(hdc,hBrush);
        nCenterX=(int)(50*cos(fAngle+2*PI/3));
        nCenterY=(int)(50*sin(fAngle+2*PI/3));
        Pie(hdc,nCenterX-50,nCenterY-50,nCenterX+50,nCenterY+50,(int)(nCenterX+50*cos(fAngle+2*PI/3)),(int)(nCenterY+50*sin(fAngle+2*PI/3)),(int)(nCenterX+50*cos(fAngle+PI+2*PI/3)),(int)(nCenterY+50*sin(fAngle+PI+2*PI/3)));
        
//        繪製黃色的葉片
        hBrush=CreateSolidBrush(RGB(0,255,255));
        SelectObject(hdc,hBrush);
        nCenterX=(int)(50*cos(fAngle+4*PI/3));
        nCenterY=(int)(50*sin(fAngle+4*PI/3));
        Pie(hdc,nCenterX-50,nCenterY-50,nCenterX+50,nCenterY+50,(int)(nCenterX+50*cos(fAngle+4*PI/3)),(int)(nCenterY+50*sin(fAngle+4*PI/3)),(int)(nCenterX+50*cos(fAngle+PI+4*PI/3)),(int)(nCenterY+50*sin(fAngle+PI+4*PI/3)));

        nNum++;
        Sleep(100);
        InvalidateRect(hwnd,NULL,1);
        EndPaint(hwnd,&ps);

        return 0;
    case WM_CLOSE:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}

旋轉的風車-VC++執行個體代碼(Win32應用程式)

//來源程式如下
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PI 3.1415926

int nNum=0,nMaxNum=20;

LRESULT CALLBACK WindowProc(
HWND hwnd,      // handle to window
UINT uMsg,      // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam   // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance,      // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine,          // command line
int nCmdShow              // show state
)

{
    HWND hwnd;
    MSG Msg;
    WNDCLASS wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=WindowProc;
    wndclass.lpszClassName="abc";
    wndclass.lpszMenuName=NULL;
    wndclass.style=0;

    RegisterClass(&wndclass);

    hwnd=CreateWindow("abc","旋轉的風車",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,600,450,NULL,NULL,hInstance,NULL);

    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg,hwnd,NULL,0))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return 0;
}

LRESULT CALLBACK WindowProc(
HWND hwnd,      // handle to window
UINT uMsg,      // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam   // second message parameter
)
{
    HDC hdc;
    HBRUSH hBrush;
    HPEN hp;
    PAINTSTRUCT ps;
    int nCenterX,nCenterY;
    double fAngle;

    switch(uMsg)
    {
    case WM_PAINT:
        hdc=BeginPaint(hwnd,&ps);
        SetMapMode(hdc,MM_ANISOTROPIC);
        SetWindowExtEx(hdc,400,300,NULL);
        SetViewportExtEx(hdc,600,450,NULL);
        SetViewportOrgEx(hdc,300,200,NULL);

//        繪製外圓
        hp=CreatePen(PS_SOLID,1,RGB(255,0,255));
        SelectObject(hdc,hp);
        Ellipse(hdc,-100,-100,100,100);

//        繪製風車的葉片
//        繪製紅色的葉片
        hBrush=CreateSolidBrush(RGB(255,0,0));
        SelectObject(hdc,hBrush);
        fAngle=2*PI/nMaxNum*nNum;
        nCenterX=(int)(50*cos(fAngle));
        nCenterY=(int)(50*sin(fAngle));
        Pie(hdc,nCenterX-50,nCenterY-50,nCenterX+50,nCenterY+50,(int)(nCenterX+50*cos(fAngle)),(int)(nCenterY+50*sin(fAngle)),(int)(nCenterX+50*cos(fAngle+PI)),(int)(nCenterY+50*sin(fAngle+PI)));

//        繪製藍色的葉片
        hBrush=CreateSolidBrush(RGB(255,255,0));
        SelectObject(hdc,hBrush);
        nCenterX=(int)(50*cos(fAngle+2*PI/3));
        nCenterY=(int)(50*sin(fAngle+2*PI/3));
        Pie(hdc,nCenterX-50,nCenterY-50,nCenterX+50,nCenterY+50,(int)(nCenterX+50*cos(fAngle+2*PI/3)),(int)(nCenterY+50*sin(fAngle+2*PI/3)),(int)(nCenterX+50*cos(fAngle+PI+2*PI/3)),(int)(nCenterY+50*sin(fAngle+PI+2*PI/3)));
        
//        繪製黃色的葉片
        hBrush=CreateSolidBrush(RGB(0,255,255));
        SelectObject(hdc,hBrush);
        nCenterX=(int)(50*cos(fAngle+4*PI/3));
        nCenterY=(int)(50*sin(fAngle+4*PI/3));
        Pie(hdc,nCenterX-50,nCenterY-50,nCenterX+50,nCenterY+50,(int)(nCenterX+50*cos(fAngle+4*PI/3)),(int)(nCenterY+50*sin(fAngle+4*PI/3)),(int)(nCenterX+50*cos(fAngle+PI+4*PI/3)),(int)(nCenterY+50*sin(fAngle+PI+4*PI/3)));

        nNum++;
        Sleep(100);
        InvalidateRect(hwnd,NULL,1);
        EndPaint(hwnd,&ps);

        return 0;
    case WM_CLOSE:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.