鱼C论坛

 找回密码
 立即注册
查看: 3435|回复: 1

[知识点备忘] 第027讲:坐标转换

[复制链接]
发表于 2016-3-12 23:51:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
知识点回顾:

1. Windows 对所有的消息均使用设备坐标

也就是以像素为单位。


2. Windows 有两个 API 函数用于逻辑坐标与设备坐标之间的转换

  • 将设备坐标转换为逻辑坐标,我们使用 DPtoLP 函数;
  • 将逻辑坐标转换为设备坐标,我们使用 LPtoDP 函数。


3. 绘制一个正弦函数的完整代码清单

搜狗截图20160312235030.png

  1. /* -------------------------------------------------------------------
  2.                     MyWindows.c -- 基本窗口模型  
  3.                                 《Windows 程序设计(SDK)》视频教程                    
  4. --------------------------------------------------------------------*/

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

  7. #define                NUM                1000
  8. #define                TWOPI                (2 * 3.14159)

  9. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

  10. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  11. {
  12.         static TCHAR szAppName[] = TEXT("MyWindows");
  13.         HWND hwnd;
  14.         MSG msg;
  15.         WNDCLASS wndclass;

  16.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  17.         wndclass.lpfnWndProc = WndProc;
  18.         wndclass.cbClsExtra = 0;
  19.         wndclass.cbWndExtra = 0;
  20.         wndclass.hInstance = hInstance;
  21.         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  22.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  23.         wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  24.         wndclass.lpszMenuName = NULL;
  25.         wndclass.lpszClassName = szAppName;

  26.         if (!RegisterClass(&wndclass))
  27.         {
  28.                 MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
  29.                 return 0;
  30.         }

  31.         hwnd = CreateWindow(szAppName,
  32.                 TEXT("鱼C工作室"),
  33.                 WS_OVERLAPPEDWINDOW,
  34.                 CW_USEDEFAULT,
  35.                 CW_USEDEFAULT,
  36.                 CW_USEDEFAULT,
  37.                 CW_USEDEFAULT,
  38.                 NULL,
  39.                 NULL,
  40.                 hInstance,
  41.                 NULL);
  42.        
  43.         ShowWindow(hwnd, iCmdShow);
  44.         UpdateWindow(hwnd);

  45.         while (GetMessage(&msg, NULL, 0, 0))
  46.         {
  47.                 TranslateMessage(&msg);
  48.                 DispatchMessage(&msg);
  49.         }

  50.         return msg.wParam;
  51. }

  52. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  53. {
  54.         HDC hdc;
  55.         RECT rect;
  56.         POINT apt[NUM];
  57.         PAINTSTRUCT ps;
  58.        
  59.         int i;
  60.         int iMapMode;
  61.         static int cxClient, cyClient;
  62.        
  63.         switch (message)
  64.         {
  65.         case WM_SIZE:
  66.                 cxClient = LOWORD(lParam);
  67.                 cyClient = HIWORD(lParam);
  68.                 return 0;

  69.         case WM_PAINT:
  70.                 hdc = BeginPaint(hwnd, &ps);

  71.                 MoveToEx(hdc, 0, cyClient / 2, NULL);
  72.                 LineTo(hdc, cxClient, cyClient / 2);
  73.                 MoveToEx(hdc, cxClient / 2, 0, NULL);
  74.                 LineTo(hdc, cxClient / 2, cyClient);

  75.                 iMapMode = GetMapMode(hdc);
  76.                 SetMapMode(hdc, MM_LOMETRIC);

  77.                 SetViewportOrgEx(hdc, 0, cyClient / 2, NULL);

  78.                 GetClientRect(hwnd, &rect);
  79.                 DPtoLP(hdc, (PPOINT)&rect, 2);

  80.                 for (i = 0; i < NUM; i++)
  81.                 {
  82.                         apt[i].x = i * (rect.right - rect.left) / NUM;
  83.                         apt[i].y = (int)((rect.top) * sin(TWOPI * i / NUM));
  84.                 }

  85.                 MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
  86.                 PolylineTo(hdc, apt, NUM);

  87.                 SetMapMode(hdc, iMapMode);

  88.                 EndPaint(hwnd, &ps);
  89.                 return 0;

  90.         case WM_DESTROY:
  91.                 PostQuitMessage(0);
  92.                 return 0;
  93.         }

  94.         return DefWindowProc(hwnd, message, wParam, lParam);
  95. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-4-9 15:24:21 | 显示全部楼层
谢谢小甲鱼老师  这段代码写得非常优美
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-23 20:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表