News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

how to change background color of static control, and add text - resolved

Started by quarantined, August 14, 2022, 05:44:08 AM

Previous topic - Next topic

zedd151

Quote from: NoCforMe on August 23, 2022, 04:10:37 AM
It is sent through WM_COMMAND:
.....
(the notification code is STN_CLICKED)

Who would have thought? I just learnt something new. I wonder why no one mentioned this earlier in this thread.  :tongue:  Might have saved some time. At any rate at some point I will post here several examples of using a clickable static control. For contrast I will also post a couple of examples that don't work as might be expected. I'm currently working on another project though so it may take a little while.

NoCforMe

Glad you learnt something.

If you want to change the color of your static on being clicked (and forget bitmaps, puleeze!), you still need to handle WM_CTRLCOLORSTATIC as described earlier; you can't do that directly from STN_CLICKED, as you have no access to the control's DC then. But it's easy peasy.

I'll cook up a testbed soon as I take a break from my other current project.
Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: NoCforMe on August 23, 2022, 06:06:56 AM
I'll cook up a testbed soon as I take a break from my other current project.


Ok, lets see who finishes their current project first.  :cool:

NoCforMe

Assembly language programming should be fun. That's why I do it.

zedd151

Quote from: NoCforMe on August 23, 2022, 08:44:42 AM
I can tell you right now it ain't gonna be me ...
lol. I am kinda making slow progress here as well.

NoCforMe

OK, I took enough of a break from my other project (a frustrating one!) to put together this li'l static control testbed. It shows that for a regular (text)-type static, the control will respond to mouse clicks (via ST_CLICKED) and behave much like a button. Also shows how easy it is to change colors via WM_CTLCOLORSTATIC. Look ma, no subclassing!

The top-left cell responds to clicks (check the status bar when you do). The other one just sits there. When I get a little more time I'll put a couple bitmap statics in, see how they work.

[later, in another part of the forest]

I put in 3 bitmap statics (SS_BITMAP). This totally demolishes what somebody here said, that such statics wouldn't respond to mouse clicks. See for yourself. Works fine. One of them even flips the BMP when clicked on.

(style = SS_BITMAP | SS_NOTIFY)
Assembly language programming should be fun. That's why I do it.

TimoVJL

WinUser.h
#define STN_CLICKED  0
#define STN_DBLCLK  1
#define STN_ENABLE  2
#define STN_DISABLE  3
May the source be with you

hutch--

 :biggrin:

> This totally demolishes what somebody here said

A STATIC class control is just that, STATIC. If you do as much work on the base control to bypass its characteristics, you may as well do it properly with CreateWindowEx() with its own WNDCLASSEX structure. When you use predefined classes for Windows, you get those basic characteristics defined in the class, if you want to create a custom control, its done with a CreateWindowEx with code of your choice in the WNDCLASSEX structure and a WndProc style procedure for message processing.

You can play games like this but you are doing it for the practice, not performance.

jj2007


NoCforMe

Well, all I know is that Micro$oft, in all their wisdom, has endowed the static class of control with interactivity in the form of STN_CLICKED notifications, so hey, who are we not to take advantage of that? Who cares if it doesn't have all the functionality of a button? Works fine in this application. Just hoping I don't get a visit in the middle of the night from the Coding Police.
Assembly language programming should be fun. That's why I do it.

jj2007

STN_CLICKED is interesting, too. I have been fighting for some days now with the NM_CUSTOMDRAW notification, and would appreciate help on this. The attached code is pure Masm32 SDK (and confused - apologies :rolleyes:)

My problem: Setting the background colour of the right pushbutton works fine, but SetTextColor doesn't work (but GetLastError=0).

From the tests I've made it seems that (on Win 7 at least) the text is set somewhere between the CDDS_PREPAINT and CDDS_POSTPAINT notifications. But SetTextColor doesn't work in any of the stages :sad:

Comment out, in line 222, the mov eax, CDRF_DODEFAULT to see what happens when CDDS_POSTPAINT is active. Btw returning CDRF_SKIPDEFAULT from the prepaint event suppresses the painting of the text, but it also suppresses important functions.

See FreeBasic's kcvinu (also a Masm32 member but not active), How to change the back color of a control at runtime ?:

QuoteTo change the fore color, Just use three functions.
SetBkMode
SetTextColor
DrawText
And then return CDRF_NOTIFYPOSTPAINT.
If you returned any other values, the color won't change.
So there is no need to use BS_OWNERDRAW style.

I can't confirm that so far :cool:

zedd151

