⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windows gdi和gdi+编程实例剖析_2.htm

📁 GDI编程的参考资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0046)http://tech.bc0436.com/dev/255/2190255_1.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Windows GDI和GDI+编程实例剖析</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN>
<META content=all name=robots>
<META content="index, follow" name=googlebot>
<META content="MSHTML 6.00.2900.2180" name=GENERATOR>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META content=硬件,软件,数码,游戏,e时代,下载,社区,评测,笔记本; name=keywords><LINK media=all 
href="Windows GDI和GDI+编程实例剖析_2.files/yesky-content-cs.css" type=text/css 
rel=stylesheet></HEAD>
<BODY>
<SCRIPT language=JavaScript>
function sendemail(){ 
OpenWindow = window.open('http://other.chinabyte.com/chinabyte/qin/sendemail-new.shtm?url=/dev/255/2190255.shtml&title=Windows GDI和GDI+编程实例剖析&channel=324','emailwin','toolbar=no,location=no,scrollbars=no,menubar=no,width=462,height=450')
}</SCRIPT>

<DIV class=width>
<DIV class=minwidth>
<DIV class=container><!--头部-->
<DIV class=hidden>yesky内容</DIV>
<SCRIPT language=JavaScript src="" type=text/JavaScript></SCRIPT>
<SPAN id=ad1 style="DISPLAY: none"></SPAN>
<DIV align=center>
<SCRIPT>document.write(navigator.userAgent.indexOf('MSIE')==-1?"<SCRIPT src='http://219.239.88.50:80/adsunion/get/;pl=pl-1-survey2005-column-051118;tp=js;sk=0;ck=0;/?'><\/SCRIPT>":"<IFRAME MARGINHEIGHT=0 MARGINWIDTH=0 FRAMEBORDER=0 BORDER=0 VSPACE=0 WIDTH=760 NORESIZE HEIGHT=90 SCROLLING=NO SRC='http://219.239.88.50:80/adsunion/get/;pl=pl-1-survey2005-column-051118;tp=if;sk=0;ck=0;/?'></IFRAME>");</SCRIPT>
</DIV>
<DIV class=clear1></DIV>
<DIV class=newbox><SPAN id=ad8></SPAN></DIV><!--导航条-->
<DIV class=newbox>
<DIV class=newbgpic>
<DIV class=left>您现在的位置: <A href="http://tech.bc0436.com/">Yesky</A>&gt;<A 
href="http://tech.bc0436.com/soft"> 软件</A>&gt;<A 
href="http://tech.bc0436.com/dev"> 开发者网络</A> </DIV>
<DIV class=right>
<DIV class=space1><SPAN id=ad3></SPAN></DIV></DIV></DIV></DIV>
<DIV class=clear1></DIV><!--结束-->
<DIV class=clear1></DIV><!--文章内容区-->
<DIV class=newbox>
<DIV class=newleft>
<DIV class=newleft1>
<DIV class=newsmallbox>Windows GDI和GDI+编程实例剖析 </DIV>
<DIV class=clear1></DIV>
<DIV class=newsmall1box>
<DIV class=newtitle1>作者: 宋宝华 </DIV>
<DIV class=newtitle2>出处: 天极网 </DIV>
<DIV class=newtitle3>责任编辑: <A title=向本编辑提问 
href="http://comments.yesky.com/t/·&frac12;&Ouml;&Ucirc;/6,324/2190255.shtml">方舟 </A></DIV>
<DIV class=newtitle4>[ 2005-11-11 08:18 ]</DIV></DIV>
<DIV class=clear1></DIV>
<STYLE type=text/css>A.zhy0815button_content {
	BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: white 1px solid; PADDING-LEFT: 8px; FONT-SIZE: 12px; PADDING-BOTTOM: 4px; BORDER-LEFT: white 1px solid; COLOR: #000000; LINE-HEIGHT: 300%; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: "宋体"; TEXT-ALIGN: center; min-width: 75px
}
A.zhy0815button_content:link {
	COLOR: #000; LINE-HEIGHT: 300%; BACKGROUND-COLOR: #d9d9d9; TEXT-DECORATION: none
}
A.zhy0815button_content:visited {
	COLOR: #000000; LINE-HEIGHT: 300%; BACKGROUND-COLOR: #d9d9d9; TEXT-DECORATION: none
}
A.zhy0815button_content:hover {
	BORDER-LEFT-COLOR: black; BORDER-BOTTOM-COLOR: white; COLOR: #333333; BORDER-TOP-COLOR: black; LINE-HEIGHT: 300%; BACKGROUND-COLOR: #cccccc; TEXT-DECORATION: none; BORDER-RIGHT-COLOR: white
}
A.zhy0815button_content:active {
	COLOR: #000000; LINE-HEIGHT: 300%; BACKGROUND-COLOR: #d9d9d9; TEXT-DECORATION: none
}
</STYLE>

