www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to change button text color in NM_CUSTOMDRAW (Win32 API question)

reply Vinod K Chandran <kcvinu82 gmail.com> writes:
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 ?
Mar 16 2021
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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?
Mar 16 2021
parent reply Vinod K Chandran <kcvinu82 gmail.com> writes:
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.
Mar 16 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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?
Mar 16 2021
next sibling parent Vinod K Chandran <kcvinu82 gmail.com> writes:
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.
Mar 16 2021
prev sibling next sibling parent reply Vinod K Chandran <kcvinu82 gmail.com> writes:
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.
Mar 16 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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
Mar 16 2021
parent reply Vinod K Chandran <kcvinu82 gmail.com> writes:
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.
Mar 16 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Wednesday, 17 March 2021 at 00:51:05 UTC, Vinod K Chandran 
wrote:
 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.
Good that you solved it, that wasn't what I thought the solution would be 👀 Was more than 10 years ago since I was "the king of win api" 😔
Mar 16 2021
parent Vinod K Chandran <kcvinu82 gmail.com> writes:
On Wednesday, 17 March 2021 at 06:39:26 UTC, Imperatorn wrote:
 Good that you solved it, that wasn't what I thought the 
 solution would be 👀
I was sure about i can solve this through NM_CUSTOMDRAW. Because, in VB .net, we can change back color & fore color of button. On the same time, there is an option to turn on the OwnerDrawStyle. If we set this property true, we need to draw the button on our own. So i am pretty sure that, that property will turn a normal button to an owner drawn button. But if we don't use that property, then also we can change the button colors. So that means, without using BS_OWNERDRAW style, we can change the colors. And that's the NM_CUSTOMDRAW message. Unfortunately, there is not much tutorials or documentation about handling this message in a Button's case. We can find some examples and articles related to ListView & Treeview. But not a single line of documentation about buttons.
 Was more than 10 years ago since I was "the king of win api" 😔
Glad to know that. Can you write an article about how to use Gdi+ in win api apps ? Now I am using gdi. But i want to test how gdi+ works on win32 apps.
Mar 17 2021
prev sibling parent Vinod K Chandran <kcvinu82 gmail.com> writes:
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
Mar 16 2021
prev sibling parent Jack <jckj33 gmail.com> writes:
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)
Mar 16 2021