Pelles C forum

C language => User contributions => Topic started by: WiiLF23 on January 07, 2024, 08:43:42 AM

Title: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 07, 2024, 08:43:42 AM
This will turn on Dark Mode for the window title bar.

Code: [Select]
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")

#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif

static INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg)
    {
        case WM_INITDIALOG:
        {
                // Enable dark mode title bar
                BOOL value = TRUE;
                DwmSetWindowAttribute(hwndDlg, DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));

                ...

https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes#enable-a-dark-mode-title-bar-for-win32-applications

Toggle DarkMode/LightMode

Code: [Select]
BOOL isDarkModeEnabled = FALSE;

void ToggleDarkMode(HWND hWnd) {
    isDarkModeEnabled = !isDarkModeEnabled;

    // Enable or disable dark mode title bar
    BOOL value = isDarkModeEnabled;
    DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));

    RECT rc;
    GetWindowRect(hWnd, &rc);

    if (isDarkModeEnabled) {
        // Increase window size by 1 pixel
        MoveWindow(hWnd, rc.left, rc.top, rc.right - rc.left + 1, rc.bottom - rc.top + 1, TRUE);
    } else {
        // Decrease window size by 1 pixel
        MoveWindow(hWnd, rc.left, rc.top, rc.right - rc.left - 1, rc.bottom - rc.top - 1, TRUE);
    }
}

Turning Some Elements Dark
Undocumented - https://stackoverflow.com/a/53545935/2694720

Code: [Select]
#include <uxtheme.h>
#pragma comment(lib, "uxtheme.lib")

void ApplyDarkModeToControls(HWND hwnd, BOOL darkMode)
{
    HWND hChild = GetWindow(hwnd, GW_CHILD);

    while (hChild)
    {
        if (darkMode)
        {
            SetWindowTheme(hChild, L"DarkMode_Explorer", NULL);
        }
        else
        {
            SetWindowTheme(hChild, NULL, NULL);
        }

        ApplyDarkModeToControls(hChild, darkMode);
        hChild = GetWindow(hChild, GW_HWNDNEXT);
    }
}

void ToggleDarkMode(HWND hWnd)
{
    isDarkModeEnabled = !isDarkModeEnabled;

    // Enable or disable dark mode title bar
    BOOL value = isDarkModeEnabled;
    DwmSetWindowAttribute(hWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));

    // Apply the theme to all child controls recursively
    ApplyDarkModeToControls(hWnd, isDarkModeEnabled);

    RECT rc;
    GetWindowRect(hWnd, &rc);

    if (isDarkModeEnabled)
    {
        MoveWindow(hWnd, rc.left, rc.top, rc.right - rc.left + 1, rc.bottom - rc.top + 1, TRUE);
    }
    else
    {
        MoveWindow(hWnd, rc.left, rc.top, rc.right - rc.left - 1, rc.bottom - rc.top - 1, TRUE);
    }
}

I hope to actually implement a full window + full control solution

https://gist.github.com/rounk-ctrl/b04e5622e30e0d62956870d5c22b7017

Enjoy!
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on January 07, 2024, 09:22:39 PM
Hi WiiLF23,

Thanks for the example and links.  I've not used uxtheme.lib so nice to see an example. 

In a program I gave the user the ability to create and save 5 themes themselves not dependent on the system theme but with some limitations. like the toolbar and menu.  Maybe I'll try uxtheme for them.

John Z
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 08, 2024, 02:17:28 AM
Hi WiiLF23,

Thanks for the example and links.  I've not used uxtheme.lib so nice to see an example. 

In a program I gave the user the ability to create and save 5 themes themselves not dependent on the system theme but with some limitations. like the toolbar and menu.  Maybe I'll try uxtheme for them.

John Z

Most welcome! I am currently implementing the full experience and I have dark mode for Menu now. The only issue im having, is applying the menu theme by calling when desired. Menu only darkens when I call in WM_INITDIALOG message.