<CENTER><A class=zhy0815button_content title=编辑推荐你阅读 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#1" target=_self>推荐阅读</A> <A 
class=zhy0815button_content title=本栏目的最新更新 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#2" target=_self>最新更新</A> <A 
class=zhy0815button_content title=阅读此文章的读者还看了以下文章 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#3" target=_self>继续阅读</A> <A 
class=zhy0815button_content title=相关主题的关键字 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#4" target=_self>相关主题</A> <A 
class=zhy0815button_content title=本文编辑方舟的其他文章 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#5" target=_self>编辑新作</A> <A 
class=zhy0815button_content title=本栏目最近最受欢迎的文章 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#6" target=_self>阅读排行</A> <A 
class=zhy0815button_content id=comment_url_tag title=读者、编辑互动讨论区 
href="http://comments.yesky.com/t/GDI/6,324/2190255.shtml">讨论本文</A><A 
class=zhy0815button_content id=comment_url_art title=读者、编辑互动讨论区 
href="http://comments.yesky.com/a/6,324/2190255.shtml">讨论本文</A> <A 
class=zhy0815button_content title=本文相关产品和厂商 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#8" target=_self>相关产品</A> 
</CENTER>
<DIV class=space2><SPAN id=ad5></SPAN></DIV><BR>
<DIV class=newmiddlebox style="OVERFLOW: hidden">
<DIV class=guanggao><SPAN id=ad4></SPAN></DIV>
<DIV class=guanggao><SPAN 
id=contentAdv></SPAN></DIV>  <B>3.GDI编程</B><BR><BR>  "GDI"菜单下的"画线"子菜单单击事件消息处理函数的代码如下:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
  <TBODY>
  <TR>
    <TD>void CGdiexampleDlg::OnGdiDrawLine()<BR>{<BR> // TODO: Add your 
      command handler code here<BR> CClientDC 
      dc(this);<BR> //逻辑坐标与设备坐标变换<BR> CRect 
      rect;<BR> GetClientRect(&amp;rect);<BR> dc.SetMapMode(MM_ANISOTROPIC);<BR> dc.SetWindowOrg(0, 
      0);<BR> dc.SetWindowExt(rect.right, rect.bottom);<BR> dc.SetViewportOrg(0, 
      rect.bottom / 2);<BR> dc.SetViewportExt(rect.right, - 
      rect.bottom);<BR> //创建绘制正旋曲线的pen并将其选入设备上下文<BR> CPen pen(PS_SOLID, 1, 
      RGB(255, 0, 0));<BR> HGDIOBJ oldObject = 
      dc.SelectObject(pen.GetSafeHandle());<BR> //绘制正旋曲线<BR> dc.MoveTo(0, 
      0);<BR> for (int i = 0; i &lt; rect.right; i++)<BR> {<BR>  dc.LineTo(i, 
      100 *sin(2 *(i / (rect.right / 5.0)) 
      *PI));<BR> }<BR> //创建绘制x轴的pen并将其选入设备上下文<BR> CPen penx(PS_SOLID, 1, RGB(0, 
      0, 
      255));<BR> dc.SelectObject(penx.GetSafeHandle());<BR> //绘制X轴<BR> dc.MoveTo(0, 
      0);<BR> dc.LineTo(rect.right, 
      0);<BR> //恢复原先的pen<BR> dc.SelectObject(oldObject);<BR>}</TD></TR></TBODY></TABLE><BR>  单击这个按钮,会出现如图1所示的效果,我们来对此进行解读。<BR><BR>
