Windows编程 - 正弦曲线

#include  < windows.h >
#include 
< math.h >

LRESULT __stdcall WndProc(HWND, UINT, WPARAM, LPARAM);

int  __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,  int  nShowCmd)
ExpandedBlockStart.gifContractedBlock.gif
{
    
char szWindowClass[] = "SineWave";

    
// 注册窗口类
    WNDCLASSEX wcex;

    wcex.cbSize        
= sizeof(WNDCLASSEX); 
    wcex.style        
= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wcex.lpfnWndProc    
= (WNDPROC)WndProc;
    wcex.cbClsExtra        
= 0;
    wcex.cbWndExtra        
= 0;
    wcex.hInstance        
= hInstance;
    wcex.hIcon        
= NULL;
    wcex.hCursor        
= LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    
= (HBRUSH)::GetStockObject(WHITE_BRUSH);
    wcex.lpszMenuName    
= NULL;
    wcex.lpszClassName    
= szWindowClass;
    wcex.hIconSm        
= NULL;

    ::RegisterClassEx(
&wcex);


    
// 创建并线程主窗口
    HWND hWnd = ::CreateWindowEx( 
        WS_EX_CLIENTEDGE,    
// 扩展样式
        szWindowClass,        // 类名
        "正弦线",        // 标题
        WS_OVERLAPPEDWINDOW,    // 窗口样式
        CW_USEDEFAULT,    // 初始 X 坐标
        CW_USEDEFAULT,    // 初始 X 坐标
        CW_USEDEFAULT,    // 宽度
        CW_USEDEFAULT,    // 高度
        NULL,        // 父窗口句柄
        NULL,            // 菜单句柄
        hInstance,    // 程序实例句柄
        NULL);     

    ::ShowWindow(hWnd, nShowCmd);
    ::UpdateWindow(hWnd);

    
// 进入消息循环
    MSG msg;
    
while(::GetMessage(&msg, NULL, 00))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        ::TranslateMessage(
&msg);
        ::DispatchMessage(
&msg); 
    }



    
return 1;
}



//  消息处理函数     //  03SineWave工程下
LRESULT __stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
ExpandedBlockStart.gifContractedBlock.gif
{
#define SEGMENTS 500  // 取的点数(在一个周期内取500个点)
#define PI 3.1415926  // 圆周率

    HDC hdc;
    PAINTSTRUCT ps;
    RECT rt;
    
int cxClient, cyClient;
    POINT pt[SEGMENTS];
    
int i;
    
    
switch(message)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    
case WM_PAINT:
        hdc 
= ::BeginPaint(hWnd, &ps);
        ::GetClientRect(hWnd, 
&rt);
        cxClient 
= rt.right - rt.left;
        cyClient 
= rt.bottom - rt.top;

        
// 画横坐标轴
        ::MoveToEx(hdc, 0, cyClient/2, NULL);
        ::LineTo(hdc, cxClient, cyClient
/2);
        
// 找出500个点的坐标
        for(i=0; i<SEGMENTS; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            pt[i].x 
= cxClient*i/SEGMENTS;
            pt[i].y 
= (int)((cyClient/2)*(1 - sin(2*PI*i/SEGMENTS)));
        }

        
// 将各点连在一起
        ::Polyline(hdc, pt, SEGMENTS);

        ::EndPaint(hWnd, 
&ps);
        
break;
    
case WM_DESTROY:
        ::PostQuitMessage(
0);
        
break;
    }

    
return ::DefWindowProc(hWnd, message, wParam, lParam);
}

转载于:https://www.cnblogs.com/answeryi/archive/2009/07/12/1522101.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值