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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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