Current progress, working successfully for full menu dark mode:

Code: [Select]
typedef BOOL(WINAPI* fnShouldAppsUseDarkMode)();
typedef BOOL(WINAPI* fnAllowDarkModeForWindow)(HWND hWnd, BOOL allow);
typedef DWORD(WINAPI* fnSetPreferredAppMode)(DWORD appMode);

enum PreferredAppMode
{
    Default,
    AllowDark,
    ForceDark,
    ForceLight,
    Max
};

HMODULE hUxtheme;

fnShouldAppsUseDarkMode ShouldAppsUseDarkMode;
fnAllowDarkModeForWindow AllowDarkModeForWindow;
fnSetPreferredAppMode SetPreferredAppMode;

void InitDarkMode()
{
    hUxtheme = LoadLibraryExW(L"uxtheme.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
    if (hUxtheme)
    {
        ShouldAppsUseDarkMode = (fnShouldAppsUseDarkMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(132));
        AllowDarkModeForWindow = (fnAllowDarkModeForWindow)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(133));
        SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
    }
}

WM_INITDIALOG:
Code: [Select]
InitDarkMode();
SetPreferredAppMode(ForceDark); // options in enum

WM_DESTROY:
Code: [Select]
FreeLibrary(hUxtheme);

(https://i.ibb.co/bWRDc0W/Dark-Mode-Testing.jpg)

(https://i.ibb.co/X530Vx7/Dark-Mode-Testing-buttons.jpg)

(https://i.ibb.co/WGvjVn7/Dark-Mode-Testing-scrollbar.jpg)
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on January 08, 2024, 05:03:57 AM
Hi WiiLF23,

The only issue im having, is applying the menu theme by calling when desired. Menu only darkens when I call in WM_INITDIALOG message.

Most Menu changes need to be followed up by  DrawMenuBar(gHWND);  might be worth a try.

John Z
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 08, 2024, 05:49:01 AM
Redraw, Invalidate, DrawMenu all fail on the parent window. Its a strange issue. I read that Microsoft decided not to expose a lot of the API for dark mode for winapi so a lot of people have discussed it over time, not very happy with their findings etc but its intended for UWP apps, so its short of a miracle to get it this far at the moment.


At this point, I will keep the current dark controls possible, and paint the window and the rest of the controls accordingly. Starting to learn about this as we speak, I think this is the best option.

Code: [Select]
case WM_PAINT: {
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwndDlg, &ps);

    // Set the background color to dark (black)
    SetBkColor(hdc, RGB(0, 0, 0));
    ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &ps.rcPaint, NULL, 0, NULL);

    EndPaint(hwndDlg, &ps);
    return 0;
}
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on January 08, 2024, 07:01:58 AM
Hi WiiLF23,

I don't use WM_PAINT for colors.  Capturing and processing WM_CTLxxxx messages allows coloring everything except the toolbar background, the menu, and the outer dialog frame. 

Attached amateur spaghetti code excerpts are how I give users their own theme choices.
There is a minor complication in it in that the program also uses Blue as a notification so a user choosing the same blue has to be checked.  Otherwise straight forward capture and control.  There is also a separate procedure for child windows of the main form.
 
Probably does not add to your knowledge and capability but worth a shot.  :)

John Z

btw any color change is instantly reflected in the window . . . .
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 08, 2024, 07:38:06 AM
Wow nice, I can certainly work with that , in fact that just about finalized the dark approach. Very interesting, thank you
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on January 08, 2024, 08:36:06 AM
Hi WiiLF23,

Hope it helps.
In mine the user can set the colors at will so here is how the brushes are created once they select the color and it implements immediate .  The cases are from the menu dropdown shown.  Again excerpted from main message loop.  You will probably do a bit different if limited 'theme' choices.

John Z
Title: Re: Enable Dark Mode for Title bar
Post by: MrBcx on January 08, 2024, 03:48:28 PM
The attached screenshot shows the progress that Armando Rivera and I made putting together some plug and play Dark Mode routines about a year ago.  The app in the screenshot was written by me over 20 years ago but converted to Dark Mode last year, as a proof of concept

