SetTextColor





VB.Net


Imports System.Runtime.InteropServices

Public Class Form1

    Private Declare Function SetTextColor Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal crColor As Integer) As Integer

    <DllImport("gdi32")>
    Private Shared Function TextOut(ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal textstring As String, ByVal charCount As Integer) As Boolean
    End Function

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        ' Create a new Graphics object
        Dim g As Graphics = Graphics.FromHwnd(TextBox1.Handle)

        ' Get the device context (HDC) from the Graphics object
        Dim hdc As IntPtr = g.GetHdc()

        ' Set the text color to red
        SetTextColor(hdc, &HFF) ' FF is the hex value for red

        ' Use the HDC to draw text
        ' g.DrawString("Hello World!", New Font("Arial", 12), Brushes.Red, New Point(10, 10))
        TextOut(hdc, 10, 10, "Hello World!", 12)

        ' Release the HDC
        g.ReleaseHdc(hdc)
    End Sub
End Class