Custom Forms With .Net Controls
Hello all. This is my first post in C# Corner. I hope you could help me with my problem.
My problem is actually a two-part problem.
First: How do I make a custom form? I've read several tutorials on line that dealt with creating a GraphicPath object. Another replacing the form's Region property with a new one. But I want a better solution. I want something that is both efficient and elegant. In a tutorial (can't remember the link, but I'll search for it), the author actually suggested putting a background image for the form and setting a transparency key via an ImageAttributes object and drawing it via Graphics.DrawImage() method. It's a nice way, but is it efficient? I mean, going by this method, I'd need an image for every little thing that the program would do... Including controls! So, my first question summarized is: What's an efficient way of making custom forms? Preferably with an image.
Second: How do I put .Net controls on the newly created custom form? I want to define my buttons, but I want to able to use the default controls the .Net library has! So, any help is welcomed!
Sam HobbsPosted Mar 8, 2010, 12:01 AM
As I said, it helps to use the terminology that others are using, instead of hoping that others use your terminology.
In articles such as New Graphical Interface: Enhance Your Programs with New Windows XP Shell Features, Microsoft uses the terminology "non-rectangular and irregularly shaped". If you search for that, you are more likely to get help, such as Shaped Windows Forms and Controls in Visual Studio .NET and Sprinkle Some Pizzazz on Your Plain Vanilla Windows Forms Apps.
EhabPosted Mar 8, 2010, 12:16 AM
Thanks for the links you have provided... It seems that, indeed, I still have a lot to learn about the subject. I'm going to read the links and hopefully they'll shed light into the matter.
Thanks :)
Sam HobbsPosted Mar 8, 2010, 12:07 AM
I won't try to explain everything of course, but the non-client area is the background and the ....
Wait, let me back up a bit. The client area is the part where the controls and graphics and such go, including menus, toolbars and status bars.
Okay, so the non-client area is the other parts, such as the border and the caption (title). I don't know what else to say without a long description that is better done in many other resources. There is a lot to learn.
EhabPosted Mar 8, 2010, 12:01 AM
Thanks for your reply. You seem to have some knowledge about C++ (or at least Windows APIs). If you could explain it to me on that level, I think I could manage. I don't understand what you mean by painting the background of the non-client painting... Could you elaborate more on that point? I know there are different events that I could subscribe to (like OnLoad, OnPaint)... Also, there were these events in Web Applications but I'm not sure if they apply to Windows applications, such as, PreInit (which happenes before the initialization of controls).
Thanks for your help so far :)
Sam HobbsPosted Mar 7, 2010, 11:44 PM
Note that that article is written by someone with minimal experience. What you are trying to do likely needs advanced knowledge and experience, or at least significantly more than beginner-level. Graphics is not my specialty and what you are trying to do requires graphics.
I don't know how much support there is in .Net for that type of thing. I think the first thing to do is to determine what is the terminology that Microsoft uses for it, then research what is documented and such.
I am sorry that I did not study that article, but my guess is that the sample is quite deficient in the sense that the non-client messages and such must be used. When a Windows window is painted, there is a WM_NCPAINT message sent before the WM_PAINT mesage. The WM_NCPAINT is for the background, and then the forground is painted on top of that in the WM_PAINT message. As a .Net programmer, you don't need to write the code that processes the WM_NCPAINT and WM_PAINT messages, but the messages happen even if you know nothing about them. So to do what you want to do, I think it is necessary to paint the background in the non-client painting, but I don't know how to do that using .Net. If .Net does not support that, then the person that wrote that article is misleading others into believing that it can be done without the Windows API.
It might be possible to do it all in .Net, but it helps to understand how Windows works at the API level. You need to at least find an article written by someone that has experience with the Windows API. Articles written by someone that avoids the Windows API like cats that avoid water will not help you get across a river without a bridge, or I mean help you get an irregularly shaped window without the tools used most commonly.
EhabPosted Mar 7, 2010, 8:52 PM
Anyway, here's the link to the tutorial I'm talking about:
Custom shaped form with a drop down in C#
I'm sorry if it required psychic powers to understand my second point, I'll simplify it a bit more. If you check the link I posted (please do) then you'll find that the author removes the default form borders (title bar, minimize, maximize, and close buttons) and paints over the form with a Image object containing the desired form shape. The author subscribe to the form's OnPaint event and draws the image while setting a transparent key for the image -- effectively giving the form the image's shape. Now, I don't know how one would go about making a non-rectangular form shape other than this way. If you know, please do share, you'd be answering my first question :)
As for the second question, following the tutorial above, I can't seem to place controls at runtime correctly. A short note on this after the first issue is covered will be greatly appreciated :)
Thanks, kind sir.
Sam HobbsPosted Mar 7, 2010, 1:53 PM
You seem to think that "custom form" means something. That term is not common. All forms are custom forms. If there is something different about what you are trying to do then you need to describe it instead of saying custom form. I also don't understand "the form is being drawn using the Graphics.DrawImage in the OnPaint event". That is not normal. Either you are trying to do something quite advanced, or you are assuming we are psychic.
If I did not answer your second point, then you need to be more clear. I can understand that you might not yet be familiar with the correct terminology, so I suggest you learn a bit more about forms. I hope you are interested in learning.
EhabPosted Mar 7, 2010, 11:08 AM
Sir, I'm going to assume you meant that I create a custom form (which is what I'm basically asking for) and then drag and drop controls on the form (which doesn't even answer my second point). I have used a tutorial for a custom form and tried dragging and dropping controls on it, it works but not exactly. If I try to add controls programmatically, the form blocks the control (since in the tutorial, the form is being drawn using the Graphics.DrawImage in the OnPaint event of the form). I really need help here, because this is a simple task that I don't know how to accomplish correctly. I can't begin to hope to even call myself a programmer (even though I'm MCTS: Web certified).
Thanks.
Sam HobbsPosted Mar 5, 2010, 11:25 AM