Jump to page: 1 2
Thread overview
How to change button text color in NM_CUSTOMDRAW (Win32 API question)
Mar 16, 2021
Vinod K Chandran
Mar 16, 2021
Imperatorn
Mar 16, 2021
Vinod K Chandran
Mar 16, 2021
Imperatorn
Mar 16, 2021
Vinod K Chandran
Mar 16, 2021
Vinod K Chandran
Mar 16, 2021
Imperatorn
Mar 17, 2021
Vinod K Chandran
Mar 17, 2021
Imperatorn
Mar 17, 2021
Vinod K Chandran
Mar 16, 2021
Vinod K Chandran
Mar 16, 2021
Jack
March 16, 2021
Hi all,
I am creating a Button class with Win32 API functions. So far so good. I am using NM_CUSTOMDRAW message to change the back color of my buttons. It's really easy to change the back color in this way. But I can't change the text color of my button. This is my pseudo code.
```
uint setBtnBackColor( LPNMCUSTOMDRAW  lp) {

    SetTextColor(lp.hdc, RGB(102, 255, 51) )    // Not working

    if lp.uItemState & CDIS_SELECTED { //----------- btn clicked
        // Change back color using SelectObject & FillRect
        // Its Working. No probs.
        }
    elseif lp.uItemState & CDIS_HOT { //------------Mouse over
        // Change back color using SelectObject & FillRect
        // Its Working. No probs.
        }
    else { // ---------Default state of button
         // Change back color using SelectObject & FillRect
        // Its Working. No probs.
        }
    return CDRF_SKIPDEFAULT
}
```
What is wrong in my approach ?
March 16, 2021
On Tuesday, 16 March 2021 at 17:26:01 UTC, Vinod K Chandran wrote:
> Hi all,
> I am creating a Button class with Win32 API functions. So far so good. I am using NM_CUSTOMDRAW message to change the back color of my buttons. It's really easy to change the back color in this way. But I can't change the text color of my button. This is my pseudo code.
> ```
> uint setBtnBackColor( LPNMCUSTOMDRAW  lp) {
>
> [...]

Omg the pain. Are you forced to use raw win api for this?
March 16, 2021
On Tuesday, 16 March 2021 at 17:45:09 UTC, Imperatorn wrote:

>
> Omg the pain. Are you forced to use raw win api for this?

Not at all. It's my hobby project. I choose raw win api. It's a fun.

March 16, 2021
On Tuesday, 16 March 2021 at 17:26:01 UTC, Vinod K Chandran wrote:
> Hi all,
> I am creating a Button class with Win32 API functions. So far so good. I am using NM_CUSTOMDRAW message to change the back color of my buttons. It's really easy to change the back color in this way. But I can't change the text color of my button. This is my pseudo code.
> ```
> uint setBtnBackColor( LPNMCUSTOMDRAW  lp) {
>
>     SetTextColor(lp.hdc, RGB(102, 255, 51) )    // Not working
>
>     if lp.uItemState & CDIS_SELECTED { //----------- btn clicked
>         // Change back color using SelectObject & FillRect
>         // Its Working. No probs.
>         }
>     elseif lp.uItemState & CDIS_HOT { //------------Mouse over
>         // Change back color using SelectObject & FillRect
>         // Its Working. No probs.
>         }
>     else { // ---------Default state of button
>          // Change back color using SelectObject & FillRect
>         // Its Working. No probs.
>         }
>     return CDRF_SKIPDEFAULT
> }
> ```
> What is wrong in my approach ?

I'm afraid you have to do everything yourself, including draw the text and call SetTextColor() on the associated HDC (that you have used in the DrawText() for exe)
March 16, 2021
On Tuesday, 16 March 2021 at 18:27:54 UTC, Vinod K Chandran wrote:
> On Tuesday, 16 March 2021 at 17:45:09 UTC, Imperatorn wrote:
>
>>
>> Omg the pain. Are you forced to use raw win api for this?
>
> Not at all. It's my hobby project. I choose raw win api. It's a fun.

I see 😁

Do you get CLR_INVALID in return?
March 16, 2021
On Tuesday, 16 March 2021 at 18:35:00 UTC, Imperatorn wrote:

>
> I see 😁
>
> Do you get CLR_INVALID in return?

As far as i know this is the value of CLR_INVALID - 4294967295.
And these are the results i got from my function.

Set Text color result - 0
Set Text color result - 36962150

Set Text color result - -1
Set Text color result - 0
Set Text color result - 36962150
Set Text color result - 0
Set Text color result - 36962150

The first two results got when the form shown. Rest are the results of a mouse hover.
March 16, 2021
On Tuesday, 16 March 2021 at 18:35:00 UTC, Imperatorn wrote:

> I see 😁
>
> Do you get CLR_INVALID in return?

That results might be wrong. So i printed them in hex. These are the hex results.
Set Text color result - 00000000
Set Text color result - 0233FF66

Set Text color result - FFFFFFFF
Set Text color result - 00000000
Set Text color result - 0233FF66
Set Text color result - 00000000
Set Text color result - 0233FF66

Look, the third one is CLR_INVALID.
March 16, 2021
On Tuesday, 16 March 2021 at 18:35:00 UTC, Imperatorn wrote:
>
> I see 😁
>
> Do you get CLR_INVALID in return?

From that results, second one contains my color value.
Set Text color result - 0233FF66
RGB(102, 255, 51) is the color.
66 = 102
FF = 255
33 = 51


March 16, 2021
On Tuesday, 16 March 2021 at 19:15:02 UTC, Vinod K Chandran wrote:
> On Tuesday, 16 March 2021 at 18:35:00 UTC, Imperatorn wrote:
>
>> I see 😁
>>
>> Do you get CLR_INVALID in return?
>
> That results might be wrong. So i printed them in hex. These are the hex results.
> Set Text color result - 00000000
> Set Text color result - 0233FF66
>
> Set Text color result - FFFFFFFF
> Set Text color result - 00000000
> Set Text color result - 0233FF66
> Set Text color result - 00000000
> Set Text color result - 0233FF66
>
> Look, the third one is CLR_INVALID.

Some info here
https://stackoverflow.com/questions/1525669/set-static-text-color-win32/1526240
March 17, 2021
On Tuesday, 16 March 2021 at 19:42:26 UTC, Imperatorn wrote:

>

At last, i found the answer myself. There is a  item called dwDrawStage in NMCUSTOMDRAW structure. If  value of dwDrawStage is equal to CDDS_PREERASE, call SetBkMode with transparent and call SetTextColor. Then draw text  with DrawText function. And finally, return CDRF_NOTIFYPOSTPAINT.  In short, do the color changing process in pre-erase stage and return CDRF_NOTIFYPOSTPAINT constant. As per MSDN, What this constant means  is,
>>> The control will notify the parent after painting an item. This occurs when the dwDrawStage of the NMCUSTOMDRAW structure equals CDDS_PREPAINT.



« First   ‹ Prev
1 2