?? 用visual c++_net進行gdi+編程 _3.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0048)http://www.qqread.com/vcdotnet/d652242000_3.html -->
<HTML lang=zh xml:lang="zh" xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>用Visual C++.NET進行GDI+編程 - QQread.com 電腦頻道</TITLE>
<META http-equiv=Content-Language content=zh-CN>
<META
content=[art_mtkw],電腦入門,上網指南,操作系統,常用軟件,黑客與安全,網絡游戲,程序開發,數據庫開發,網頁制作,圖像處理,多媒體制作,網絡編程,加密技術,網絡技術
name=keywords>
<META content="用Visual C++.NET進行GDI+編程" name=description>
<META content=QQRead.com name=author>
<META content=all name=robots>
<META http-equiv=Content-Type content="text/html; charset=gb2312"><LINK
href="用Visual C++_NET進行GDI+編程 _3.files/qqread_html.css" type=text/css
rel=stylesheet><LINK href="用Visual C++_NET進行GDI+編程 _3.files/qqread_html_com.css"
type=text/css rel=stylesheet>
<STYLE type=text/css>.STYLE2 {
FONT-SIZE: 14px
}
</STYLE>
<META content="MSHTML 6.00.2900.2180" name=GENERATOR></HEAD>
<BODY><!-- 公共頁頭 -->
<DIV style="TEXT-ALIGN: center">
<DIV id=topad>
<TABLE>
<TBODY>
<TR>
<TD width=10><SPAN
id=ad_qqread_top></SPAN><!--End//--></TD></TR></TBODY></TABLE></DIV>
<DIV id=CHBig>
<DIV id=CHBigCSDNLogo><A href="http://www.qqread.com/"><IMG alt=巧巧讀書網
src="用Visual C++_NET進行GDI+編程 _3.files/qqread.gif" border=0></A></DIV>
<DIV id=CHBigHeaderWrapper>
<DIV id=CHBigNavBar2>
<UL>
<LI><A href="http://www.qqread.com/news/" target=_blank>新聞中心</A>|
<LI><A href="http://www.qqread.com/skill/" target=_blank>技巧集錦</A>|
<LI><A href="http://www.qqread.com/itlife/" target=_blank>IT人生雨露</A>|
<LI><A href="http://www.qqread.com/pcbase/" target=_blank>電腦入門</A>|
<LI><A href="http://www.qqread.com/netreference/" target=_blank>上網指南</A>|
<LI><A href="http://www.qqread.com/safe/" target=_blank>黑客與安全</A>|
<LI><A href="http://www.qqread.com/commentsoft/" target=_blank>常用軟件</A>|
<LI><A href="http://www.qqread.com/os/" target=_blank>操作系統</A>|
<LI><A href="http://www.qqread.com/netgame/" target=_blank>網絡游戲</A><BR>
<LI><A title=圖片欣賞 href="http://www.qqread.com/photo/index.html"
target=_blank>圖片欣賞</A>|
<LI><A href="http://www.qqread.com/program/" target=_blank>程序開發</A>|
<LI><A href="http://www.qqread.com/database/" target=_blank>數據庫開發</A>|
<LI><A href="http://www.qqread.com/webdesign/" target=_blank>網頁制作</A>|
<LI><A href="http://www.qqread.com/photohandle/" target=_blank>圖像處理</A>|
<LI><A href="http://www.qqread.com/mutimedia/" target=_blank>多媒體制作</A>|
<LI><A href="http://www.qqread.com/netprogram/" target=_blank>網絡編程</A>|
<LI><A href="http://www.qqread.com/encrypt/" target=_blank>加密技術</A>|
<LI><A href="http://www.qqread.com/network/" target=_blank>網絡技術</A>
</LI></UL></DIV>
<DIV id=CHBigNavBar1></DIV></DIV>
<DIV style="CLEAR: both"></DIV></DIV></DIV><!-- /公共頁頭 -->
<DIV id=OutBox>
<DIV id=listMainArea>
<DIV id=disclaimer><A href="http://www.qqread.com/">巧巧讀書首頁</A> > <A
title=程序開發 href="http://www.qqread.com/program/index.html">程序開發</A> > <A
title=.Net技術 href="http://www.qqread.com/dotnet/index.html">.Net技術</A> > <A
title=VC.NET href="http://www.qqread.com/vcdotnet/index.html">VC.NET</A> >
正文</DIV>
<DL class=ReadArticle>
<DT>用Visual C++.NET進行GDI+編程
<DD class=ArticleInfo3>
<SCRIPT src="用Visual C++_NET進行GDI+編程 _3.files/adtitle.js"
type=text/javascript></SCRIPT>
<HR SIZE=2>
<DD class=ArticleInfo3>
<DIV align=center><A href="http://www.qqread.com/">http://www.qqread.com/</A>
作者:ADDING 2006-01-24 來源:yesky 瀏覽:<SPAN id=count></SPAN> </DIV>
<DD class=ArticleInfo_3>
<DD class=ArticleInfo_3><A
href="http://www.qqread.com/vcdotnet/d652242000_2.html">上一頁</A> <A
href="http://www.qqread.com/vcdotnet/d652242000.html">1</A> <A
href="http://www.qqread.com/vcdotnet/d652242000_2.html">2</A> <FONT
color=#ff0000>3</FONT>
<DD class=ArticleContent><BR> 6、用Visual C++.NET使用GDI+的一般方法<BR><BR> 在Visual
C++.NET使用GDI+一般遵循下列步驟:<BR><BR> (1)
在應用程序中添加GDI+的包含文件gdiplus.h以及附加的類庫gdiplus.lib。通常gdiplus.h包含文件添加在應用程序的stdafx.h文件中,而gdiplus.lib可用兩種進行添加:第一種是直接在stdafx.h文件中添加下列語句:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>#pragma comment( lib, "gdiplus.lib"
)</TD></TR></TBODY></TABLE><BR> 另一種方法是:選擇"項目"ò"屬性"菜單命令,在彈出的對話框中選中左側的"鏈接器"ò"輸入"選項,在右側的"附加依賴項"框中鍵入gdiplus.lib,結果如圖1所示。<BR><BR><IMG
alt="用Visual C++.NET進行GDI+編程(圖一)"
src="用Visual C++_NET進行GDI+編程 _3.files/20060124pq205_1.gif"
border=0><BR>圖1<BR><BR> (2) 在應用程序項目的應用類中,添加一個成員變量,如下列代碼:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>ULONG_PTR
m_gdiplusToken;</TD></TR></TBODY></TABLE><BR> 其中,ULONG_PTR是一個DWORD數據類型,該成員變量用來保存GDI+被初始化后在應用程序中的GDI+標識,以便能在應用程序退出后,引用該標識來調用Gdiplus::
GdiplusShutdown來關閉GDI+。<BR><BR> (3)
在應用類中添加ExitInstance的重載,并添加下列代碼用來關閉GDI+:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>int
CEx_GDIPlusApp::ExitInstance()<BR>{<BR> Gdiplus::GdiplusShutdown(m_gdiplusToken);<BR> return
CWinApp::ExitInstance();<BR>}</TD></TR></TBODY></TABLE><BR> (4)
在應用類的InitInstance函數中添加GDI+的初始化代碼:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>BOOL
CEx_GDIPlusApp::InitInstance()<BR>{<BR> CWinApp::InitInstance();<BR> Gdiplus::GdiplusStartupInput
gdiplusStartupInput;<BR> Gdiplus::GdiplusStartup(&m_gdiplusToken,
&gdiplusStartupInput,
NULL);<BR> ...<BR>}</TD></TR></TBODY></TABLE><BR> (5)
在需要繪圖的窗口或視圖類中添加GDI+的繪制代碼。<BR><BR> 下面分別就單文檔和基于對話框應用程序為例,說明使用GDI+的一般過程和方法。<BR><BR> 1.
在單文檔應用程序中使用GDI+<BR><BR> 在上面的過程中,我們就是以一個單文檔應用程序Ex_GDIPlus作為示例的。下面列出第5步所涉及的代碼:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>void CEx_GDIPlusView::OnDraw(CDC* pDC)<BR>{<BR> CEx_GDIPlusDoc* pDoc
= GetDocument();<BR> ASSERT_VALID(pDoc);<BR> using namespace
Gdiplus;<BR> Graphics graphics( pDC->m_hDC );<BR> Pen newPen( Color(
255, 0, 0 ), 3 );<BR> HatchBrush newBrush(
HatchStyleCross,<BR> Color(255, 0, 255, 0),<BR> Color(255, 0, 0, 255));
<BR> // 創建一個填充畫刷,前景色為綠色,背景色為藍色<BR><BR> graphics.DrawRectangle(
&newPen, 50, 50, 100, 60); <BR> //
在(50,50)處繪制一個長為100,高為60的矩形<BR><BR> graphics.FillRectangle(
&newBrush, 50, 50, 100, 60);<BR> //
在(50,50)處填充一個長為100,高為60的矩形區域<BR>}</TD></TR></TBODY></TABLE><BR> 編譯并運行,結果如圖2所示。<BR><BR><IMG
alt="用Visual C++.NET進行GDI+編程(圖二)"
src="用Visual C++_NET進行GDI+編程 _3.files/20060124pq205_2.gif"
border=0><BR>圖2<BR><BR> 2. 在基于對話框應用程序中使用GDI+<BR><BR> 步驟如下:<BR><BR> (1)
創建一個默認的基于對話框的應用程序Ex_GDIPlusDlg。<BR><BR> (2) 打開stdafx.h文件添加下列代碼:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>#include <gdiplus.h><BR>#pragma comment( lib, "gdiplus.lib"
)</TD></TR></TBODY></TABLE><BR> (3) 打開Ex_GDIPlusDlg.h文件,添加下列代碼:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>class CEx_GDIPlusDlgApp : public
CWinApp<BR>{<BR> ...<BR> public:<BR> virtual BOOL
InitInstance();<BR> ULONG_PTR
m_gdiplusToken;<BR> ...<BR>};</TD></TR></TBODY></TABLE><BR> (4) 在
CEx_GDIPlusDlgApp類的屬性窗口中,單擊"重寫"工具按鈕,為該添加ExitInstance的重載:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>int
CEx_GDIPlusDlgApp::ExitInstance()<BR>{<BR> Gdiplus::GdiplusShutdown(m_gdiplusToken);<BR> return
CWinApp::ExitInstance();<BR>}</TD></TR></TBODY></TABLE><BR> (5)
定位到CEx_GDIPlusDlgApp::InitInstance函數處,添加下列GDI+初始化代碼:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>BOOL
CEx_GDIPlusDlgApp::InitInstance()<BR>{<BR> CWinApp::InitInstance();<BR> Gdiplus::GdiplusStartupInput
gdiplusStartupInput;<BR> Gdiplus::GdiplusStartup(&m_gdiplusToken,
&gdiplusStartupInput,
NULL);<BR> ...<BR>}</TD></TR></TBODY></TABLE><BR> (6)
定位到CEx_GDIPlusDlgDlg::OnPaint函數處,添加下列GDI+代碼:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD>void CEx_GDIPlusDlgDlg::OnPaint() <BR>{<BR> if
(IsIconic())<BR> {<BR> ...<BR> }<BR> else<BR> {<BR> CPaintDC dc(this);
// 用于繪制的設備上下文<BR> using namespace Gdiplus;<BR> Graphics graphics(
dc.m_hDC );<BR> Pen newPen( Color( 255, 0, 0 ), 3 );<BR> HatchBrush
newBrush( HatchStyleCross,<BR> Color(255, 0, 255, 0),<BR> Color(255,
0, 0, 255)); <BR> graphics.DrawRectangle( &newPen, 50, 50, 100,
60); <BR> graphics.FillRectangle( &newBrush, 50, 50, 100,
60);<BR> CDialog::OnPaint();<BR> }<BR>}</TD></TR></TBODY></TABLE><BR> (7)
編譯并運行,結果如圖3所示。<BR><BR><IMG alt="用Visual C++.NET進行GDI+編程(圖三)"
src="用Visual C++_NET進行GDI+編程 _3.files/20060124pq205_3.gif"
border=0><BR>圖3<BR><BR> 從上述例子可以看出,只要能獲得一個窗口的設備環境指針,就可構造一個Graphics對象,從而可以在其窗口中進行繪圖,我們不必在像以往那樣使用Invalidate/UpdateWindow來防止<A
href="http://www.qqread.com/keywords/windows.html"
target=_blank>Windows</A>對對話框窗口進行重繪。
<TABLE style="MARGIN: 10px 8px 3px 7px" cellSpacing=0 cellPadding=0 align=left
border=0>
<TBODY>
<TR>
<TD><SPAN id=ad_qqread_mid_big></SPAN></TD></TR></TBODY></TABLE>
<DD class=ArticleContent>關鍵字: <A
href="http://www.qqread.com/keywords/image-processing.html">圖像處理</A> <A
href="http://www.baidu.com/baidu?tn=jkday2000&word=設計">設計</A> <A
href="http://www.qqread.com/keywords/operating-system.html">操作系統</A> <A
href="http://www.qqread.com/keywords/word.html">word</A> <A
href="http://www.qqread.com/keywords/windows.html">windows</A> <A
href="http://www.qqread.com/keywords/visualcpp.html">visual
c++</A> <A
href="http://www.baidu.com/baidu?tn=jkday2000&word=PDA">pda</A> <A
href="http://www.qqread.com/keywords/os.html">os</A> <A
href="http://www.qqread.com/keywords/mfc.html">mfc</A> <A
href="http://www.qqread.com/keywords/ie.html">ie</A>
<DD class=ArticleInfo_3><A
href="http://www.qqread.com/vcdotnet/d652242000_2.html">上一頁</A> <A
href="http://www.qqread.com/vcdotnet/d652242000.html">1</A> <A
href="http://www.qqread.com/vcdotnet/d652242000_2.html">2</A> <FONT
color=#ff0000>3</FONT> </DD></DL>
<DIV id=ArticleContent4>
<SCRIPT src="用Visual C++_NET進行GDI+編程 _3.files/adend.js"
type=text/javascript></SCRIPT>
</DIV>
<TABLE style="MARGIN-BOTTOM: 5px" cellSpacing=1 cellPadding=0 width=582
align=center bgColor=#235f99 border=0>
<TBODY>
<TR bgColor=#ffffff>
<TD bgColor=#ffffff>
<SCRIPT src="用Visual C++_NET進行GDI+編程 _3.files/580.js"
type=text/javascript></SCRIPT>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -