程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言基礎知識 >> 在窗口中輸出文字

在窗口中輸出文字

編輯:C語言基礎知識

在這裡假定已經利用ApplicationWizard生成了一個SDI界面的程序代碼。接下來的你只需要在CView派生類的OnDraw成員函數中加入繪圖代碼就可以了。在這裡我需要解釋一下OnDraw函數的作用,OnDraw函數會在窗口需要重繪時自動被調用,傳入的參數CDC* pDC對應的就是DC環境。使用OnDraw的優點就在於在你使用打印功能的時候傳入OnDraw的DC環境將會是打印機繪圖環境,使用打印預覽時傳入的是一個稱為CPreviewDC的繪圖環境,所以你只需要一份代碼就可以完成窗口/打印預覽/打印機繪圖三重功能。利用Windows的設備無關性和M$為打印預覽所編寫的上千行代碼你可以很容易的完成一個具有所見即所得的軟件。

輸出文字一般使用CDC::BOOL TextOut( int x, int y, const CString& str )和CDC::int DrawText( const CString& str, LPRECT lpRect, UINT nFormat )兩個函數,對TextOut來講只能輸出單行的文字,而DrawText可以指定在一個矩形中輸出單行或多行文字,並且可以規定對齊方式和使用何種風格。nFormat可以是多種以下標記的組合(利用位或操作)以達到選擇輸出風格的目的。

DT_BOTTOM底部對齊   Specifies bottom-justified text. This value must be combined with DT_SINGLELINE.

DT_CALCRECT計算指定文字時所需要矩形尺寸   Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text, but does not draw the text.

DT_CENTER中部對齊   Centers text horizontally.

DT_END_ELLIPSIS or DT_PATH_ELLIPSIS   Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified.

You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash () characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash.

DT_EXPANDTABS   Expands tab characters. The default number of characters per tab is eight.

DT_EXTERNALLEADING   Includes the font抯 external leading in the line height. Normally, external leading is not included in the height of a line of text.

DT_LEFT左對齊   Aligns text flush-left.

DT_MODIFYSTRING   Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified.

Note Some uFormat flag combinations can cause the passed string to be modified. Using DT_MODIFYSTRING with either DT_END_ELLIPSIS or DT_PATH_ELLIPSIS may cause the string to be modified, causing an assertion in the CString override.

DT_NOCLIP   Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.

DT_NOPREFIX禁止使用&前綴   Turns off processing of prefix characters. Normally, DrawText interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the two-ampersand (&&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying DT_NOPREFIX, this processing is turned off.

DT_PATH_ELLIPSIS   

DT_RIGHT右對齊   Aligns text flush-right.

DT_SINGLELINE單行輸出   Specifies single line only. Carriage returns and linefeeds do not break the line.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved