Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted

convert system.color to integer ?

 

hi does anyone know if it's possible to convert a colour to integer? i can put some code together and run my app , but when i click a button to test it, it errors.

i have managed to get it to show a value ( rgb i think ) like this :

           int c=Convert.ToInt32(Color.AliceBlue.ToArgb());
		MessageBox.Show(c.ToString());

but i want to be able to get the int value of the actuall colour not the rgb:-\

Posted

We posted at the same time. Here is one of my methods as an example:

public static int[] GetArgbValues(Color c)
{
int[]Argb= {(int)c.R, (int)c.G, (int)c.B};
return Argb;
}

 

As you can see, the Color.R ext. are bytes and can be implicitly converted to int. You don't need an explicit conversion.

C#
  • Leaders
Posted

i seem to get an error with that, cannot convert int[] to int. so i tried messing with it a bit, but Argb seems to be returning something funny lol

System.Int32[] thats what Argb is showing as.

Posted

It does not return a Color.FromArgb. It returns an integer array with 3 elements. Three integer values that you can plug into a call to Color.FromArgb()

Ex:

 

Color g= Color.FromArgb(c[0], c[1], c[2]);

C#
  • Leaders
Posted

ok if i elaberate a little with what i'm making maybe a suggestion on how to change the colours.

i have adapted my vb6 coding for changing progressbar colours ( front and back ) to C#

	[DllImport("User32.Dll")]
       public static extern int SendMessage(int hwnd,int wMsg,int wParam,int lParam);
       
	public const int PBM_SETBKCOLOR = 0x2001;
       public const int PBM_SETBARCOLOR = 0x409;

	private void button4_Click(object sender, System.EventArgs e)
	{
		int j=this.progressBar1.Handle.ToInt32();
		//get handle for progressbar
		SendMessage(j,PBM_SETBKCOLOR,0,10);
		//set backcolor of progressbar
		SendMessage(j,PBM_SETBARCOLOR,0,255);
		//set forecolor of progressbar
	}

in that case the 10 sets the backcolor to black , the 255 sets the forecolor to red.

i want to be able to allow a change of colour where the user can actually tell what colour they are setting rather than a number that means nothing to them.

  • *Experts*
Posted

Use the ColorTranslator.ToOle() function.

 

MessageBox.Show(ColorTranslator.ToOle(Color.FromArgb(255,255,0)).ToString());

 

shows

 

65535

  • Leaders
Posted

cheers volt :) that inspired me

using System.Runtime.InteropServices;
/// top of form ^^^
///
///
	[DllImport("User32.Dll")]
       public static extern int SendMessage(int hwnd,int wMsg,int wParam,int lParam);
       
	public const int PBM_SETBKCOLOR = 0x2001;
       public const int PBM_SETBARCOLOR = 0x409;
//// in design area ( below ) ^^^^
////
	public void SetProgressBackColor(Color c)
	{/// set the back color of the bar
       int a=Convert.ToInt32(c.R.ToString());
	int b=Convert.ToInt32(c.G.ToString());
	int d=Convert.ToInt32(c.B.ToString());
       int tot=Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a,b,d)).ToString());
	int j=this.progressBar1.Handle.ToInt32();
       SendMessage(j,PBM_SETBKCOLOR,0,tot);
	}

	public void SetProgressForeColor(Color c)
	{/// set the forecolor of the bar
		int a=Convert.ToInt32(c.R.ToString());
		int b=Convert.ToInt32(c.G.ToString());
		int d=Convert.ToInt32(c.B.ToString());
		int tot=Convert.ToInt32(ColorTranslator.ToOle(Color.FromArgb(a,b,d)).ToString());
		int j=this.progressBar1.Handle.ToInt32();
		SendMessage(j,PBM_SETBARCOLOR,0,tot);
	}
	private void button4_Click(object sender, System.EventArgs e)
	{
		SetProgressBackColor(System.Drawing.Color.BlueViolet);
           SetProgressForeColor(System.Drawing.Color.Red);
	}

:)

thanks again for the help.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...