Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
Additional button on the titlebar of the windows form
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.drawing.


Marcin
I develop with C#.
I want to add a button next to 'X' close button on windows form. I do not show minimize and maximize buttons.
I want to reset the form to its default position and size when it will be clicked and I would like to have custom symbol on this new button.

Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Marcin
Thanks to the code posted in another message, I got the not fully working example.
That is great. Here is the code.
In the windows c# application you have to declare 3 external functions that are used to draw on the window forms. Looks like old MFC...
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

then declare constant for event that redwaws the window form
//Message constants
public const int WM_NCPAINT = 0x0085;

Overwrite the Winproc function and PaintNC that paints the window on the screen... Old win32... I never thought that I could overwrite the win32 functions in c#.
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCPAINT:
{
base.WndProc (ref m);
IntPtr hDC = GetWindowDC(m.HWnd);
Graphics g = Graphics.FromHdc(hDC);
PaintNC(m.HWnd);
g.Dispose();
ReleaseDC(m.HWnd, hDC);
m.Result = IntPtr.Zero;
}
break;

default :
base.WndProc(ref m);
break;
}
}

protected void PaintNC(System.IntPtr hWnd)
{
IntPtr hDC = GetWindowDC(hWnd);
Graphics g = Graphics.FromHdc(hDC);
int CaptionHeight = Bounds.Height - ClientRectangle.Height; //Titlebar
Size CloseButtonSize = SystemInformation.CaptionButtonSize;
int X = Bounds.Width - 4 - CloseButtonSize.Width * 2;
int Y = 6;
ControlPaint.DrawButton(g, X, Y, 15, 14, ButtonState.Normal);
g.Dispose();
ReleaseDC(hWnd, hDC);
}

However this code is not 100% functional. The button is drawn to the one previous size of the form, it means that when you resize the window the button adjusts its position to the old size not the new one. So now I have to figure out how to update the size of the window form before the PaintNC function is called....

After that how can I subscribe to the button clicked event?
Any ideas...
Reply to this message...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
Saurabh
Of course its not 100% functional because I had posted it when I was trying
it out. Anyways, the obvious flaw was I was drawing it with the constant
height and width, moreover I am still worried about drawing it at a correct
location as it seems to miss the position for a fraction of a second till
you get the next repaint done. As far as I know, overriding the WndProc is
the only way to get hold of WM_NCPAINT so there is no other way to know when
to paint your button. Another flaw is, If you run it on XP, the close button
will appear as RED background with white cross but your button will appear
with GRAY background as that is drawn using the ControlPaint class which
does not have anything to set the caption button style for the button (or i
could not find one)

As far as subscribing to the click is concerned, I fire it myself as the
button drawn is not actually a button but just a drawing, so I trapped the
WM_NCLBUTTONUP / DOWN and check if its within the button bounds and fire the
events accordingly.

Hope this helps,

--Saurabh

"Marcin" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
> Thanks to the code posted in another message, I got the not fully working
example.
[Original message clipped]

functions in c#.
[Original message clipped]

button adjusts its position to the old size not the new one. So now I have
to figure out how to update the size of the window form before the PaintNC
function is called....
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Marcin
http://www.catch22.org.uk/tuts/titlebar.asp
I found this on the internet. It is in C, but it overwrites the events that you may use in your code.
I think that that link is very good, and I am working on implementing this solution into C# code that you developed.
I'll keep you posted…

Reply to this message...
Vote that this is a GOOD answer...
 
Email Archiving and Email Filing - what’s the difference?
Web-based task/todo list management
 
    
Saurabh
Excellent. Post the updates on the newsgroup rather than e-mail.

BTW, have you thought anything about the problem that I had mentioned about
not having XP style caption button.

--Saurabh

"Marcin" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Open source windows
The Law Society’s guidelines on e-mail management
 
    
Yevgeny
Hi guys.

I have to make the same task too (add additional button to title bar)

Do you solve all the problems?
Do you have any additional tips or info?

Thanks in advance.
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Marcin
Still working on it in my free time...
Reply to this message...
Vote that this is a GOOD answer...
 
Google Docs… no.
Twitter Elite
 
 
System.Drawing.Graphics
System.IntPtr
System.Windows.Forms.ButtonState
System.Windows.Forms.ControlPaint
System.Windows.Forms.SystemInformation




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734