<TABLE width="90%" align=center border=0>
  <TBODY>
  <TR>
    <TD>
      <DIV align=center><IMG 
      src="Windows GDI和GDI+编程实例剖析_2.files/2ik8432209qm.jpg" border=0><BR>图1 
      绘制正旋曲线</DIV></TD></TR></TBODY></TABLE><BR>  前文提到,GDI编程需进行设备坐标和逻辑坐标的转化。而屏幕上的设备坐标通常会按客户坐标给出,客户坐标依赖于窗口的客户区域,其起始位置位于客户区域的左上角。为示区别,图2给出了设备坐标和用户逻辑坐标的示例。<BR><BR>
<TABLE width="90%" align=center border=0>
  <TBODY>
  <TR>
    <TD>
      <DIV align=center><IMG 
      src="Windows GDI和GDI+编程实例剖析_2.files/70kz9wgst3nk.jpg" border=0><BR>图2 
      设备坐标与逻辑坐标</DIV></TD></TR></TBODY></TABLE><BR>  设备坐标与逻辑坐标的转换关系如下:<BR><BR>
<P align=center><IMG src="Windows GDI和GDI+编程实例剖析_2.files/4siv5dpb1b21.gif" 
border=0><BR><IMG src="Windows GDI和GDI+编程实例剖析_2.files/wi3uv29l246b.gif" 
border=0></P>  公式中的&lt;Xvorg, Yvorg&gt;是设备空间中视口的原点,而&lt; Xworg, Yworg 
&gt;是逻辑空间中窗口的原点。 
Xwext/Xvext和Ywext/Yvext分别是窗口与视口水平和垂直范围的比例。<BR><BR>  因此,经过程序中的dc.SetWindowOrg 
(0,0) 和dc.SetViewportOrg (0,rect.bottom/2)语句我们设置了视口和窗口的原点;而经过程序中的dc.SetWindowExt 
(rect.right,rect.bottom) 和dc.SetViewportExt (rect.right,-rect.bottom) 
语句我们设置了视口和窗口的范围。由于视口和窗口的纵坐标方向相反,设置视口的垂直范围为负值。这样我们得到了一个逻辑坐标原点为客户区水平方向最左边和垂直方向居中的坐标系,我们在这个坐标系上直接绘制正旋曲线,不需要再理睬Windows对话框客户区坐标了。<BR><BR>  void 
CGdiexampleDlg::OnGdiDrawLine()函数中未指定逻辑设备和物理设备的映射模式,则为缺省的MM_TEXT。在这种模式下,一个逻辑单位对应于一个像素点。映射模式是GDI中的一个重要概念,其它的映射模式还有MM_LOENGLlSH、MM_HIENGLISH、MM_LOMETRIC和MM_HIMETRIC等。我们可以通过如下语句指定映射模式为MM_TEXT:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
  <TBODY>
  <TR>
    <TD>dc.SetMapMode(MM_TEXT);</TD></TR></TBODY></TABLE><BR>  值得一提的是,从上述代码可以看出:在GDI编程中,几乎所有的操作都围绕设备上下文dc展开。的确,这正是GDI编程的特点!设备上下文是Windows 
使用的一种结构,所有GDI操作前都需取得特定设备的上下文,函数中的CClientDC dc (this) 
语句完成这一功能。<BR><BR>  归纳可得,利用GDI进行图形、图像处理的一般操作步骤为:<BR><BR>  1. 
取得指定窗口的DC;<BR><BR>  2. 确定使用的坐标系及映射方式; <BR><BR>  3. 进行图形、图像或文字处理;<BR><BR>  4. 
释放所使用的DC。<BR><BR>  4.GDI+编程<BR><BR>  "GDI+"菜单下的"画线"子菜单单击事件消息处理函数的代码如下: <BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
  <TBODY>
  <TR>
    <TD>void CGdiexampleDlg::OnGdipDrawLine()<BR>{<BR> // TODO: Add your 
      command handler code here <BR> CClientDC dc(this); 
      <BR> //逻辑坐标与设备坐标变换<BR> CRect 
      rect;<BR> GetClientRect(&amp;rect);<BR> dc.SetMapMode(MM_ANISOTROPIC);<BR> dc.SetWindowOrg(0, 
      0);<BR> dc.SetWindowExt(rect.right, rect.bottom);<BR> dc.SetViewportOrg(0, 
      rect.bottom / 2);<BR> dc.SetViewportExt(rect.right, - 
      rect.bottom);<BR><BR> //创建Graphics对象<BR> Graphics 
      graphics(dc);<BR> //创建pen<BR> Pen 
      myPen(Color::Red);<BR> myPen.SetWidth(1);<BR> //画正旋曲线<BR> for (int i = 0; 
      i &lt; rect.right; i++)<BR> {<BR>  graphics.DrawLine(&amp;myPen, i, 100 
      *sin(2 *(i / (rect.right / 5.0)) *PI), i +<BR>1, 100 *sin(2 *((i + 1) / 
      (rect.right / 5.0)) 
      *PI));<BR> }<BR> //画X轴<BR> myPen.SetColor(Color::Blue);<BR> graphics.DrawLine(&amp;myPen, 
      0, 0, rect.right, 0);<BR>}</TD></TR></TBODY></TABLE><BR>  由于我们使用的是Visual 
