convert system.color to integer ? (C#)

dynamic_sysop

Senior Contributor
Joined
Oct 1, 2002
Messages
1,039
Location
Ashby, Leicestershire.
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 :
Visual Basic:
            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:-\
 
Not sure if this is the best way to do this but the string representation of ToArgb() is what you want.
ToArgb().ToString()
 
We posted at the same time. Here is one of my methods as an example:
C#:
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.
 
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.
 
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]);
 
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#
Visual Basic:
		[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.
 
Use the ColorTranslator.ToOle() function.

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

shows

65535
 
cheers volt :) that inspired me
Visual Basic:
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.
 
Back
Top