說明:
TextOut 函式可在指定的裝置環境代碼(Device Context)的某座標上輸出文字
引用函式庫:
GDI32
適用系統:
Windows NT 3.1 或 Windows 95以上
函式原型:
Declare Function TextOut Lib "gdi32" Alias "TextOutA" ( _ ByVal hdc As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal lpString As String, _ ByVal nCount As Long _ ) As Long
參數型態及說明:
hdc:Long 要輸出文字的裝置環境代碼(Device Context)
x:Long 文字輸出的 X 座標
y:Long 文字輸出的 Y 座標
lpString:String 要輸出的文字
nCount:Long 文字要輸出的長度
回傳值:
Long 呼叫成功的話,將會回傳非零的數值;若呼叫失敗的話,則回傳 0
.NET Framework API:
System.Drawing.Graphics.DrawString
VB範例:
檢視原始碼 Visual Basic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 'VB的宣告 Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" ( _ ByVal hdc As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal lpString As String, _ ByVal nCount As Long _ ) As Long Private Declare Function GetDC Lib "user32" ( _ ByVal hwnd As Long _ ) As Long Dim hdc As Long '取得目前執行視窗的裝置環境代碼(Device Context) hdc = GetDC(Form1.hwnd) Dim rtvl As Long '在目前執行視窗的(30, 30)位置上輸出-TextOut rtvl = TextOut(hdc, 30, 30, "TextOut", 7) |
RB範例:
檢視原始碼 REALBasic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 'RB的宣告 Declare Function TextOut Lib "gdi32" Alias "TextOutA" ( _ hdc As Integer, _ x As Integer, _ y As Integer, _ lpString As CString, _ nCount As Integer _ ) As Boolean Declare Function GetDC Lib "user32" ( _ hwnd As Integer _ ) As Integer Dim hdc As Integer '取得目前執行視窗的裝置環境代碼(Device Context) hdc = GetDC(Window1.WinHWND) Dim rtvl As Boolean '在目前執行視窗的(30, 30)位置上輸出-TextOut rtvl = TextOut(hdc, 30, 30, "TextOut", 7) |
VB.NET範例:
檢視原始碼 VB.NET
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 'VB.NET的宣告 <DllImport("gdi32.dll")> _ Shared Function TextOut( _ ByVal hdc As IntPtr, _ ByVal x As Integer, _ ByVal y As Integer, _ ByVal lpString As String, _ ByVal nCount As Integer _ ) As Boolean End Function <DllImport("user32.dll")> _ Shared Function GetDC( _ ByVal hwnd As IntPtr _ ) As IntPtr End Function Dim hdc As IntPtr '取得目前執行視窗的裝置環境代碼(Device Context) hdc = GetDC(Me.Handle) Dim rtvl As Boolean '在目前執行視窗的(30, 30)位置上輸出-TextOut rtvl = TextOut(hdc, 30, 30, "TextOut", 7) |
C#範例:
檢視原始碼 C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //C#的宣告 [DllImport("gdi32")] public static extern bool TextOut( IntPtr hdc, int x, int y, string lpString, int nCount ); [DllImport("user32")] public static extern IntPtr GetDC( IntPtr hwnd ); IntPtr hdc; //取得目前執行視窗的裝置環境代碼(Device Context) hdc = GetDC(this.Handle); bool rtvl; //在目前執行視窗的(30, 30)位置上輸出-TextOut rtvl = TextOut(hdc, 30, 30, "TextOut", 7); |
我用C#測試沒有成功耶...大大可以指導一下嗎???
還有如果要在最上層顯示(像以前網咖那樣提示時間)有辦法做到嗎???
使用時有任何錯誤嗎?
視窗沒有出現TextOut
我是放在FormLoad裡面
完全沒改過
版本Visual Studio 2010
我測試是 ok 哩, 以上面範例來看, 是會在該程式視窗輸出字串的。
如果要顯示在桌面上, 則你要先利用其它 API 取得桌面的 Handle。