The translated BCX code compiles and displays as shown using Pelles C v12. 
It also compiles and runs correctly using VS2022 with the Clang compiler.

Discussion thread: https://bcxbasiccoders.com/smf/index.php?topic=826.0

Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 08, 2024, 09:45:38 PM
Thank you MrBcx, I have migrated the responsible code to a new dialog-based project, reducing a lot (cross-compiler declarations and other bits). Its also fully converted to handle Unicode.

Here is the project file for a basic GUI window.

I would like to clean it up, I realize a lot of what is left over is conversion support.

(https://i.ibb.co/7ND0bgf/Dark-Mode-Pelles-C-GUI.jpg)
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 08, 2024, 10:12:41 PM
Update:

Much cleaner, and right down to dark mode rendering I am after! This is perfect.

- Updated to be DPI aware (font smoothing). Fixes pixilated font edges.
- Removed redundant code  from source provided by MrBCX.

Enjoy!
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on January 08, 2024, 11:03:16 PM
The attached screenshot shows the progress that Armando Rivera and I made putting together some plug and play Dark Mode routines about a year ago.  The app in the screenshot was written by me over 20 years ago but converted to Dark Mode last year, as a proof of concept

The translated BCX code compiles and displays as shown using Pelles C v12. 
It also compiles and runs correctly using VS2022 with the Clang compiler.

Discussion thread: https://bcxbasiccoders.com/smf/index.php?topic=826.0

Thanks MrBCX and Mr. Rivera!  Allowing use with attribution?

I tried just the Menu change procedure (which I could never get working before) in my program. Added MIM_APPLYTOSUBMENUS and seems to work but I've a little more to do in my case.  Also my font color does not carry over.

As always MrBCX provides education!

Thanks WiiLF23 - I got your functional extraction version too. Fairly sure I'm going to stick with my user defined system independent themes method but maybe I'll be able to set menu font color and toolbar colors now.

John Z
Title: Re: Enable Dark Mode for Title bar
Post by: MrBcx on January 08, 2024, 11:27:40 PM

Allowing use with attribution?


Consider it Public Domain.  Attribution is always appreciated too.
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 08, 2024, 11:42:33 PM
Many thanks, sorry I tend to piece together fast - I also dont leave many code comments at all as you notice, will be improving on this as I go.

Currently adding a Light/Dark toggle. 50% done but a few caveats
Title: Re: Enable Dark Mode for Title bar
Post by: MrBcx on January 08, 2024, 11:47:38 PM
Many thanks, sorry I tend to piece together fast - I also dont leave many code comments at all as you notice, will be improving on this as I go.

Currently adding a Light/Dark toggle. 50% done but a few caveats

No worries! 

Knowledge, similar to light, works best when it is diffused.
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 09, 2024, 03:28:34 AM
Done. Dark/Light mode toggle function to switch between modes. Working well it appears.

(https://i.ibb.co/d4WKYPN/toggle-1.jpg)

(https://i.ibb.co/56gCwr0/toggle-2.jpg)
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on January 09, 2024, 05:16:04 AM
Hi WiiLF23,

Great you are on the way now.....

Don't forget you'll have to add in the treeview case for your program.

Doesn't look like it will be much help to my program as I want a finer level of control for the user.  However it is very instructive.  The menu part only partially works in my code because I have used the menu checkbox feature.....so for now I'll keep the system menu as is.
I will try to see if the toolbar color can changed - in the past it had to be done using a superimposed bitmap of the color desired.

John Z

Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 09, 2024, 08:04:07 AM
Sounds good - very pleased with it! One thing I had to do was catch the owner drawn root item highlight event, or color it and then keep it colored when menu is focused.

https://stackoverflow.com/a/69441170/2694720

Working exactly like Notepad++ in dark mode.

(https://i.ibb.co/4M9sxGr/Clipboard02.jpg)

They thing I had to do was capture ODS_SELECTED, which had it running around for a while. The git link was a very helpful.

- Explorer-style default menu hover highlight color (top items)
- Dark mode appropriate menu hover highlight color (top items) w/ selection state

Updated ZIP
Title: Re: Enable Dark Mode for Title bar
Post by: MrBcx on January 09, 2024, 03:27:33 PM
Good job -- your toggle switch is a particularly useful addition.  Thanks!

Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 09, 2024, 05:56:40 PM
Most welcome!

But now we must support dark mode for Windows 7 - Windows 11. Now, dark mode is working in Windows 7 Basic, including menus and window background but I need to implement a fallback in absence of the uxtheme calls for native dark mode and paint custom if not supported. This is where John's code will come in nicely as fallback support.

Notepad++ does not paint dark contextmenu's in Windows 7 Basic, which is easily done without question so we can take that a step further for the HMENU non-client area.

Basically, there will be dark mode on all supported versions of Windows. At least those that support the APIs in use here.

What is happening, is the newer common controls UI is being painted on older versions, due to lack of detection.

ODS_SELECTED + ODS_HOTLIGHT need to be used but they need to consider the OS version, and fallback to a reasonable choice.
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on January 09, 2024, 09:33:33 PM
Hi WiiLF23,

but I need to implement a fallback

Fallbacks - I love it!  I do that as well for ex if GDI+ fails the program falls back to OLE.
If memory based processing can't get the memory it needs I fall back to disk based processing.

John Z
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 10, 2024, 12:47:30 AM
Haha ;D exactly!  Its possible, so why not make it work right?

Windows 7 basic

(https://i.ibb.co/vLqG8kN/Win7-Fallback.jpg)

---

Also added the ability to force the dark mode effect fallback - good for testing to know what users will generally see on their end. on older versions of windows. Some compatibility efforts are involved, but never say never :p

- Windows XP+ client-area dark mode (mostly, controls to be confirmed)
- Menu subitem tile handling for MF_OWNERDRAW sizing
- Other fixes and updates, shorter functions, cleaner code

(https://i.ibb.co/dprpH8W/4.jpg)


Updated ZIP
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on January 10, 2024, 02:28:47 AM
For those following the progress, update the following event:

Code: [Select]
case ONOFF: {
    ToggleDarkMode(hwndDlg);
    darkModeFallback = IsDlgButtonChecked(hwndDlg, CHECKBOX_FALLBACK);

    SetWindowTheme(hwndDlg, isDarkModeEnabled ? L"Explorer" : L"DarkMode_Explorer", NULL);
    InvalidateRect(hwndDlg, NULL, TRUE);
    UpdateWindow(hwndDlg);

    return TRUE;
}

This will allow full redraw of the window for older versions. Required for Windows 7, and probably XP as well. Windows 8/8.1 is unknown if it updates right away or not. Someone will have to confirm that.

UPDATE:

Owner-drawn tabs. This appeared to be necessary, as the tab control visibility was not great in dark mode so I added a custom paint event and added tab focus highlight effect.

(https://i.ibb.co/6YgMj2Y/fulldraw.jpg)
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on January 29, 2024, 11:06:23 PM
Hi WiiLF23,

Question for you - In your Dark Mode implementation did/do you invert the color of the normal Edit mouse IDC_IBEAM ?  Or maybe you have no user inputs edit boxes?

It inverts itself on the windows background but inside the edit box it appears black and not inverting the color there making it hard to see.  So I'm looking at making IDC_IBEAM default to white when Dark Mode is active.

John Z

So it looks like my best approach is to add a custom cursor, easy enough to do.  Then when in dark mode in an edit box the IBEAM will be a white one....
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on February 04, 2024, 01:37:22 AM
Hi John Z, correct you will need to invert the cursor color and called SetCursor(...) for the darkmode conditional. I do this on [X] close icons on custom drawn elements in my controls.

I havent implemented this into the main application yet, as demo is still in project folder elsewhere. I set it and left it.

Since that time, I have figured out how to completely darken/customize TabControl, TreeView, etc.
(https://i.ibb.co/3y8snVn/tv-draw.jpg)

You can see here it is easily possible to draw rows and states in TreeView (took me an hour). Also, customizing dark windows is also very easy as this example shows a splash tutorial on a one-time timer.

(https://i.ibb.co/x1Rr6hf/capture-007-03022024-162731.jpg)

^ This is just a BMP within a dialog window. But there is a lot of control logic for the window, drag event, close/mouseover, capture handling, etc. It works perfectly fine. Close button turns red with WM_MOUSEMOVE etc (native button control).

This all combined, can produce a much better dark mode experience - along with the tab drawing.

IntroDialog.zip will get you started with the splash feature.
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on February 04, 2024, 08:10:41 AM
Thanks WiiLF23!

I've got a fairly decent Dark Mode toggle.
Since I had previously coded for 5 custom user defined
themes it was not a big leap to add a Dark Mode.
I looked at a Menu in dark mode, easy to do, but decided
not.  So I left the main dialog frame, menu, toolbar and
status bar as is.

I completed a white IBEAM which works well in DarkMode.
Started on the white cursor bar but was having problems, it
showed on the main form but not in the edit boxes. Probably
wrong handle or something. Still looking :)

Thanks very much,
John Z
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on February 04, 2024, 11:09:35 PM
Hey John Z, the Windows stock cursors have black cursors available in system.

You might just need to align with a custom cursor file:

Code: [Select]
// Load a custom black arrow cursor from resources
HCURSOR hCursor = LoadCursor(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_BLACK_ARROW));
SetCursor(hCursor);

I havent come across cursor work yet, but I do switch them back and forth when cursor is over specific REC coordinates (custom close button, etc). I'll post what I find as well.
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on February 05, 2024, 01:51:31 AM
Hi WiiLF23,

Well yes there are white and dark cursors, but I did not find a white IBEAM....so I just made my own, res1.cur below. It works very well and tied to the editable areas whereas the non-editable areas use the standard arrow cursor.

I misspoke in the prior post when I called out a 'white cursor bar' I was referring to a white caret bar needed as in the picture below, which shows a dark caret. :(

John Z
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on February 05, 2024, 04:53:22 AM
Hey John Z, that IBeam cursor looks great (and much easier on the eyes in contrast). Did you sort out the scroll bar color? I'm curious about that, as that is probably the one thing I have not tackled yet for my UI. Dark mode is on hold for the moment, as I work the other controls (basically finished as of today). I cant wait to dive back into it  ;D
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on February 05, 2024, 10:27:13 PM
Hi WiiLF23,

Well sad to say that another feature removed by Micro$oft in WIN10 and onward was the ability to set the scroll bar color!  It was global anyway so it would need to be set back when program looses focus, however SetSysColors for
Code: [Select]
COLOR_SCROLLBAR 0

Scroll bar gray area.

Windows 10 or greater: This value is not supported.

There are web hints that it can be changed through the registry but that seems excessive for an application.  For my program the bars are gray, as most are, so not too visually disruptive in Dark Mode.

I can envision a totally custom drawn scrollbar using bitmap picture controls and line drawings and buttons but every element is reinvented and discrete, possible, but worth the effort?  Maybe it is out there somewhere...

John Z 
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on February 05, 2024, 10:46:28 PM
Hey John Z. It would appear to me that MS may of removed the API exposure for modifying the scrolling bar in order to divert the choices toward the unofficial dark mode visual theme string "Darkmode_Explorer".

I bet you can do this in .NET 7 on Windows 10 easily. I'll never touch Windows 11, or their VS stack again.

That is good info too, that would take a bit of time to discover - thank you for that bit.
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on February 05, 2024, 10:58:06 PM
Hi WiiLF23,

I located a possible solution:
"Replace a Window's Internal Scrollbar with a customdraw scrollbar Control"
over on CodeProject :
https://www.codeproject.com/Articles/14724/Replace-a-Window-s-Internal-Scrollbar-with-a-custo

Sounds reasonable -

I've not tried it yet.

John Z
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on February 09, 2024, 04:37:34 AM
Hi John Z,
That's interesting! I wonder how many editions that is supported on (notably Windows 10). I've never seen something like that before - very cool!

Title: Re: Enable Dark Mode for Title bar
Post by: John Z on February 09, 2024, 08:34:50 PM
Hi WiiLF23,

Yup, out-of-the-box thinking, sometimes neat results occur, sometimes disaster  ;)

Custom caret for 'manual' dark mode is working well I'll post a bit more about it later.
Nothing earthshaking relatively straightforward, certainly no OOB thinking.....


John Z
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on February 09, 2024, 10:27:39 PM
Custom Caret for Dark Mode (sorry a little long...)

Turns out it is not too difficult.  There are a few point to clear up however.

The color of the caret is not going to be the color of the bitmap replacement.
It is some combination of the background and bitmap colors.  Somewhere I read only
white bits are inverted. For example with a certain gray background a brown bitmap
gives a green caret change background the caret is red with same bitmap...See attachments

The bitmap caret is not automatically scaled with the font height. To look best
this would require building the caret bitmap in code on the fly after a font change
rather then using a predefined bit map.  Still considering if worth it...

I'm using accelerator keys in the program so my main windows is created with
hwndMain= CreateDialog(hInstance, MAKEINTRESOURCE(DLG_MAIN),....
so that I can have TranslateAccelerator in the actual msg loop

IN/On the main window are edit boxes.  for example
  CONTROL "Middle", Text2, "Edit", ES_AUTOHSCROLL|WS_TABSTOP, 188, 72, 104, 12, WS_EX_STATICEDGE, 202

These controls are the ones needing the custom caret when in DarkMode

In main MainDlgProc I check the msg for what has current focus if one of the edit controls has it and it was not also the last control to have focus then I set the caret to the control.  SO far so good everything appears to work as expected.  If not in Dark Mode skip unless 1st transition out of DarkMode then set caret back to system original.

My concern is that - 
MS says "The system provides one caret per queue. A window should create a caret only when it has the keyboard focus or is active. The window should destroy the caret before losing the keyboard focus or becoming inactive."
A new caret will always destroy the old one

If further says "the windows will be sent WM_KILLFOCUS before the actual focus is lost so that DestroyCaret(); can be used.  The edit box child windows never send this message when they lose focus.  It does appear that they automatically destroy my caret when leaving because the next time I go there I still need to set the caret to the new one even though I did it before.

I did add DestroyCaret to the CLOSE message routine for the main window just in case.
Did a lot of testing jumping to different programs and within my own everything appears to be ok but I'm not sure I'm not missing something....

Code: [Select]
/-----------------------------------------------------------------------
// Function : Set_one_Caret
// Task : Set the dark mode edit caret cursor to usable color
//              : and set it back to the std caret as required
// Note     :
// Input : context on state TRUE = Dark Mode caret, FALSE = Std
//-----------------------------------------------------------------------
void Set_one_Caret( BOOL context_on, HANDLE hwnd)
{ int ans;
  static HANDLE last_hwnd=NULL;
  static BOOL last_context=FALSE;
  DWORD err;
  RECT cwin;
  int h=0;

  if ((last_hwnd == hwnd) && (last_context == context_on))
{ return;}

    // NOT really needed just for testing
GetClientRect(text[0].hctl,&cwin);// sizes std caret to control window
    h = cwin.bottom-cwin.top-2;       // only useful for built in caret

    HBITMAP hCaret1=NULL;// caret bitmap handles
if (context_on)
  { hCaret1 = LoadBitmap(ghInstance,MAKEINTRESOURCE(CARET_W));
if (hCaret1 == NULL)
           MessageBoxA(NULL,"Failed load bitmap","Set_One_Caret",MB_OK);
     // no crash with NULL though just uses system caret - so ok fall through
        ans = CreateCaret(hwnd,hCaret1,0,h);   // new cursor, h ignored
err = GetLastError();
if (ans == 0)
{MessageBoxA(NULL,"Failed Create New","Set_one_Caret",MB_OK);
             Show_Error(err, L"Set_one_Caret- creating"); //error display
    }
ans = ShowCaret(hwnd);
err = GetLastError();
if (ans == 0)
  { MessageBoxA(NULL,"Failed Show New","Set_one_Caret",MB_OK);
            Show_Error(err, L"Set_one_Caret- show"); //error display
  }
      }

   else //revert to system caret
{ // test use edit box height (h) to the set system caret height - Works
ans = CreateCaret(hwnd,(HBITMAP) 1,2,h);   // std Caret
        err = GetLastError();
if (ans == FALSE)
   {MessageBoxA(NULL,"Failed Re-Create Old","Set_one_Caret",MB_OK);
            Show_Error(err, L"Set_one_Caret- Reset,Create"); //error display
   }
ans = ShowCaret(hwnd);
        err = GetLastError();
if (ans == FALSE)
  { MessageBoxA(NULL,"Failed Re-Show Old","Set_one_Caret",MB_OK);
            Show_Error(err, L"Set_one_Caret- Reset,Apply"); //error display
  }
}

last_hwnd = hwnd;
last_context = context_on;

return;

}/* end set_one_caret */





John Z
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on February 29, 2024, 09:00:19 PM
Looking good John Z.

I've been off the map for a bit (literally), and I am very thankful I produced the app to accommodate mass interruption. This leaves me with 1 foot in the door and the other pending for an undetermined timeframe.

I guess this is time to dig even deeper in C programming, and provide what I learn and prove possible.

I know it is not the end. I just have massive hopes and dreams, and it is hard for one person to pull it off on this scale.

Wish me luck!
Title: Re: Enable Dark Mode for Title bar
Post by: Ushavilash on April 10, 2024, 10:36:20 AM
Thanks for sharing that information and resources that is really helpful.
Title: Re: Enable Dark Mode for Title bar
Post by: WiiLF23 on April 11, 2024, 11:28:20 PM
You are most welcome! I will revisit this in coming weeks for additions and improvements.

TreeView painting procedure should be implemented into dark mode as well, as many people including myself rely on TreeView for data.

I completed the painting 2 months back, but I didn't post about it. I'll add the addition of this code, as well as a custom splash screen making full use of control designing.

(https://i.ibb.co/H2r8jKH/Tree-View-Paint.jpg)
Title: Re: Enable Dark Mode for Title bar
Post by: John Z on April 12, 2024, 12:22:05 PM
TreeView color fallback is also easy to do if one wants to supplement or avoid using window theme methods, which my Dark Mode mostly does.

Here is a code snippet:
Code: [Select]
case WM_CTLCOLORLISTBOX:
case WM_CTLCOLOREDIT:

//BookMark [{treeview color}]
        if ((HWND)lParam == GetDlgItem(gHWND, TreeView1))
  { if ((StartUp.ThemeAll == TRUE))// && (StartUp.ThemeNum >0))
      {// could color here too
TreeView_SetBkColor(GetDlgItem(gHWND, TreeView1),gColor);
                TreeView_SetTextColor(GetDlgItem(gHWND, TreeView1),lColor);
                return TRUE;
      }
    else
      {// back to 'normal'
TreeView_SetBkColor(GetDlgItem(gHWND, TreeView1),RGB(255,255,255));
                TreeView_SetTextColor(GetDlgItem(gHWND, TreeView1),0);
                return TRUE;
      }
           }

The advantage is that one can color it anyway desired.  Both background and font can be colored.

John Z