デスクトップに文字列を描画         <TOP>


デスクトップに文字列を描画します。

CreateDC 指定されたデバイスのデバイスコンテキストを、指定された名前で作成

DeleteDC 指定されたデバイスコンテキストを削除

SetBkMode バックグラウンドの塗りつぶしモード設定

DrawText 文字列を指定領域に出力

SetTextColor デバイスコンテキストの文字色を変更

 

文字色を指定し実行ボタンをクリックすると、指定スクリーン矩形領域に文字列を描画します。

 

 

'================================================================
'= デスクトップに文字列を描画
'=     (DrawText.bas)
'================================================================
#include "Windows.bi"

Type RECT
    Left   As Long
    Top    As Long
    Right  As Long
    Bottom As Long
End Type

#define TRANSPARENT 1                   '背景色を設定しない

' 指定されたデバイスのデバイスコンテキストを、指定された名前で作成
Declare Function Api_CreateDC& Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName$, lpDeviceName As Any, lpOutput As Any, ByVal lpInitData As Any)

' 指定されたデバイスコンテキストを削除
Declare Function Api_DeleteDC& Lib "gdi32" Alias "DeleteDC" (ByVal hDC&)

' バックグラウンドの塗りつぶしモード設定
Declare Function Api_SetBkMode& Lib "gdi32" Alias "SetBkMode" (ByVal hDC&, ByVal iBkMode&)

' 文字列を指定領域に出力
Declare Function Api_DrawText& Lib "user32" Alias "DrawTextA" (ByVal hDC&, ByVal lpStr$, ByVal nCount&, lpRect As RECT, ByVal wFormat&)

' デバイスコンテキストの文字色を変更
Declare Function Api_SetTextColor& Lib "gdi32" Alias "SetTextColor" (ByVal hDC&, ByVal crColor&)

Var Shared Group1 As Object
Var Shared Radio(2) As Object
Var Shared Button1 As Object

'================================================================
'=
'================================================================
Declare Function Index bdecl () As Integer
function Index()
    Index = Val(Mid$(GetDlgRadioSelect("Radio1"), 6)) - 1
End Function

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Group1.Attach GetDlgItem("Group1") : Group1.SetFontSize 14
    For i = 0 To 2
        Radio(i).Attach GetDlgItem("Radio" & Trim$(Str$(i + 1))) : Radio(i).SetFontSize 14
    Next
    Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

    ShowWindow -1
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var hDC As Long
    Var rct As RECT
    Var Col As Long
    Var Str As String
    Var Ret As Long

    Str = "F-Basic Programming Tips"

    'デスクトップのDC取得(第一引数を"Display"とした場合他の引数は"Null")
    hDC = Api_CreateDC("DISPLAY", ByVal 0, ByVal 0, ByVal 0)
           
    '文字を描画する矩形領域
    rct.Left = 30
    rct.Top = 54
    rct.Right = 400
    rct.Bottom = 100

    Select Case Index
        Case 0
            Col = &HFF              'Red
        Case 1
            Col = &HFF00            'Green
        Case 2
            Col = &HFF0000          'Blue
    End Select

    Ret = Api_SetBkMode(hDC, TRANSPARENT)
    Ret = Api_SetTextColor(hDC, Col)
    Ret = Api_DrawText(hDC, Str, Len(Str), rct, 0)

    Ret = Api_DeleteDC(hDC)
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End