Leaders dynamic_sysop Posted June 8, 2003 Leaders Posted June 8, 2003 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:-\ Quote
Leaders dynamic_sysop Posted June 8, 2003 Author Leaders Posted June 8, 2003 s'ok i've sussed it:) int c=Convert.ToInt32(Color.Red.R.ToString()); MessageBox.Show(c.ToString()); // gets the r value , g for the g value etc... Quote
aewarnick Posted June 8, 2003 Posted June 8, 2003 Not sure if this is the best way to do this but the string representation of ToArgb() is what you want. ToArgb().ToString() Quote C#
aewarnick Posted June 8, 2003 Posted June 8, 2003 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. Quote C#
Leaders dynamic_sysop Posted June 8, 2003 Author Leaders Posted June 8, 2003 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. Quote
aewarnick Posted June 8, 2003 Posted June 8, 2003 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]); Quote C#
Leaders dynamic_sysop Posted June 8, 2003 Author Leaders Posted June 8, 2003 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. Quote
*Experts* Volte Posted June 8, 2003 *Experts* Posted June 8, 2003 Use the ColorTranslator.ToOle() function. MessageBox.Show(ColorTranslator.ToOle(Color.FromArgb(255,255,0)).ToString()); shows 65535 Quote
Leaders dynamic_sysop Posted June 8, 2003 Author Leaders Posted June 8, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.