Solved

Setting the BackColor of a Static Window !!!

Posted on 2006-06-18
7
546 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month4 days, 10 hours left to enroll

636 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