C++6.0而非VS.Net,我们需要下载微软的GDIPLUS支持包。在微软官方网站下载时需认证Windows为正版,我们可从这个地址下载:http://www.codeguru.com/code/legacy/gdi/GDIPlus.zip。一个完整的GDI+支持包至少包括如下文件:<BR><BR>  (1)头文件:gdiplus.h<BR><BR>  (2)动态链接库的.lib文件:gdiplus.lib<BR><BR>  (3)动态链接库的.dll文件:gdiplus.dll<BR><BR>  少了(1)、(2)程序不能编译,少了(3)程序能以共享DLL的方式编译但是不能运行,运行时找不到.dll文件。<BR><BR>  为使得Visual 
C++6.0支持GDI+,我们需要在使用GDI+对象的文件的开头添加如下代码:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
  <TBODY>
  <TR>
    <TD>#define UNICODE<BR>#ifndef ULONG_PTR<BR>#define ULONG_PTR unsigned 
      long*<BR>#endif<BR>#include "c:\gdiplus\includes\gdiplus.h"<BR>using 
      namespace Gdiplus;<BR>#pragma comment(lib, 
    "c:\\gdiplus\\lib\\gdiplus.lib")</TD></TR></TBODY></TABLE><BR>  在Visual 
C++中使用GDI+必须先进行GDI+的初始化,我们在CWinApp派生类的InitInstance函数中进行此项工作是最好的:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
  <TBODY>
  <TR>
    <TD>/////////////////////////////////////////////////////////////////////////////<BR>// 
      CGdiexampleApp initialization<BR><BR>BOOL 
      CGdiexampleApp::InitInstance()<BR>{<BR> AfxEnableControlContainer();<BR><BR> // 
      Standard initialization<BR><BR> #ifdef _AFXDLL<BR>  Enable3dControls(); // 
      Call this when using MFC in a shared 
      DLL<BR> #else<BR>  Enable3dControlsStatic(); // Call this when linking to 
      MFC statically<BR> #endif<BR><BR> //初始化gdiplus的环境<BR> GdiplusStartupInput 
      gdiplusStartupInput;<BR> ULONG_PTR gdiplusToken;<BR> // 
      初始化GDI+.<BR> GdiplusStartup(&amp;gdiplusToken, &amp;gdiplusStartupInput, 
      NULL);<BR><BR> CGdiexampleDlg dlg;<BR> m_pMainWnd = &amp;dlg;<BR> int 
      nResponse = dlg.DoModal();<BR> if (nResponse == IDOK){}<BR> else if 
      (nResponse == 
      IDCANCEL){}<BR><BR> //关闭gdiplus的环境<BR> GdiplusShutdown(gdiplusToken);<BR><BR> return 
      FALSE;<BR>}</TD></TR></TBODY></TABLE><BR>  单击"GDI+"菜单下的"画线"子菜单,也会出现如图1所示的效果。观察void 
