Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

Transparent text Box by CreateWindowEx(edit)

$
0
0
How to Transparent text Box by CreateWindowEx(edit)?

.BackgroundBrush = CreatePatternBrush(Form1.Image1.Picture.Handle) 'IT'S good
CODE FROM HERE

YOU GUYS MUST BE SMARTER THAN THIS...-VBForums
https://www.vbforums.com/showthread....92#post5516192

Code:

Option Explicit
Private Const GWL_EXSTYLE = -20
Private Const WS_EX_LAYERED = &H80000

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongW" ( _
    ByVal hwnd As Long, _
    ByVal nIndex As Long) As Long


Private Const NULL_BRUSH = 5
Private Const HOLLOW_BRUSH = NULL_BRUSH
Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long

Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function SetBkMode Lib "gdi32.dll" ( _
  ByVal hdc As Long, _
  ByVal nBkMode As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function GetBkColor Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Const WS_EX_CLIENTEDGE = &H200
Private Const WS_CHILD = &H40000000
Private Const SW_SHOWNORMAL = 1
Private Const GWL_WNDPROC = (-4)
Private Const WM_CTLCOLOREDIT = &H133
Private Const WM_CTLCOLORSTATIC = &H138

Private lFormWndProc As Long

Public Type Editbox
    hwnd As Long
    ForeColor As Long
    BackgroundBrush As Long
    Index As Long
End Type

Private tEditBoxes() As Editbox
Private lEditBoxCount As Long
Dim TxtHwnd As Long
Public Const TRANSPARENT As Long = 1

Public Function FormWindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim tEditBox As Editbox
   
    If Msg = WM_CTLCOLOREDIT Then
    'CTLCOLOR_EDIT:  //对所有编辑框控件的设置
   
        tEditBox = GetEditBox(lParam)
        'lParam就是控件句柄
        If tEditBox.hwnd Then
            With tEditBox
            'Debug.Print "do tEditBox"
                Dim OldBKMode As Long
                OldBKMode = SetBkMode(wParam, TRANSPARENT)

                Call SetTextColor(wParam, .ForeColor)
                If .BackgroundBrush Then
                    Debug.Print "Delete_BackgroundBrush"
                    Call DeleteObject(.BackgroundBrush)
                End If
                Debug.Print "set BackgroundBrush"
 

'                .BackgroundBrush = CreateSolidBrush(GetBkColor(wParam)) 
              .BackgroundBrush = CreatePatternBrush(Form1.Image1.Picture.Handle)


                'CreateSolidBrush(GetStockObject(HOLLOW_BRUSH))
                'CreateSolidBrush (GetBkColor(wParam))

                FormWindowProc = .BackgroundBrush
            End With
            Exit Function
        End If
'    ElseIf Msg = WM_CTLCOLORSTATIC Then
'        Debug.Print "WM_CTLCOLORSTATIC"
    End If
    FormWindowProc = CallWindowProc(lFormWndProc, hwnd, Msg, wParam, lParam)
End Function

Public Function SubClassForm(ByVal hwnd As Long) As Boolean
    If lFormWndProc Then Exit Function
    lFormWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf FormWindowProc)
    SubClassForm = True
End Function

Public Function RemoveFormSubclassing(ByVal hwnd As Long) As Boolean
    If lFormWndProc Then Exit Function
    Call SetWindowLong(hwnd, GWL_WNDPROC, lFormWndProc)
    RemoveFormSubclassing = True
End Function

Public Function CreateEditbox(ByVal ParentHwnd As Long, ByVal Left As Long, ByVal Top As Long, ByVal Width As Long, ByVal Height As Long) As Editbox
    Dim lHwnd As Long
   
    lHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "", WS_CHILD, Left, Top, Width, Height, ParentHwnd, 0&, App.hInstance, 0&)
   
    If lHwnd = 0 Then Exit Function
    TxtHwnd = lHwnd
'    SetWindowLong TxtHwnd, _
'                  GWL_EXSTYLE, _
'                  GetWindowLong(TxtHwnd, GWL_EXSTYLE) Or WS_EX_LAYERED ' Or WS_DISABLED
                 
    Call ShowWindow(lHwnd, SW_SHOWNORMAL)
   
    lEditBoxCount = lEditBoxCount + 1
   
    ReDim Preserve tEditBoxes(lEditBoxCount)
   
    tEditBoxes(lEditBoxCount).hwnd = lHwnd
    tEditBoxes(lEditBoxCount).ForeColor = vbBlack
    tEditBoxes(lEditBoxCount).Index = lEditBoxCount
    CreateEditbox = tEditBoxes(lEditBoxCount)
End Function

Public Function GetEditBox(ByVal hwnd As Long) As Editbox
    Dim lIndex As Long
    For lIndex = 0 To lEditBoxCount
        If tEditBoxes(lIndex).hwnd = hwnd Then Exit For
    Next
    If lIndex <= lEditBoxCount Then
        GetEditBox = tEditBoxes(lIndex)
    End If
End Function

Public Function SetEditboxForeColor(ByVal Index As Long, ByVal Color As ColorConstants) As ColorConstants
    If Index > lEditBoxCount Then Exit Function
    tEditBoxes(Index).ForeColor = Color
    SetEditboxForeColor = tEditBoxes(Index).ForeColor
End Function

in form1:
Code:

Private Sub Form_Load()
    Me.Picture = LoadPicture("D:\Data2\03编程临时测试资料\01图片\0006背景图.jpg")
    Dim tNewEditBox As Editbox
   
    SubClassForm hwnd
    tNewEditBox = CreateEditbox(hwnd, 10, 10, 150, 28)
    Call SetEditboxForeColor(tNewEditBox.Index, vbRed)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    RemoveFormSubclassing hwnd
End Sub


Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>