472,962 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,962 software developers and data experts.

C# application - system transparent control inside user drawn control...

This one's a hairy one to explain. What I've got is a class called "VistaGroupBox" that I put some nice-looking Vista-style gradients in the background and made to look like the ribbon categories you see in Office 2007. I got it working great, and created a few more controls, including a "VistaButton", which is primarily transparent when the cursor does not hover over it. However, when I added a standard Panel in the VistaGroupBox and put my button inside the Panel (I want the Panel to auto-scroll), the VistaButton's transparent background started as though it were in the upper-left-hand corner of the VistaGroupBox, which was not where the button actually was at all! The button draws correctly when outside the Panel.

So, what I've got is this:
Expand|Select|Wrap|Line Numbers
  1. VistaGroupBox
  2.  +-Panel
  3.     +-VistaButton
In order to draw my transparent controls, I've made a function that does this: (Some controls, like Button, don't have transparency support at all - I got this from the Mono implementation)
Expand|Select|Wrap|Line Numbers
  1.         public delegate void InvokePaintDelegate(Control c, PaintEventArgs e);
  2.         public delegate void InvokePaintBackgroundDelegate(Control c, PaintEventArgs e);
  3.         /// <summary>
  4.         /// This function looks like a mess primarily because of the #if ... #endifs.  The problem is, the "BROKEN WINDOWS"
  5.         /// section is how WINDOWS does it, which is a problem for going through multiple transparent areas for whatever reason.
  6.         /// MY_CORRECT works if you're only going from user drawn transparency to user drawn transparency - that is, none of your
  7.         /// controls use Control.OnPaintBackground (like a Transparent Panel).  WINDOWS_HACK works in all conditions - too bad it uses
  8.         /// a bit more memory due to the image creation.
  9.         /// </summary>
  10.         /// <param name="c">The control to paint the parent to</param>
  11.         /// <param name="pevent">The PaintEventArgs from the OnPaintBackground call</param>
  12.         /// <param name="InvokePaint">The control's InvokePaint method - because it is protected.</param>
  13.         /// <param name="InvokePaintBackground">The control's InvokePaintBackground method - because it is protected.</param>
  14.         public static void DrawParent(Control c, PaintEventArgs pevent, InvokePaintDelegate InvokePaint, InvokePaintBackgroundDelegate InvokePaintBackground)
  15.         {
  16.             PaintEventArgs parent_pe;
  17.             GraphicsState state;
  18.             Rectangle clipRect;
  20.             Graphics g;
  22.             IntPtr hdc = pevent.Graphics.GetHdc();
  23.             g = Graphics.FromHdc(hdc);
  24.             if (c.Region != null)
  25.                 g.Clip = c.Region;
  26.             else
  27.                 g.Clip = new Region(c.ClientRectangle);
  28.             clipRect = new Rectangle(pevent.ClipRectangle.X + c.Left, pevent.ClipRectangle.Y + c.Top, pevent.ClipRectangle.Width, pevent.ClipRectangle.Height);
  29. #elif MY_CORRECT
  30.             g = pevent.Graphics;
  31.             clipRect = new Rectangle(pevent.ClipRectangle.X + c.Left, pevent.ClipRectangle.Y + c.Top, pevent.ClipRectangle.Width, pevent.ClipRectangle.Height);
  32. #elif WINDOWS_HACK
  33.             Image img = new Bitmap(c.Parent.Width, c.Parent.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  34.             g = Graphics.FromImage(img);
  35.             clipRect = new Rectangle(c.Location, c.Size);
  36.             g.Clip = new Region(clipRect);
  37. #endif
  39.             parent_pe = new PaintEventArgs(g, clipRect);
  41.             state = g.Save();
  42. #if !WINDOWS_HACK
  43.             g.TranslateTransform(-c.Left, -c.Top);
  44. #endif
  45.             InvokePaintBackground(c.Parent, parent_pe);
  46.             g.Restore(state);
  48.             state = g.Save();
  49. #if !WINDOWS_HACK
  50.             g.TranslateTransform(-c.Left, -c.Top);
  51. #endif
  52.             InvokePaint(c.Parent, parent_pe);
  53.             g.Restore(state);
  56.             g.Dispose();
  57.             pevent.Graphics.ReleaseHdc(hdc);
  58. #elif WINDOWS_HACK
  59.             g.Dispose();
  60.             pevent.Graphics.DrawImage(img, new Rectangle(new Point(0,0), c.Size), new Rectangle(c.Location, c.Size), GraphicsUnit.Pixel);
  61.             img.Dispose();
  62. #endif
  63.         }
You can see that I've got 3 preprocessor directives here, each should be used exclusively. BROKEN_WINDOWS behaves exactly like the Windows default, WINDOWS_HACK works properly, except I'm creating images, and so it tends to be a bit slow.

Anyone got some time to help me figure this one out?
Sep 30 '07 #1
0 2819

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Steve Koon | last post by:
Any thoughts on getting this project to work or suggesting another method would be appreciated. Steve ========================================== Project: A Windows Form which acts as a...
by: Sushil Srivastava | last post by:
Hi Guys, Would you be able to help me using C# GUI (with user interface component) in my MFC application. I have used managed extension, COM-interops, etc but problem is this C# component has...
by: Mark Deibert | last post by:
I'm a former VB6 coder. Quit a few years ago. Now I'm back and trying to teach myself VB.NET. I don't remember having this much difficulty learning VB6. I'm totally stuck on something and need your...
by: Brian Henry | last post by:
Hello, I want to make a control that has a scrollable Gantt type display (not really one but will look similar to one) this is going to be custom drawn (i already have the drawing procedure...
by: Adam Maltby | last post by:
Hi, I need to do a transparent label but with a difference. I have drawn on my form a custom background using CreateGraphics as I needed an ellipse looking fill in a rectangle - so the actual...
by: Lucile | last post by:
Hi All, I am trying to obtain some sort of a "Model" based on horizontal & vertical lines (like a frame) drawn on a form or on a PictureBox. For frames configuration purposes, I created a white...
by: Adam Clauss | last post by:
I have a completely user-drawn user control in which I enabled transparency (follownig suggestions at...
by: NickP | last post by:
Hi there, I have a usercontrol that inherits from Windows.Forms.Button. In the OnCreateControl method I set the style of the control so that it supports transparent backcolor and set the...
by: Kevinst | last post by:
Hello there, I have got a problem and several approaches but none is really staisfying: Lets assume we have a form. On its surface we have got several animations (like a blinking light). This...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.