CGdiexampleDlg::OnGdipDrawLine() 函数,我们发现用GDI+进行图形、图像操作的步骤为:<BR><BR>  (1)创建 
Graphics 对象:Graphics 对象表示GDI+绘图表面,是用于创建图形图像的对象;<BR><BR>  (2)使用 Graphics 
对象绘制线条和形状、呈现文本或显示与操作图像。<BR><BR>  Graphics 对象是GDI+的核心,GDI中设备上下文dc和Graphics 
对象的作用相似,但在GDI中使用的是基于句柄的编程模式,而GDI+中使用的则是基于对象的编程模式。Graphics封装了GDI+ 
绘图面,而且此类无法被继承,它的所有成员函数都不是虚函数。<BR><BR>  下面,我们来逐个用实际代码实现GDI+的新增功能,这些新增功能包括:渐变的画刷(Gradient 
Brushes)、基数样条函数(Cardinal Splines)、持久的路径对象(Persistent Path 
Objects)、变形和矩阵对象(Transformations &amp;Matrix Object)、可伸缩区域(Scalable 
Regions)、Alpha混合(Alpha Blending)和丰富的图像格式支持等。<BR><BR>
<DIV class=right>共8页。 <A class=page 
href="http://tech.bc0436.com/dev/255/2190255.shtml" target=_self><FONT 
face=Webdings>9</FONT></A> <A class=page 
href="http://tech.bc0436.com/dev/255/2190255.shtml" target=_self><FONT 
face=Webdings>7</FONT></A> <A class=page 
href="http://tech.bc0436.com/dev/255/2190255.shtml" target=_self><B>1</B></A> <A 
class=page href="http://tech.bc0436.com/dev/255/2190255_1.shtml" 
target=_self><FONT color=#666666><B>2</B></FONT></A> <A class=page 
href="http://tech.bc0436.com/dev/255/2190255_2.shtml" target=_self><B>3</B></A> 
<A class=page href="http://tech.bc0436.com/dev/255/2190255_3.shtml" 
target=_self><B>4</B></A> <A class=page 
href="http://tech.bc0436.com/dev/255/2190255_4.shtml" target=_self><B>5</B></A> 
<A class=page href="http://tech.bc0436.com/dev/255/2190255_5.shtml" 
target=_self><B>6</B></A> <A class=page 
href="http://tech.bc0436.com/dev/255/2190255_6.shtml" target=_self><B>7</B></A> 
<A class=page href="http://tech.bc0436.com/dev/255/2190255_7.shtml" 
target=_self><B>8</B></A> <A class=page 
href="http://tech.bc0436.com/dev/255/2190255_2.shtml" target=_self><FONT 
face=Webdings>8</FONT></A> <A class=page 
href="http://tech.bc0436.com/dev/255/2190255_7.shtml" target=_self><FONT 
face=Webdings>:</FONT></A></DIV></DIV>
<CENTER><A class=zhy0815button_content title=编辑推荐你阅读 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#1" target=_self>推荐阅读</A> <A 
class=zhy0815button_content title=本栏目的最新更新 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#2" target=_self>最新更新</A> <A 
class=zhy0815button_content title=阅读此文章的读者还看了以下文章 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#3" target=_self>继续阅读</A> <A 
class=zhy0815button_content title=相关主题的关键字 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#4" target=_self>相关主题</A> <A 
class=zhy0815button_content title=本文编辑方舟的其他文章 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#5" target=_self>编辑新作</A> <A 
class=zhy0815button_content title=本栏目最近最受欢迎的文章 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#6" target=_self>阅读排行</A> <A 
class=zhy0815button_content id=comment_url_tag title=读者、编辑互动讨论区 
href="http://comments.yesky.com/t/GDI/6,324/2190255.shtml">讨论本文</A><A 
class=zhy0815button_content id=comment_url_art title=读者、编辑互动讨论区 
href="http://comments.yesky.com/a/6,324/2190255.shtml">讨论本文</A> <A 
class=zhy0815button_content title=本文相关产品和厂商 
href="http://tech.bc0436.com/dev/255/2190255_1.shtml#8" target=_self>相关产品</A> 
</CENTER><!-- Error -->
<DIV class=clear1></DIV></DIV><!--结束-->
<DIV class=clear1></DIV>
<DIV class=space2><SPAN id=ad7></SPAN></DIV><!--文章评论区-->
<SCRIPT language=JavaScript>
<!--
var aid = 2190255;
var channelid = 324;
var tagname = "GDI";


//-->
</SCRIPT>

<SCRIPT language=JavaScript>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -