473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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;
  19.  
  20.             Graphics g;
  21. #if BROKEN_WINDOWS
  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
  38.  
  39.             parent_pe = new PaintEventArgs(g, clipRect);
  40.  
  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);
  47.  
  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);
  54.  
  55. #if BROKEN_WINDOWS
  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.         }
  64.  
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 2838

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

Similar topics

3
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...
3
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...
5
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...
5
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...
4
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...
1
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...
4
by: Adam Clauss | last post by:
I have a completely user-drawn user control in which I enabled transparency (follownig suggestions at...
3
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.