My FAQ,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Visual C++ > 图形图像与多媒体
World space 到 Page space 的转换简介
作者:未知 时间:2005-07-20 14:15 出处:VC知识库 责编:MyFAQ
              摘要:World space 到 Page space 的转换简介

World space 到 Page space 的转换简介

作者:刘毓麟

下载源代码

一、坐标空间和 World space 到 Page space的转换
  坐标空间是一个平面的,在上面图形都是以平面直角坐标定位的。应用程序借助坐标空间可以缩放,平移,剪切,镜像图形的输出。 在Windows GDI中涉及到以下几种坐标空间

        世界坐标空间(world space)
        页面坐标空间(page space)
        设备坐标空间(device space)
        物理坐标空间(physical device space)	  
  通过转换可以改变目标的大小,方向,形状,转换同时能把图形目标从一个坐标空间转化 到另一个坐标空间,最终在物理设备如屏幕、打印机上显示出来。



  我现在介绍的是在编程中相对比较少用到的 World space 到 Page space 的转换。在默认的GDI作图模式下是没有World space 到Page space的转换的,只有在应用程序通过 SetGraphicsMode设置graphics mode到GM_ADVANCED下并且调用SetWorldTransform 设置变换参 数时才会有World space 到Page space的转换。

二、World space 到Page space的转换原理

1、重要结构和函数如下:
     typedef struct  _XFORM { 
           FLOAT eM11; 
           FLOAT eM12; 
           FLOAT eM21; 
           FLOAT eM22; 
           FLOAT eDx; 
           FLOAT eDy; 
     }XFORM, *PXFORM; 	  
用于变换的矩阵数据。
     int SetGraphicsMode(
           HDC hdc,    // handle to device context
           int iMode   // graphics mode
     );	  
设定设备环境上下文(DC)的graphics mode , 当graphics mode设定为GM_ADVANCED才能 支持世界变换。
     BOOL SetWorldTransform(
          HDC hdc,               // handle to device context
          CONST XFORM *lpXform   // transformation data
     );	  
设定设备环境上下文(DC)的世界变换。
     BOOL CombineTransform(
        LPXFORM lpxformResult, // combined transformation
        CONST XFORM *lpxform1, // first transformation
        CONST XFORM *lpxform2 // second transformation
     );     	  
世界变换的组合运算。
     BOOL GetWorldTransform(
        HDC hdc, // handle to device context
        LPXFORM lpXform // transformation
        );	  
得到设备环境上下文(DC)的世界变换。
2、变换原理 假定World space中的坐标为(x,y),变换后坐标为(x'',y''),变换矩阵数据为:
                    FLOAT eM11; 
                    FLOAT eM12; 
                    FLOAT eM21; 
                    FLOAT eM22; 
                    FLOAT eDx; 
                    FLOAT eDy; 	  
则变换计算公式如下:
                                  | eM11 eM12 0 | 
            |x'' y'' 1| = |x y 1| * | eM21 eM22 0 | 
                                  | eDx  eDy  1 | 	  
三、示例 将图形以原点为中心逆时针旋转。



