Click here to Skip to main content
Click here to Skip to main content

Mac OS X Theme for Windows Forms

By , 26 Apr 2010
 
Download Mac_Theme_for_WinForms.zip - 2.56 MB

Introduction

Some programmers might want to apply some themes or skins to their forms so that it will look cool, sophisticated, and attractive.  I have created a Mac OS X open source theme library for everyone and the usage of it is indicated on this article. 

Background

I'm a die-hard Apple Mac OS X fan. Unfortunately I can't buy a Mac so I want to have the look and feel of Mac OS X Applications on my Windows Forms Application. I created a library so I can use it on any of my WinForms. This idea of mine doesn't modify the non-client area of a form, but creates a new pre-designed mac themed borderless form and makes your target form as its child control. 

How it was created  

The theme was purely made up of a borderless form with lots of double-buffer enabled panels, each specially positioned to serve as the title bar, control buttons,resizing grips, and container of the form you want to apply the theme.  

macthemestruct.png

This form includes several events such as resizing, moving, maximizing, minimizing, and closing. The target form (form where the theme is to be applied) was converted into a control and it will be owned by the Form Container (panel|bodypanel).

This mac themed form behaves normally the same way as a regular Windows border do. Here are some controls that make this custom theme similar to a regular Windows border.  

Titlebar 

-> This is where the form's text property is shown, via a centered label control. This part holds the caption and the control buttons (close, maximize/restore, and minimize buttons). You can move the window by dragging this part or maximize/restore the window by double-clicking.

Control Buttons 

-> Includes the close, maximize/restore, and minimize buttons. As a normal Windows border do, you can use these buttons to close the form, maximize, restore, and minimize the window.

Borders

-> These are the edges of the form where we can perform the resizing operations. 

Caption 

-> Displays the Form's Text property. 

Client Area

-> This is where the main graphical user interface of the application is found.

 

Controls that make up the Mac Theme

Here are the controls that are included in the mactheme Form class:

Control Name    Description   
mactheme This is the main borderless form that holds all the controls below. 
bodypanel This panel holds the form where you applied the theme. (form transformed into a low-level control and is owned by this control) 
bottompnl  The black 1px bottom border responsible for resizing. 
bottompnl2 bottompnl resizing grip extension. 
rightpnl The black 1px right border responsible for resizing. 
rightpnl2 rightpnl resizing grip extension. 
leftpnl  The black 1px left border responsible for resizing. 
leftpnl2 leftpnl resizing grip extension. 
toppnl  The black 1px top border responsible for resizing. 
titleCaption Shows the Text property of the form where your applied the theme. 
controlboxToolTip Enables the control buttons to show their descriptions through a tooltip. 
cmdClose Closes the form. 
cmdMaxRes  Maximizes/restores the form. 
cmdMin  Minimizes the form. 
swresize  Resizing grip for the bottom-right corner. 
nwresize Resizing grip for the bottom-left corner. 
panelmod1 (I forgot to rename this, sorry)The title bar. 
 

Each of these controls have event handlers for them to function accordingly. Resizing and moving functions use unmanaged codes (SendMessage and ReleaseCapture). 

 

The  image below shows a demo form. (Right=normal Windows theme|FormBorderStyle: Sizable; TopLevel:True;)  and (Left= Mac theme|FormBorderStyle:none;TopLevel:False;ParentForm:mactheme) 

macthemecomp.png

 

Using the code   

This library can generate three types of Mac themed form:

1. Thick Borders | Sizable (no top corners resize) 

2. Thick Borders; Resize disabled | Dialog Window 

3. Thin Borders | Sizable ( no corner resize) 

 Namespaces Included:   

using System; 
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices; 

Here are the following functions to apply the theme.  [Recommended to be written the Load event for the form] 

using xthemecore_macos; 

A] Select Form where you want to apply the theme. Create a Load event for the form. 

B] Create instance: 

ThemeManager mgr = new ThemeManager(); 

C] Apply Theme  [only one can be applied for each form] 

1. Thick Borders | Resizable  

mgr.ApplyFormThemeSizable(name_of_form);

2. Thick Borders; Resize disabled | Dialog Window

mgr.ApplyFormThemeSingleSizable(name_of_form);

3. Thin Borders | Resizable (You can put null on the parentForm parameter if the dialog doesn't have any owner.

Remember: 

-> When assigning the owner of a form WITHIN a form that has this mac theme, do not type this, instead type this.ParentForm because your form becomes child control once you apply the mac theme. 

mgr.ApplyFormThemeDialog(name_of_form, name_of_owner); 

Some Code Snippets Included 

You can transform a form into a low level control by using these codes...

Form parentForm = new Form(); 
this.TopLevel = false; 
this.FormBorderStyle = FormBorderStyle.None; 
parentForm.Controls.Add(this); 
parentForm.Show();

 This library uses some Windows API for the moving and resizing operation. 

internal const int WM_NCLBUTTONDOWN =161;
internal const int HT_CAPTION = 0x2;
internal const int HTBOTTOM = 15;
internal const int HTBOTTOMLEFT = 16;
internal const int HTBOTTOMRIGHT = 17;
internal const int HTRIGHT = 11;
internal const int HTLEFT = 10;
internal const int HTTOP = 12; 
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, long lParam, long wParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();  

 I have used panels that are double-buffered enabled to prevent flickers. 

 public class panelmod : Panel
        {
        public panelmod()
            {
            //sets the DoubleBuffered property of the panel to true.
            this.DoubleBuffered = true;
            }
        }  

Some Minor Problems 

a] You may find resizing difficult especially on the thin border theme.  

b] Form ownership must be clearly stated. Remember that the Mac themed form will be the owner of the form where you applied the theme.

------------------ 

If you discover some problems or bugs, just post a comment on this article or email me at john_november03@hotmail.com. Thank you. 

History 

This is the original version of this article. 

License

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

About the Author

John Espiritu
Software Developer (Junior) Team Console
Philippines Philippines
Coding just for fun and learning.
 
I started learning programming using MS Access' VBA codes, then VB6, VB.NET, C#, and Java. I am more accustomed in programming with the .NET framework.
 
I'm currently a B.S Computer Science student studying at STI College Bacoor, Philippines.. An institute that specializes in IT.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralExcellent Example just one thingmemberHayden2523-Sep-10 5:34 
Brilliant job, had to search along time for an example like this.
One thing I noticed was that there was quite a specific issue occuring. When you run the program, then deactivate it by choosing another window and activate it by selecting the title bar (no other control causes this), you are no longer able to move the cursor down to the windows taskbar.
I ended up just removing the "Cursor.Clip = Screen.PrimaryScreen.WorkingArea;" in the titlebar_MouseDown() and it still seemed to work fine.
Please let me know what implications that might have.
GeneralMDI child windowmemberTigerChan5-May-10 17:14 
Hi, very interesting work!
I've tried it and found out setting the form to be a MDI child ( set MdiParent ) caused error.
Is there any fix ?
GeneralRe: MDI child windowmemberVistaHacker9-May-10 17:18 
Hmm, I'll fix the codes for the MDI settings and update this article someday. Thank you for the bug report! Smile | :)
GeneralReally coolmemberDarell F. Butch Jr.4-May-10 2:50 
This is really cool, being a Mac fan myself. There are some issues when using dual monitors, but still really cool.
GeneralRe: Really coolmemberVistaHacker9-May-10 17:08 
Thanks! Big Grin | :-D
GeneralRe: Really coolmemberVistaHacker9-May-10 20:56 
hmm, can you please send me a screenshot on how the theme looks like when using dual monitors? I'll try to fix the problem. Thanks. Big Grin | :-D
GeneralRe: Really coolmemberDarell F. Butch Jr.10-May-10 1:49 
Sorry, should have been more specific; was intending for you to fix. It appears that the screen cordorancees get off. For example, if the for is moved to the second screen and then try dragging the screen around, then mouse pointer moves off to the left of the form. Dragging still works and there is no exception, just the screen position of the mouse point is not on the form header anymore.
 
No worries from my side, really liked the theme.
 
--D
GeneralRe: Really coolmemberVistaHacker11-May-10 1:59 
I see. Thanks! Smile | :)
GeneralMY VOTE 3memberjacky_zz3-May-10 22:22 
MY vote 3.
here is my mac form which paint use VC++.
http://www.cppblog.com/images/cppblog_com/jackyxinli/9457/o_kjdss130.gif[^]

modified on Tuesday, May 4, 2010 4:29 AM

GeneralRe: MY VOTE 3memberVistaHacker4-May-10 2:21 
The design of my theme is Mac OS X. Yours is the old version.
GeneralRe: MY VOTE 3memberjacky_zz4-May-10 16:41 
yes, it is. yours is not none-client paint, mine is.
GeneralRe: MY VOTE 3memberVistaHacker9-May-10 17:15 
I'm just demonstrating how to theme a winform without any modifications to the non-client area. Cool | :cool:
Well yours is cool too. Big Grin | :-D
GeneralNice trymemberAli BaderEddin29-Apr-10 14:42 
You get my vote of 4
-- Ali B
 
http://mycodelog.com

GeneralRe: Nice trymemberVistaHacker29-Apr-10 16:14 
Thanks! Big Grin | :-D
GeneralResizingmemberkasparovthe227-Apr-10 3:23 
If I want to resize the window manually (per this.Width or this.Height), nothing changes. Which control do I have to resize oder does the mactheme form block the resizing if sizable style is nonsizable ?
 
best regards
GeneralRe: Resizingmemberkasparovthe227-Apr-10 3:32 
Self reply Wink | ;)
 
I edited your code a bit for providing more features for handling resizing etc.
I'll send you the updated source this evening (evening in germany is about 12 PM in eastern america Wink | ;) )
 
best regards
GeneralRe: ResizingmemberVistaHacker27-Apr-10 3:39 
Thanks. Big Grin | :-D
GeneralNice work, howevermemberEpoque26-Apr-10 17:04 
I believe recently a skinning utility called SkinFramework was released. It effectively allows you to skin an application using the non-client region's paint events. Perhaps you should try looking it up, and manipulating the resource files included to skin your application with the Mac OS X design.
 
However, nice work nontheless Smile | :)
GeneralRe: Nice work, howevermemberVistaHacker26-Apr-10 17:30 
Thanks! Big Grin | :-D
I'll try your suggestion. Smile | :)
GeneralGood work!memberThe Manoj Kumar26-Apr-10 16:15 
Good job. My vote of 5 for you. It would look better with pure Mac OS icons Smile | :) .
GeneralRe: Good work!memberVistaHacker26-Apr-10 17:31 
Thanks. Smile | :)
I'll try to find some Mac OS toolbar icons.
GeneralNice 10/10memberMember 438892626-Apr-10 9:44 
Nice Wink | ;) 10/10
Whats the name of the icon set? Smile | :)
GeneralRe: Nice 10/10memberVistaHacker26-Apr-10 17:36 
Thanks! Smile | :)
Here's the link of my toolbar icons (PNG): http://p.yusukekamiyamane.com/[^]
GeneralNicemember622011926-Apr-10 6:04 
How about Glassy effect and Rounded corner ? It'd be great Big Grin | :-D
GeneralRe: Nicememberkasparovthe226-Apr-10 6:44 
Yeah, and what about coloring the minimize/maximize/close-buttons ?
best regards

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 26 Apr 2010
Article Copyright 2010 by John Espiritu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid