zoukankan      html  css  js  c++  java
  • 学习windows编程 day4 之视口和窗口

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        static int cxClient, cyClient;
        static int iMapMode;
        switch (message)
        {
        case WM_SIZE:
            cxClient = LOWORD(lParam);
            cyClient = HIWORD(lParam);
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
    //视口和窗口
            //逻辑坐标系统 ->窗口(程序员编码的)
            //设备坐标系统 -> 视口(眼睛看到的)
    
            //映射模式决定逻辑坐标的单位
            //设备坐标的单位是像素
    
            //在窗口下编程,在视口下显示
            
            MoveToEx(hdc, cxClient / 2, 0, NULL);
            LineTo(hdc, cxClient / 2, cyClient);
            MoveToEx(hdc, 0, cyClient / 2, NULL);
            LineTo(hdc, cxClient, cyClient / 2);
            //获取旧的映射模式
            iMapMode = GetMapMode(hdc);
            //设置新的    x向右 y向上 单位 0.1mm
            SetMapMode(hdc, MM_LOMETRIC);
            //设置视口
            SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);    //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0)
    
            TextOut(hdc, 100, 100, L"ViewPort", 8);
    
    
            EndPaint(hwnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
    
        return DefWindowProc(hwnd, message, wParam, lParam);
    }

    正弦函数练习

    #define NUM 1000
    #define PI 3.1415926

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect;
    static int cxClient, cyClient; static int iMapMode; POINT apt[1000]; switch (message) { case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); //视口和窗口 //逻辑坐标系统 ->窗口(程序员编码的) //设备坐标系统 -> 视口(眼睛看到的) //映射模式决定逻辑坐标的单位 //设备坐标的单位是像素 //在窗口下编程,在视口下显示 MoveToEx(hdc, 0, cyClient/2, NULL); LineTo(hdc, cxClient, cyClient/2); MoveToEx(hdc, 0, 0, NULL); LineTo(hdc, 0, cyClient); //获取旧的映射模式 iMapMode = GetMapMode(hdc); //设置视口 SetViewportOrgEx(hdc, 0, cyClient / 2, NULL); //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0) for (int i = 0; i < NUM;i++) { apt[i].x = cxClient / NUM*i; //单位不一致,需要测试 apt[i].y = -(int)(100 * sin(2 * PI*i / NUM)); } Polyline(hdc, apt, 1000); EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }

     坐标转换

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        static int cxClient, cyClient;
        static int iMapMode;
        POINT apt[1000];
        switch (message)
        {
        case WM_SIZE:
            cxClient = LOWORD(lParam);
            cyClient = HIWORD(lParam);
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
    //视口和窗口
            //逻辑坐标系统 ->窗口(程序员编码的)
            //设备坐标系统 -> 视口(眼睛看到的)
    
            //映射模式决定逻辑坐标的单位
            //设备坐标的单位是像素
    
            //在窗口下编程,在视口下显示
    
            //设备坐标转换为逻辑坐标    DPtoLP
            //逻辑坐标转换为设备坐标    LPtoDP
    
            MoveToEx(hdc, 0, cyClient/2, NULL);
            LineTo(hdc, cxClient, cyClient/2);
            MoveToEx(hdc, 0, 0, NULL);
            LineTo(hdc, 0, cyClient);
    
            //获取旧的映射模式
            iMapMode = GetMapMode(hdc);
            SetMapMode(hdc,MM_LOMETRIC);
    
            DPtoLP(hdc, &rect, 2);    //句柄,POINT指针,点个数    rect是两个点,矩形左上,右下
    
            //设置视口
            SetViewportOrgEx(hdc, 0, cyClient / 2, NULL);    //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0)
    
            for (int i = 0; i < NUM;i++)
            {
                apt[i].x = rect.right*i / NUM;    //单位不一致,需要测试
                apt[i].y = (int)(rect.bottom/2 * sin(2 * PI*i / NUM));
            }
    
            Polyline(hdc, apt, 1000);
    
            EndPaint(hwnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
    
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
  • 相关阅读:
    hpuoj--校赛--2015年的第一场雪(暴力)
    hpuoj--校赛--考试来了(水题)
    hpuoj--校赛--特殊的比赛日期(素数判断+模拟)
    bean copy
    spring boot+jaspersoft实现复杂报表
    spring boot 启动时运行代码(2)ApplicationListener
    Alibaba Java Coding Guidelines 安装(eclipse)并简单使用
    spring boot 系统启动时运行代码(1)-@PostConstruct
    java泛型&bean copy list
    jgroups-raft
  • 原文地址:https://www.cnblogs.com/ssyfj/p/8511367.html
Copyright © 2011-2022 走看看