Solved

Setting the BackColor of a Static Window !!!

Posted on 2006-06-18
7
543 Views
Last Modified: 2009-12-16
I have created a Static Window using the "CreateWindowEX" API and it works just fine.

Now, how do I set it's back color to Red  ?

I have tried SetBckColor and other Functions but no luck .

Regards.
0
Comment
Question by:RAFAAJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:RAFAAJ
ID: 16933060
Anybody ??
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 16937094
You will probably have to subclass that window and when it receives a message to repaint itself, use FillRect() and TextOut() to render your backcolor and text.

Otherwise.. if you do force a one-time backcolor change, it'll be empty when the window repaints itself (ie: minimized then shown again).
0
 

Author Comment

by:RAFAAJ
ID: 16937993
Yes But how do you actually do that ?

I have alraedy tried all that but I faild . I must be making some mistake.

Thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RAFAAJ
ID: 16947315
I can't beleive I can't get an answer to what seems to be ( and should be ) an easy\basic thing to do.!!!!!

I have somehow managed to do that via the "FillRect" API  but the existing
Text on the Static Window gets burried under the fill and can't be seen !

Anyone has a fix or maybe a better approach to achieve this ?

Here is a small part of the code I have so far:


'\ Goes In an Excel  Standard Module
Sub CreateStaticWnd()

    udtR = GetMyRangeRect(Range("D10"))
   
    lngControlHndl = CreateWindowEx(WS_EX_NOACTIVATE + WS_EX_TOOLWINDOW,
"STATIC", _
    "Hello!", SS_CENTER + WS_CHILD, udtR.left, udtR.top, _
    (udtR.Right - udtR.left), udtR.Bottom - udtR.top, _
    Application.hwnd, 0, Application.hInstance, 0)
   
    SetParent lngControlHndl, Application.hwnd
   
    ShowWindow lngControlHndl, SW_NORMAL
   
    hdc = GetDC(lngControlHndl)
    mBrush = CreateSolidBrush(vbRed)

    '\ this covers up the whole client area and hides the text too !
    FillRect hdc, udtR, mBrush

End Sub

I have also tried trapping the "WM_CTLCOLORSTATIC" Message through
Subclassing but can't make it work either :(

So to recap : How do I set the backgroud Color of the Client Area of a
Custom Window ( in this case it's a Static Wnd) without affecting its
Children or Text ?

Help please.

Regards.

0
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 125 total points
ID: 16947933
>> I can't get an answer to what seems to be ( and should be ) an easy\basic thing to do
If you had to ask this question, then it must not have been as easy/basic as you thought, no?

>>but the existing Text on the Static Window gets burried
Yes, that's why I said you have to paint both the background (FillRect) *and* text (TextOut) but if you don't do this via subclassing WM_PAINT, it's pointless.

This works for me...


Form1:
----------------
Option Explicit

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 SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function UpdateWindow Lib "user32" (ByVal hWnd As Long) As Long

Private Const WS_CHILD As Long = &H40000000

Private m_ctl As Long
Private Sub Form_Load()
    m_ctl = CreateWindowEx(0, "STATIC", vbNullString, WS_CHILD, 0, 0, 100, 20, Me.hWnd, 0, App.hInstance, ByVal 0)
    If (m_ctl <> 0) Then
        Call SetWindowText(m_ctl, ByVal "test")
        Call Module1.HookWindow(True, m_ctl)
        Call ShowWindow(m_ctl, 1)
        Call UpdateWindow(m_ctl)
    End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
    Call Module1.HookWindow(False, m_ctl)
End Sub


Module1:
-------------------
Option Explicit

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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function BeginPaint Lib "user32" (ByVal hWnd As Long, lpPaint As PAINTSTRUCT) As Long
Private Declare Function EndPaint Lib "user32" (ByVal hWnd As Long, lpPaint As PAINTSTRUCT) As Long
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hbrush As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long

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

Private Type PAINTSTRUCT
    hdc As Long
    fErase As Long
    rcPaint As RECT
    fRestore As Long
    fIncUpdate As Long
    rgbReserved(32) As Byte
End Type


Private Const GWL_WNDPROC = (-4)
Private Const WM_PAINT = &HF

Private m_PrevProc As Long
Public Sub HookWindow(ByVal bTrue As Boolean, ByVal hWnd As Long)
    If (bTrue = True) Then
        m_PrevProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf HookProc)
    Else
        Call SetWindowLong(hWnd, GWL_WNDPROC, m_PrevProc)
    End If
End Sub
Private Function HookProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Select Case uMsg
        Case WM_PAINT
            Dim ps As PAINTSTRUCT
            Dim hdc As Long
            hdc = BeginPaint(hWnd, ps)
            '--> draw background
            Dim rc As RECT
            Dim hbrush As Long
            Call GetClientRect(hWnd, rc)
            hbrush = CreateSolidBrush(vbBlue)
            Call FillRect(hdc, rc, hbrush)
            Call DeleteObject(hbrush)
            '--> draw text
            Dim txt As String
            'get text
            txt = GetWndText(hWnd)
            'set backcolor of text
            Call SetBkColor(hdc, vbGreen)
            'set color of text
            Call SetTextColor(hdc, vbRed)
            'draw text
            Call TextOut(hdc, 0, 0, txt, Len(txt))
            Call EndPaint(hWnd, ps)
    End Select
    HookProc = CallWindowProc(m_PrevProc, hWnd, uMsg, wParam, lParam)
End Function
Private Function GetWndText(ByVal hWnd As Long) As String
    Dim txt As String
    Dim txtlen As Integer
    txt = Space$(255)
    txtlen = GetWindowText(hWnd, txt, 255)
    GetWndText = Left$(txt, txtlen)
End Function
0
 

Author Comment

by:RAFAAJ
ID: 16950529
zzzzzooc .

Thank you very much indeed for the Snipet Code...I have tried it and it DOES WORK !!...Hopefully, it will need some tweaking and should work fine for my project.

Also, I will study this code more closely to help me get an insight into the inner workings of Windows Painting & Graphics.

Can you suggest a good Book or Website Source on Windows Graphics\Painting in VB ?

Again, Thanks for your patience with this.
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 16956160
"Painting and Drawing"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/pantdraw_3wtj.asp

MSDN is primarily the only reference I use. You can locate all of the functions, types and constants used above through that, and it'll provide you documentation on mostly everything you'll need to know (and examples -- usually in C++).
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding to a VBA? 6 91
Modifying Conditional Format from VBA code 3 84
Add and format columns in vb6 7 84
Problem to line 23 80
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question