Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Implementing WinAPI function SetSysColors in C#

Posted on 2003-02-24
Medium Priority
Last Modified: 2007-12-19
I want to write some code that changes the desktop color, and the piece I'm stuck on is how to implement the API function in C#

Here is what msdn says:
BOOL SetSysColors(
  int cElements,
  const INT* lpaElements,
  const COLORREF* lpaRgbValues

I'm particularly confused by COLORREF, which is a "macro" in C++ and I'm not sure how you implement that in C#.  Also, lpaElements documentation refers to some constants, but I'm not sure how you make sure those work in C# without redefining them, and I don't know their integer values.  

The code should look something like this (once the API function is implemented)

// Window elements to change.
int aiElements[1] = {COLOR_DESKTOP};

// Array of RGB values.
COLORREF aColors[1] = RGB(0x80, 0x00, 0x80);  // dark purple

// Set the window elements in aiElements to the colors
// specified in aColors.
SetSysColors(1, aiElements, aColors);

Question by:kmhess
1 Comment

Accepted Solution

Nebulus_ earned 200 total points
ID: 8015495
Here an example, I hope this help you:

using System.Runtime.InteropServices;


const int COLOR_DESKTOP = 1;

uint RGB(byte byRed, byte byGreen, byte byBlue) {
  uint res = byBlue;
  res = res<<8;
  res = res<<8;
  return res;


public static extern bool SetSysColors(int cElements, int[] lpaElements, uint[] lpaRgbValues);


int[] aiElements = {COLOR_DESKTOP};
uint[] aColors = {RGB(0x00, 0x80, 0x00)};
SetSysColors(1, aiElements, aColors);

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question