Quote from: jj2007 on August 24, 2022, 06:51:40 AM
SetTextColor doesn't work (but GetLastError=0).



        include \masm32\include\masm32rt.inc


        WinMain     proto
        CreateMain  proto :dword, :dword, :dword, :dword
        CenterMain  proto :dword, :dword, :dword
        WndProc     proto :dword, :dword, :dword, :dword


    .data
        hInstance   dd 0
        hWnd        dd 0
        ww          dd 800
        wh          dd 600
        h11         dd 0
        selected    dd 1
        statClass   db "STATIC", 0
        Class       db "Window_CLASS", 0
        DispName    db "Untitled", 0


    .code


    cell proc par:dword, x:dword, y:dword, id:dword
    local cn:dword, ws:dword, hi:dword, hndl:dword, hfnt:dword
        mov eax, hInstance
        mov hi, eax
        lea eax, statClass
        mov cn, eax
        mov ws, WS_VISIBLE
         or ws, WS_CHILD
         or ws, SS_CENTER
         or ws, SS_NOTIFY
        invoke CreateWindowEx, 0, cn, 0, ws, x, y, 64, 64, par, id, hi, 0
        mov hndl, eax
        fn RetFontHandle, "tahoma", 56, 400
        mov hfnt, eax
        invoke SendMessage, hndl, WM_SETFONT, hfnt, 0
        mov eax, hndl
       
        ret
    cell endp
   
    WinMain proc
    local msg:MSG
        invoke CreateMain, 0, addr Class, addr WndProc, 0
        mov hWnd, eax
       
        invoke cell, hWnd, 4, 4, 211  ;; create static window standard "STATIC" class
        mov h11, eax
       
        fn SetWindowText, h11, "8"    ;; set text into static control for demonstration of issue
        invoke CenterMain, hWnd, ww, wh
        invoke ShowWindow, hWnd, SW_SHOWNORMAL
        invoke UpdateWindow, hWnd
        invoke SetWindowText, hWnd, addr DispName
      StartLoop:
        invoke GetMessage, addr msg, 0, 0, 0
        cmp eax, 0
        je ExitLoop
        invoke TranslateMessage, addr msg
        invoke DispatchMessage, addr msg
        jmp StartLoop
      ExitLoop:
        invoke ExitProcess, 0
        ret
    WinMain endp


    WndProc proc hWin:dword, uMsg:dword, wParam:dword, lParam:dword
      .if uMsg == WM_COMMAND
        .if wParam == 211
          .if selected == 0    ;;  if cell not selected, select it
            mov selected, 1
            invoke InvalidateRect, h11, 0, TRUE
          .elseif selected == 1  ;;  if cell is selected, deselect it
            mov selected, 0
            invoke InvalidateRect, h11, 0, TRUE
          .endif
        .endif
      .elseif uMsg==WM_CTLCOLORSTATIC
      .if selected == 0                       ;; check if cell is selected
        invoke SetBkMode, wParam, TRANSPARENT
        invoke SetTextColor, wParam, 00FF0000h                                 ;; SetTextColor
        invoke CreateSolidBrush, 00FFD0E0h    ;; set color accordingly
        ret
      .elseif selected == 1
        invoke SetBkMode, wParam, TRANSPARENT
        invoke SetTextColor, wParam, 000000FFh                                 ;; SetTextColor
        invoke CreateSolidBrush, 00FFFFFFh    ;; set color accordingly
        ret
      .endif
      .elseif uMsg == WM_CLOSE
        invoke PostQuitMessage, 0
        return 0
      .endif
        invoke DefWindowProc, hWin, uMsg, wParam, lParam
        ret
    WndProc endp


    CreateMain proc hI:dword, cn:dword, wp:dword, ic:dword
    local wc:WNDCLASSEX, hIcon:dword, rct:RECT
        mov hIcon, 0
        cmp ic, 0
        jz @f
        invoke LoadIcon, hI, ic
        mov hIcon, eax
        @@:
        mov wc.cbSize, sizeof WNDCLASSEX
        mov wc.style, CS_BYTEALIGNWINDOW
        mrm wc.lpfnWndProc, wp
        mov wc.cbClsExtra, 0
        mov wc.cbWndExtra, 0
        mrm wc.hInstance, hI
        mov wc.hbrBackground, COLOR_BTNFACE+2
        mov wc.lpszMenuName, 0
        mrm wc.lpszClassName, cn
        mrm wc.hIcon, hIcon
        invoke LoadCursor, 0, IDC_ARROW
        mov wc.hCursor, eax
        mrm wc.hIconSm, hIcon
        invoke RegisterClassEx, addr wc
        invoke CreateWindowEx, 528, cn, 0, 0CF0000h, 0, 0, 0, 0, 0, 0, 0, 0
        ret
    CreateMain endp


    CenterMain proc hWin:dword, wd:dword, ht:dword
    local rct:RECT, x:dword, y:dword
        invoke SystemParametersInfoA, SPI_GETWORKAREA, 0, addr rct, 0
        mov eax, rct.right
        sub eax, wd
        shr eax, 1
        mov x, eax
        mov eax, rct.bottom
        sub eax, ht
        shr eax, 1
        mov y, eax
        invoke MoveWindow, hWin, x, y, wd, ht, TRUE
        ret
    CenterMain endp


    end WinMain

You're welcome.   :cool:


You could try to modify the code to fit what you need to do

jj2007

Quote from: Swordfish on August 24, 2022, 07:04:11 AMYou're welcome.   :cool:

You could try to modify the code to fit what you need to do

Nice code but far from what I want to achieve. A static control does not give the feedback that a user expects from a pushbutton.

zedd151

Quote from: jj2007 on August 24, 2022, 07:11:28 AM
A static control does not give the feedback that a user expects from a pushbutton.
Yes you are right I guess.
But have you tried implementing for your button, the same way as has been done for the static in my example?


jj2007

Check my code, swordfish. I am using the NM_CUSTOMDRAW notification, which is more complicated but the official M$ way to do it.