Transparent backgrounds for controls
-
Hello there.... I am new to c# (and c like languages in general) and I am trying make a user defined control - essentially just a graphic with a few methods for moving it around and collision detection etc (guess what I am trying to write!). So far I think I have got my head around overriding the onPaint method for my new control, and in this overridden method I construct a simple polygon out of an array of points. Now, I think I have conquered the collision detection (in a somewhat shoddy manner I am sure) but for can't work out how to make my control background transparent - the only part of the control I want displayed is the polygon, but instead I get the polygon and the control background (a grey rectangle or whatever colour I set it to). My current investigations are leading me in the direction of Control.setStyle but can't quite get it right. Can anybody please help me? :confused: Incidentally, I have tried setting the paintEventArgs.graphics.cliparea to be a region that matches the polygon, but I still get the graphics object background being displayed. Write soon, and save me a headache. Cheers, Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
-
Hello there.... I am new to c# (and c like languages in general) and I am trying make a user defined control - essentially just a graphic with a few methods for moving it around and collision detection etc (guess what I am trying to write!). So far I think I have got my head around overriding the onPaint method for my new control, and in this overridden method I construct a simple polygon out of an array of points. Now, I think I have conquered the collision detection (in a somewhat shoddy manner I am sure) but for can't work out how to make my control background transparent - the only part of the control I want displayed is the polygon, but instead I get the polygon and the control background (a grey rectangle or whatever colour I set it to). My current investigations are leading me in the direction of Control.setStyle but can't quite get it right. Can anybody please help me? :confused: Incidentally, I have tried setting the paintEventArgs.graphics.cliparea to be a region that matches the polygon, but I still get the graphics object background being displayed. Write soon, and save me a headache. Cheers, Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
I've just done a quick check and it seems user defined controls ie, anything derived from UserControl (or Control I think) cannot set it's BackColor property to transparent. Why not set the controls BackColor to be the same as the BackColor of the form containing the controls? Regards Senkwe Just another wannabe code junky
-
I've just done a quick check and it seems user defined controls ie, anything derived from UserControl (or Control I think) cannot set it's BackColor property to transparent. Why not set the controls BackColor to be the same as the BackColor of the form containing the controls? Regards Senkwe Just another wannabe code junky
Hi Senkwe, Thanks for taking the time to help me. I have tried your idea but nfortunately your suggestion does not solve the problem. My polygon represents an irregular shape - actually, an asteroid. The foreground is, say, red - yes, a red asteroid (and why not?). The background which c# paints it on is the same colour as the form (grey). It is possible to change the background colour to black, green etc etc. When you do this, you get a red asteroid in a black (or whatever) rectangle. When two asteroids overlap slightly the rectangle from one blocks out (is on top of) the other asteroid - this is my problem. All asteroid need to be drawn on a transparent rectangle so that their canvasses do not obscure each other. If you look at control.settype, it seems there may be a way to make the background colour transparent, in fact I have managed this, however, the asteroid is redrawn very slowly.... here is some of the code from the constructor of asteroid:control public asteroid(int startX, int startY, int incX, int incY, Form theForm) { this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.backgroundColor = Color.FromArgb(1,Color.Gray); ... I think I will tidy up my code and paste here for people to read. As an additional problem, when I am performing collision testing, I have a peculiar problem that seems to be inconsistent. When I create a rectangular region from my asteroid control, I can use region.isVisible(point) to test whether point is included in the region. This works. When I create an asteroid shaped region (instead of a rectangular shape), the call to region.isVisible(point) always seems to return false. Perhaps I am misunderstanding the region concept. Any ideas? Jase jason.king@profox.co.uk Feel the love at www.profox.co.uk
-
Hi Senkwe, Thanks for taking the time to help me. I have tried your idea but nfortunately your suggestion does not solve the problem. My polygon represents an irregular shape - actually, an asteroid. The foreground is, say, red - yes, a red asteroid (and why not?). The background which c# paints it on is the same colour as the form (grey). It is possible to change the background colour to black, green etc etc. When you do this, you get a red asteroid in a black (or whatever) rectangle. When two asteroids overlap slightly the rectangle from one blocks out (is on top of) the other asteroid - this is my problem. All asteroid need to be drawn on a transparent rectangle so that their canvasses do not obscure each other. If you look at control.settype, it seems there may be a way to make the background colour transparent, in fact I have managed this, however, the asteroid is redrawn very slowly.... here is some of the code from the constructor of asteroid:control public asteroid(int startX, int startY, int incX, int incY, Form theForm) { this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.backgroundColor = Color.FromArgb(1,Color.Gray); ... I think I will tidy up my code and paste here for people to read. As an additional problem, when I am performing collision testing, I have a peculiar problem that seems to be inconsistent. When I create a rectangular region from my asteroid control, I can use region.isVisible(point) to test whether point is included in the region. This works. When I create an asteroid shaped region (instead of a rectangular shape), the call to region.isVisible(point) always seems to return false. Perhaps I am misunderstanding the region concept. Any ideas? Jase jason.king@profox.co.uk Feel the love at www.profox.co.uk
One thing that might speed up your drawing code is to enable double buffering, then all of your graphics drawing is done in system memory; then copied to video memory once; by passing the system->video bottle neck. Hope that helps a little, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972
-
Hello there.... I am new to c# (and c like languages in general) and I am trying make a user defined control - essentially just a graphic with a few methods for moving it around and collision detection etc (guess what I am trying to write!). So far I think I have got my head around overriding the onPaint method for my new control, and in this overridden method I construct a simple polygon out of an array of points. Now, I think I have conquered the collision detection (in a somewhat shoddy manner I am sure) but for can't work out how to make my control background transparent - the only part of the control I want displayed is the polygon, but instead I get the polygon and the control background (a grey rectangle or whatever colour I set it to). My current investigations are leading me in the direction of Control.setStyle but can't quite get it right. Can anybody please help me? :confused: Incidentally, I have tried setting the paintEventArgs.graphics.cliparea to be a region that matches the polygon, but I still get the graphics object background being displayed. Write soon, and save me a headache. Cheers, Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
Thanks to the 2 people that helped me out. Neither solution really helped that much, but I did learn, so that's a good thing. In the end, to remove background rectangles, I made one big background rectangle and drew everything on that, in other words, I painted everything on one canvas (instead of one canvas per game character). This seems to have done the job nicely. If anyone wants to see the finished code, drop me a mail. :rose: Jase:rose: Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk