• 学习windows编程 day3 之窗口绘画一:点线绘制


    #include <windows.h>
    #include <math.h>
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    #define PI 3.1415926
    #define NUM 1000
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
        //声明全局数据:类名
        static TCHAR szClassName[] = TEXT("MyWindows");
        HWND hwnd;
        MSG msg;
    
        //注册窗口类
        WNDCLASS wndclass;
    
        wndclass.hInstance = hInstance;
        wndclass.lpszClassName = szClassName;
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.lpfnWndProc = WndProc;
        wndclass.lpszMenuName = NULL;
        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wndclass.style = CS_HREDRAW;
    
        if (!RegisterClass(&wndclass))
        {
            MessageBox(NULL, TEXT("this program must run in Windows NT!"), szClassName, MB_ICONERROR);
            return 0;
        }
    
        hwnd = CreateWindow(
            szClassName,
            TEXT("MyFirstPractice"),
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            NULL,
            NULL,
            hInstance,
            NULL
            );
    
        ShowWindow(hwnd, nShowCmd);
        UpdateWindow(hwnd);
    
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        int i;
        static int padding;
        static int xCount, yCount;    //线条数
        static int xClient, yClient;//窗口大小
    
        switch (message)
        {
        case WM_CREATE:
            padding = 50;
            break;
        case WM_SIZE:
            xClient = LOWORD(lParam);
            yClient = HIWORD(lParam);
    
            xCount = xClient / padding - 1;
            yCount = yClient / padding - 1;
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
    
            GetClientRect(hwnd, &rect);
    //1        //画点
    //         for (i = rect.left; i < rect.right;i++)
    //         {
    //             //画像素点--->成线
    //             //setpixel返回的是个颜色rgb,因为系统有时候会自动用相近颜色替换,效率不高
    //             //setpixelv返回bool类型,效率更高
    //             //但是当用这些函数绘制图形,会快速出入栈,效率不高
    //             SetPixel(hdc, i, 100, RGB(255, 0, 0));
    //         }
    
    //2        //画线
            //movetoex将当前绘图位置移动到某个具体的点,可以获得之前的位置坐标
    //         POINT pt;
    //         MoveToEx(hdc, 100, 50, &pt);    //默认起点是左上角
    //         //LineTo(hdc, pt.x, pt.y);
    //         LineTo(hdc, 150, 100);
    //         LineTo(hdc, 300, 400);
    // 
    //         //getcurrentpositionex获取当前绘图点坐标
    //         GetCurrentPositionEx(hdc, &pt);
    
    //3        //绘制一个网格
            //先绘制上到下
            //获取线条数量
    //         for (i = 1; i <= xCount;i++)
    //         {
    //             MoveToEx(hdc, i*padding, 0, NULL);
    //             LineTo(hdc, i*padding, yClient);
    //         }
    // 
    //         //再绘制左到右
    //         for (i = 1; i <= yCount;i++)
    //         {
    //             MoveToEx(hdc, 0, i*padding, NULL);
    //             LineTo(hdc, xClient, i*padding);
    //         }
    
    //4        //绘制网格2
            //rect就是客户区矩形......
    //         for (int x = 0; x < rect.right;x+=50)
    //         {
    //             MoveToEx(hdc, x, 0, NULL);
    //             LineTo(hdc, x, rect.bottom);
    //         }
    // 
    //         for (int x = 0; x < rect.bottom; x += 50)
    //         {
    //             MoveToEx(hdc, 0, x, NULL);
    //             LineTo(hdc, rect.right, x);
    //         }
    
    //5.    //画一个五角星
    //         MoveToEx(hdc, 30, 10,NULL);
    //         LineTo(hdc, 20, 50);
    //         LineTo(hdc, 50, 20);
    //         LineTo(hdc, 10, 20);
    //         LineTo(hdc, 40, 50);
    //         LineTo(hdc, 30, 10);
    
    //6.    //折线绘制五角星
    //         POINT apt[] = { 30, 10, 20, 50, 50, 20, 10, 20, 40, 50, 30, 10 };
    //         Polyline(hdc, apt, 6);
    //         PolylineTo(hdc, apt, 6);    //会重当前位置为起点开始向顶点绘制
    
    //7.    //同时绘制多个图像
    //         POINT apt2[] = {
    //             30, 10, 20, 50, 50, 20, 10, 20, 40, 50, 30, 10,    //五角星
    //             120, 120, 180, 120, 120, 180,120,120,    //直角三角形
    //             300, 300, 300, 400, 400, 400, 400, 300,300,300    //矩形
    //         };
            
    //         DWORD count[] = { 6, 4, 5 };
    //         PolyPolyline(hdc, apt2, count, 3);;
    
    //        MoveToEx(hdc, 100, 100, NULL);
    //        LineTo(hdc, 500, 100);
    //8.    //绘制sin曲线
    //         POINT apt3[400];
    //         double x, y,rad;
    //         for (int i = 0; i < 400;i+=1)
    //         {
    //             x = i;
    //             rad = PI / 180.0 * (360.0 / 400.0)*i;
    //             y = sin(rad);
    //             
    //             apt3[i].x = x+100;
    //             apt3[i].y = y*100+100;
    //         }
    //         Polyline(hdc, apt3, 400);
    
    //9.    //h绘制sin曲线,规范
            POINT apt4[NUM];
            MoveToEx(hdc, 0, yClient / 2, NULL);
            LineTo(hdc, xClient, yClient / 2);
    
            for (int j = 0; j < NUM;j++)
            {
                apt4[j].x = j*xClient/NUM;
                apt4[j].y = (int)((1 - sin(PI * 2 / NUM*j)) / 2 * yClient);
            }
            Polyline(hdc, apt4, NUM);
    
            DrawText(hdc, L"this is my first pragram win32", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
    
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
  • 相关阅读:
    c# 基础算法(一) 九九乘法
    万能模糊查询SQL
    C#通过连接ODBC的方式调用存储过程
    《从设计到模式》学习笔记part1
    C#知识归纳
    Python之路
    Tomcat优化
    Zabbix 3.0 + Nginx + Mariadb
    Spark DataFrame ETL教程
    Python连接presto
  • 原文地址:https://www.cnblogs.com/ssyfj/p/8506078.html
Copyright © 2020-2023  润新知