Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting title bar and border colors programmatically

Tags:

I am trying to change my application' s title bar and border colors programmatically. I tried lots of things but with no success, and decided to change these colors system-wide. Because it is also acceptable for me to change title bar and border colors as my application is running, and revert them back in the end of my application. (Managed environment, with small set of applications running)

Is it possible to change these colors dynamically(process-wide, or system-wide unless process-wide change is possible)? Can you suggest any way to achieve this?

I tried something like the following but it doesn' t do what I want:

int aElements[2] = {COLOR_WINDOW, COLOR_ACTIVECAPTION};
DWORD aOldColors[2];
DWORD aNewColors[2];

aOldColors[0] = GetSysColor(aElements[0]); 
aOldColors[1] = GetSysColor(aElements[1]); 
aNewColors[0] = RGB(0x80, 0x80, 0x80);  // light gray 
aNewColors[1] = RGB(0x80, 0x00, 0x80);  // dark purple 

SetSysColors(2, aElements, aNewColors);
SetSysColors(2, aElements, aOldColors);

Thanks in advance

EDIT

This is exactly what I want:

enter image description here

like image 499
Alpay Avatar asked Aug 31 '15 11:08

Alpay


2 Answers

I don't recommend to customize border and title redrawing. It's really hard to do it the right way. Office just draws everything by itself in the client area but using normal border. Using NC_PAINT the right way is a pain and may introduce flickering. Especially positioning the minimize,maximize and close buttons is difficult, because every windows does it differently. Also take into account accessibility, larger fonts used, customized user settings.

Whats the purpose of changing the colors?

To change the global colors you have to at least separate your code

// call this once at startup of your application (e.g. in WM_CREATE)

SetSysColors(2, aElements, aNewColors); 

// call this when closing you application (e.g. in WM_DESTROY)

SetSysColors(2, aElements, aOldColors); 
like image 76
lexx9999 Avatar answered Sep 27 '22 02:09

lexx9999


I know you are using C++, but I am handy with C#. So that you may get some idea, take look at the following code, which modifies form appearance.

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("User32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    const int WM_NCPAINT = 0x85;
    if (m.Msg == WM_NCPAINT)
    {
        IntPtr hdc = GetWindowDC(m.HWnd);
        if ((int)hdc != 0)
        {
            Graphics g = Graphics.FromHdc(hdc);
            g.FillRectangle(Brushes.Green, new Rectangle(0, 0, 4800, 23));
            g.Flush();
            ReleaseDC(m.HWnd, hdc);
        }
    }
}

Also, you could use the Drawing Custom Borders in Windows Forms project from CodePlex. This project is a tiny library that allows users to customize Windows Forms, like customizing a windows' non-client area.

like image 42
DOTNET Team - WeblineIndia Avatar answered Sep 26 '22 02:09

DOTNET Team - WeblineIndia