Re: How to change the color of the title bar in c#?

  • Thread starter jack Charbonneau
  • Start date
J

jack Charbonneau

Hi, My system is the same as yours. I wrote the simplest app possible
to do this - just a single form with your code in the Form1_Activated
method. I tested on 2 different machines - and no luck. Could you look
at my code and see if I got something wrong? Thanks again!

Here's the code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Activated += new System.EventHandler(this.Form1_Activated);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

[DllImport("user32.dll")]
private static extern Int32 GetSysColor(Int32 Index);
[DllImport("user32.dll")]
private static extern bool SetSysColors(int cElements, Int32[]
lpaElements, Int32[] RgbValues);
private const int COLOR_ACTIVECAPTION = 2;
private const int COLOR_2NDACTIVECAPTION = 27;
private int oldSystemTitleBarColor;
private int[] element = new int[] {COLOR_ACTIVECAPTION};
private int[] element2 = new int[] {COLOR_2NDACTIVECAPTION};
private static int[] rgb = null;
private static int[] rgb_old = null;

private void Form1_Activated(object sender, System.EventArgs e)
{
if (rgb == null)
{
oldSystemTitleBarColor = GetSysColor(COLOR_ACTIVECAPTION);
oldSystemTitleBarColor = GetSysColor(COLOR_2NDACTIVECAPTION);
rgb = new int[]{Color.White.ToArgb() & 0x00ffffff};
}
bool b = SetSysColors(1, element, rgb);
b = SetSysColors(1, element2, rgb);
}
}
}
 
J

Jonny Yu

Hi,
It seems your code works fine on my machine, here is full version of my
code, you can compile and test it on your computer. I'll also send you an
compiled executable file to your e-mail box, if my code still can't work on
your machine, you can try that executable to see if it works properly. If my
executable works, please send your whole test project(source as well as the
executable) to me([email protected]).

here is my code:

using System;
using System.Drawing;
using System.Drawing .Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AccessibleDescription = "";
this.AccessibleName = "";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(520, 385);
this.Name = "Form1";
this.Text = "Form1";
this.Activated += new System.EventHandler(this.Form1_Activated);
this.Deactivate += new System.EventHandler(this.Form1_Deactivate);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

[DllImport("user32.dll")]
private static extern Int32 GetSysColor(Int32 Index);
[DllImport("user32.dll")]
private static extern bool SetSysColors(int cElements,Int32[]
lpaElements,Int32[] lpaRgbValues);

private const int COLOR_ACTIVECAPTION = 2;
private const int COLOR_2NDACTIVECAPTION = 27;

private static int[] element = new
int[]{COLOR_ACTIVECAPTION,COLOR_2NDACTIVECAPTION};
private int[] colors = null;
private int[] old_colors = null ;

private void Form1_Activated(object sender, System.EventArgs e)
{
if(colors == null)
{
old_colors = new
int[]{GetSysColor(COLOR_ACTIVECAPTION),GetSysColor(COLOR_2NDACTIVECAPTION)};
colors = new int[]{Color.White.ToArgb() & 0x00ffffff,Color.Gray.ToArgb()
& 0x00ffffff};
}
bool b = SetSysColors(2,element,colors);
if (!b)
MessageBox.Show("Form1_Activated : SetSysColors failed");

}

private void Form1_Deactivate(object sender, System.EventArgs e)
{
if (old_colors == null)
old_colors = new
int[]{GetSysColor(COLOR_ACTIVECAPTION),GetSysColor(COLOR_2NDACTIVECAPTION)};
bool b = SetSysColors(2,element,old_colors);
if (!b)
MessageBox.Show("Form1_Deactivate : SetSysColors failed");
}
}
}

Regards,
Ying Shen Yu
Microsoft Partner Online Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top