Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to make the MDI parent form non-rectangular so i have set the background as a bitmap image which has lots of red color in it. Then i set the transparency key to red. But still the red areas are not showing as transparent. I have even set the display property to 16 bits (I have a windows XP, sp-3).
Please help.
Posted

Well, I noticed that. hence, I've worked on it. and here is the result.

Set IsMdiContainer property of your parent form to value false. then , replace form_load event handler as following:

<br />
private void Form1_Load(object sender, EventArgs e)<br />
 {<br />
   this.TransparencyKey = Color.FromArgb(255, 220, 33, 55);<br />
   <br />
   MdiClient Client = new MdiClient();<br />
   <br />
   this.Controls.Add(Client);<br />
            <br />
   Form Child = new Form();<br />
   <br />
   Child.Size = new Size(100, 100);<br />
   Child.FormBorderStyle = FormBorderStyle.FixedDialog;<br />
   Child.StartPosition = FormStartPosition.CenterParent;<br />
   Child.MaximizeBox = false;<br />
   Child.MinimizeBox = false;<br />
   <br />
   Child.MdiParent = this;<br />
   this.pictureBox1.Controls.Add(Child);<br />
 <br />
   Child.Show();         <br />
 }<br />


this code, will do transparency, and will add the mdiclient and child form. everything works fine, just, as you can see, childe form can be moved over all the parent form area. hence, the last TODO is finding a way to prevent it from being moved over transparented areas.
I'll work on that too, as soon as possible.

hope this helps.

--------------------
Regards

H.Maadani
 
Share this answer
 
v2
Of course there is a way, my friend.

you can always get the back color of MdiClient by this little piece of code :

<br />
MessageBox.Show(string.Format("R = {0}, G = {1}, B = {2}",<br />
 Client.BackColor.R, Client.BackColor.G, Client.BackColor.B));<br />


as you can see, it is (171,171,171).
So ;

<br />
this.TransparencyKey = Color.FromArgb(255, 171, 171, 171);<br />


will transparent the form itself.

P.S. 1 : rate the answers which help you, it makes life easier for people who come visit this question. and of course choosin an answer as solution wil help so. :)

P.S. 2 : I've published your question and the answers as an article for everyone to use. visit it at
Making a transparent MDI parent form[^]

------------------
Regards

H.Maadani
 
Share this answer
 
You can create non-rectangular windows using regions.
Here an article that demonstrates this -
Creating Bitmap Regions for Forms and Buttons[^]
 
Share this answer
 
v2
I think, the problem is you 'Red' in the picturebox is not that 'Red' which VisualStudio knows, you know? you'll have to set Transparency key exactly to the color you use.

I did it by setting Trnsparency Key to black, and using a black filled image loaded in a picturebox.

(donno how to add pics to my answer, otherwise would have put some pix.)

----------------------
Regards

H.Maadani
 
Share this answer
 
But i tried even that approach. I set proper colors and named them properly. It is still not transparent. Have you tried doing what you said to an MDI parent form? That is after setting the IsMDIContainer Property to true?
 
Share this answer
 
I agree with superman, however, we can do this by transparency too.

here is a little sample. I created a png file with photoshop, and filled areas I wanted with a red which I could see what is its ARGB equilant. so, in form load event, I've set the transparency key of form to that ARGB color.

http://rapidshare.com/files/340223944/TransparentSolution.rar[^]


--------------------
Regards

H.Maadani
 
Share this answer
 
Thank you very much Maadani, for taking the trouble to post your application online. That is appreciated.
But that wasn't what i was looking for. I guess i did not frame my question properly and thus nobody understood what i wanted.
I have already implemented what Maadani posted and what Superman told. But all these work only on "normal" forms.
Can you please tell me how to do set transparency key to a "parent" form?
For instance, Maadani, can you please set the "IsMDIContainer" property to true and then tell me if the form is working as desired?
 
Share this answer
 
Got it.

the fact is, when you set the 'IsMDIContaine' to true, a control of type 'MdiClient' is pre-programatically added to the form, with its Dock set to fill. so it hides the forms background, and overrides any painting action of form.

so, my solution is simple : Dispose it.

change form_load to something like this :

<br />
foreach (var c in Controls)<br />
{<br />
  if (c is MdiClient)<br />
  {<br />
     var client = (MdiClient)c;<br />
     client.Dispose();<br />
     this.TransparencyKey = Color.FromArgb(255, 220, 33, 55);<br />
     break;<br />
  }<br />
}<br />


and its done.

P.S. : I'm wondering what you'll do with such MDIParent form! ;)

---------------------
Regards

H.Maadani
 
Share this answer
 
v2
Thank you Maadani. Your solution does make my MDI form transparent. :)
But there is a small problem :( . Once we dispose of the client, then how can i expect the MDIform to open a child form? What i mean is, will the following code work
MIDL
chldForm.MdiParent = this;
           chldForm.Show();


after disposing of the client.
It shouldn't right? And thus this way of setting transparency defeats the entire purpose of creating an MDIParent form. Once you dispose of the client it cannot create any child forms because it is no more an MDIContainer.
This is the actual problem i've been facing and i want to work around it.

P.S. I want to make a transparent MDIParent form for an application where the form should take the outline of a soldier. It's really very hard to set a graphics path to follow the outline of a soldier so i'm trying to utilize the transparency key effect.
 
Share this answer
 
Thank you Maadani. Thank you very much. :) :) :) This is almost exactly what i was looking for :) :)
I just have one more small doubt. You have used the code
this.TransparencyKey = Color.FromArgb(255, 220, 33, 55);
to set the transparency key and this works like a charm! :) It makes transparent all elements within the form which have that particular color. But is there any way to make the form itself transparent? I'm asking this because the backcolor of an MDIParent form cannot be changed by setting the backcolor property , and the values which you have given, (255, 220, 33, 55) do not match with the default color of the MDIParent form in my system.
I would be really really grateful if you told me how to work around this.
 
Share this answer
 
Thank you very very much. :) :) :) This solves my problem completely. :) :) :)

P.S. I will rate the answers from now on.....i'm new to code project so...

P.P.S I'm glad you published a complete solution for this problem. It will be useful for many......and i'm sure this is the first ever complete solution to such a problem....at least on the net...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900