主要代码如下:
01void TransformAndDraw(int iTransform,HWND hWnd)?
02{ 
03      HDC hDC; 
04      XFORM xForm; 
05      RECT rect;  
06      ::GetClientRect(hWnd, (LPRECT) &rect);     
07      hDC = ::GetDC(hWnd);  
08      ::MoveToEx(hDC ,rect.right / 2 , 0 , NULL  ) ; 
09      ::LineTo(hDC , rect.right / 2 , rect.bottom ) ; 
10      ::MoveToEx(hDC , 0 , rect.bottom / 2 , NULL )  ; 
11      ::LineTo(hDC ,  rect.right , rect.bottom / 2) ; 
12      ::SetGraphicsMode(hDC, GM_ADVANCED); 
13      switch (iTransform) 
14      { 
15      case 0:
16          AfxGetMainWnd()->SetWindowText("原始图像") ;
17             break; 
18          case 1:        // Scale to 1/2 of the original size. 
19              xForm.eM11 = (FLOAT) 0.5; 
20              xForm.eM12 = (FLOAT) 0.0; 
21              xForm.eM21 = (FLOAT) 0.0; 
22              xForm.eM22 = (FLOAT) 0.5; 
23              xForm.eDx  = (FLOAT) 0.0; 
24              xForm.eDy  = (FLOAT) 0.0; 
25              ::SetWorldTransform(hDC, &xForm); 
26              AfxGetMainWnd()->SetWindowText("缩小到一半") ;            
27              break; 
28   
29          case 2:   // Translate right by 3/4 inch. 
30              xForm.eM11 = (FLOAT) 1.0; 
31              xForm.eM12 = (FLOAT) 0.0; 
32              xForm.eM21 = (FLOAT) 0.0; 
33              xForm.eM22 = (FLOAT) 1.0; 
34              xForm.eDx  = (FLOAT) 75.0; 
35              xForm.eDy  = (FLOAT) 0.0; 
36              ::SetWorldTransform(hDC, &xForm); 
37          AfxGetMainWnd()->SetWindowText("平移") ;
38              break; 
39   
40          case 3:      // Rotate 30 degrees counterclockwise. 
41              xForm.eM11 = (FLOAT) 0.8660; 
42              xForm.eM12 = (FLOAT) 0.5000; 
43              xForm.eM21 = (FLOAT) -0.5000; 
44              xForm.eM22 = (FLOAT) 0.8660; 
45              xForm.eDx  = (FLOAT) 0.0; 
46              xForm.eDy  = (FLOAT) 0.0; 
47              ::SetWorldTransform(hDC, &xForm); 
48          AfxGetMainWnd()->SetWindowText("旋转") ;
49              break; 
50   
51          case 4:       // Shear along the x-axis with a 
52                            // proportionality constant of 1.0. 
53              xForm.eM11 = (FLOAT) 1.0; 
54              xForm.eM12 = (FLOAT) 1.0; 
55              xForm.eM21 = (FLOAT) 0.0; 
56              xForm.eM22 = (FLOAT) 1.0; 
57              xForm.eDx  = (FLOAT) 0.0; 
58              xForm.eDy  = (FLOAT) 0.0; 
59              ::SetWorldTransform(hDC, &xForm); 
60          AfxGetMainWnd()->SetWindowText("剪切") ;
61              break; 
62   
63          case 5:     // Reflect about a horizontal axis. 
64              xForm.eM11 = (FLOAT) 1.0; 
65              xForm.eM12 = (FLOAT) 0.0; 
66              xForm.eM21 = (FLOAT) 0.0; 
67              xForm.eM22 = (FLOAT) -1.0; 
68              xForm.eDx  = (FLOAT) 0.0; 
69              xForm.eDy  = (FLOAT) 0.0; 
70              ::SetWorldTransform(hDC, &xForm); 
71              AfxGetMainWnd()->SetWindowText("水平镜像") ;
72              break;  
73        
74      case 6:     // Reflect about a Vertical axis. 
75              xForm.eM11 = (FLOAT) -1.0; 
76              xForm.eM12 = (FLOAT) 0.0; 
77              xForm.eM21 = (FLOAT) 0.0; 
78              xForm.eM22 = (FLOAT) 1.0; 
79              xForm.eDx  = (FLOAT) 0.0; 
80              xForm.eDy  = (FLOAT) 0.0; 
81              ::SetWorldTransform(hDC, &xForm);
82          AfxGetMainWnd()->SetWindowText("垂直镜像") ;            
83              break; 
84   
85      }      
86      ::DtoLP(hDC, (LPPOINT) &rect, 2); 
87      HBITMAP hBMP1;
88      hBMP1 = ::LoadBitmap(AfxGetResourceHandle() ,  MAKEINTRESOURCE(IDB_BITMAP1)) ;
89      HDC hImageDC = ::CreateCompatibleDC((HDC)hDC);
90      HBITMAP hOldImageBMP = (HBITMAP)::SelectObject( hImageDC , hBMP1 );
91      ::BitBlt((HDC)hDC,(rect.right/2 -40),(rect.bottom / 2 -40),
                 80,80,hImageDC,0,0,SRCCOPY );        
92      ::SelectObject((HDC)hDC , hOldImageBMP);      
93      ::DeleteObject(hBMP1) ; 
94      ::DeleteDC(hImageDC) ; 
95      ::ReleaseDC(hWnd, hDC); 
96  } 	  
四、结束语
  错误之处希望大家批评指正!关于坐标变换的详细信息请参阅msdn中 Coordinate Spaces and Transformations一节。源代码见附件。
 

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 myfaq.com.cn All rights reserved. www.myfaq.com.cn 版权所有