Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can Bob Powell or anybody else help me

1,015 views
Skip to first unread message

Saurabh

unread,
Jan 29, 2004, 11:26:57 AM1/29/04
to
Hi All,

I had posted my message on the windows forms newsgroup and got a reply to
ask you guys about the query.

TIA,

--Saurabh

Try posting in the microsoft.public.dotnet.framework.drawing newsgroup.
There are several people there who can help you with this. Specifically Bob
Powell.

Cheers,
Paul

"Saurabh" <sau...@nagpurcity.net> wrote in message
news:eBmySdn5...@TK2MSFTNGP10.phx.gbl...
> Hi All,
>
> I have added my own button in the titlebar of a form next to the close
> button. Everything works fine but when I resize the dialog, the button
does
> not get redrawn at a correct location.
>
> I calculate the button position relative to the close button and draw a
> button using ControlPaint, but if i increase the size of the form briskly,
> the distance between the close button and my button increases till the
next
> redraw. If i reduce the width of the form by dragging the border inwards,
my
> button gets drawn overlapping on the close button. I want my button to be
at
> a fixed position relative to the close button. Is there any way to achieve
> this?
>
> TIA,
>
> --Saurabh
>
>


Bob Powell [MVP]

unread,
Jan 29, 2004, 12:45:30 PM1/29/04
to
The big question is I suppose... How are you doing that calculation.

If the button moves I suppose that the result of that is flawed somehow.

Post some code.

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Saurabh" <sau...@nagpurcity.net> wrote in message

news:%238g2xTo...@TK2MSFTNGP11.phx.gbl...

David P

unread,
Jan 29, 2004, 10:00:02 PM1/29/04
to
My guess would be that you get the message/event that the window size has
changed before it ACTUALLY changes, so you're drawing based on old
information. But that's just a guess and I don't know the solution.

Saurabh

unread,
Jan 30, 2004, 6:00:50 AM1/30/04
to
I have overriden the WndProc and in the WM_NCPAINT handler, which I guess
paints the close button alongwith all the other non-client area, I spaced
mybutton at a specific distance from the close button using the following
code :
---------code---------------
int CaptionHeight = Bounds.Height - ClientRectangle.Height;

Size CloseButtonSize = SystemInformation.CaptionButtonSize;

int X = Bounds.Width - 4 - CloseButtonSize.Width * 2;

int Y = 6;

MyButton.Bounds = new Rectangle(X, Y, 15, 15);

---------end of code----------

Please ignore the constant values as they came there by actually seeing the
adjustment on screen. Anyways, since I calculate the position for my button
relative to the close button, I expect it to remain at a same distance to
the close button. I'll recreate it in a simple project and post it soon.

Thanks

--Saurabh

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:eApcF$o5DH...@TK2MSFTNGP10.phx.gbl...

Bob Powell [MVP]

unread,
Jan 30, 2004, 6:15:23 AM1/30/04
to
This code doesn't explain how you're gettting the bounds which is the basis
of your positioning or which DC you use.

I saw this code on the other group. Please post something a bit more
comprehensive like the whole WM_NCPAINT handler.

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Saurabh" <sau...@nagpurcity.net> wrote in message

news:e3bcNCy5...@TK2MSFTNGP10.phx.gbl...

Saurabh

unread,
Jan 30, 2004, 6:29:42 AM1/30/04
to
Here it is.... This code should directly compile. Run this and on the
displayed form, grab the right edge and increase the size, just one
condition, do it briskly and you should be able to see the behaviour.

--Saurabh

-----------code-----------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace ShortButComplete
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

[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);

//Message constants
public const int WM_NCPAINT = 0x0085;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

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, 15, ButtonState.Normal);
g.Dispose();
ReleaseDC(hWnd, hDC);
}


#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(322, 295);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}

--------end of code-------------


"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message

news:uzAV3Jy5...@TK2MSFTNGP11.phx.gbl...

Bob Powell [MVP]

unread,
Jan 30, 2004, 7:46:59 AM1/30/04
to
All you're seeing is the lag in drawing the button. The button doesn't stay
in the wrong place. It just does is while the window is being dragged and
then goes back to where you wanted it.

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Saurabh" <sau...@nagpurcity.net> wrote in message

news:%23y%23qVSy5...@TK2MSFTNGP09.phx.gbl...

Saurabh

unread,
Jan 30, 2004, 12:00:35 PM1/30/04
to
Does it mean that the logic that i've used to calculate the button position
is correct? At least thats a relief.

What I am saying is, If I briskly drag the right hand resize border
outwards, the distance between the close button and my button increses TILL
WM_NCPAINT is sent again because of some reason. Its not a lag. If I drag
the border and leave it as it is, I can actually see them far apart
similarly is i reduce the size i can see them overlap.

--Saurabh

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message

news:%23Rj8B9y...@TK2MSFTNGP09.phx.gbl...

Bob Powell [MVP]

unread,
Jan 30, 2004, 4:28:16 PM1/30/04
to
Maybe I'm not seeing the same effect as you are. When I drag the corners of
the window around briskly the button, which seems generally to be positioned
correctly, looks as if it's being drawn in the wrong place momentarily. I
have not seen the button wander away from its designated place after I
stopped dragging the window around. It seems to me that you calculated the
position of the button just fine.

Are you saying that when you drag the window of button drifts off-station
and remains there after you've released the mouse?

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Saurabh" <sau...@nagpurcity.net> wrote in message

news:uKoSPL15...@TK2MSFTNGP09.phx.gbl...

David P

unread,
Jan 30, 2004, 11:08:33 PM1/30/04
to
My opinion remains the same. I suggest the following experiment:

When you recieve WM_NCPAINT, post (PostMessage) a WM_USER to your window.
Then add a case WM_USER: to your switch, and when you recieve the message,
draw the button:

> > > case WM_USER:


> > > {
> > > 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;

I'd try it myself but I'm busy...

"Saurabh" <sau...@nagpurcity.net> wrote in message

news:uKoSPL15...@TK2MSFTNGP09.phx.gbl...

Saurabh

unread,
Feb 9, 2004, 11:58:23 AM2/9/04
to
Thanks Bob and David.

Actually I was away for the week, I did not try anything during that time.
Thans a lot for your inputs!!

--Saurabh

"Saurabh" <sau...@nagpurcity.net> wrote in message

news:uKoSPL15...@TK2MSFTNGP09.phx.gbl...

Saurabh

unread,
Feb 10, 2004, 7:24:33 AM2/10/04
to
And this has struck me again at a different place. I use the ControlPaint
class to draw my button in the caption bar. If I try using it on XP, the
close button of XP appears as a white cross with a red background and my
button appears as a grey background, Of course controlpaint draws the
windows button rather than the caption button is there any way of telling it
that i want the button to be drawn with the caption button style rather than
the button style ??

--Saurabh

"Saurabh" <sau...@nagpurcity.net> wrote in message

news:%23tKKu4y...@TK2MSFTNGP12.phx.gbl...

Yevgeny

unread,
Feb 23, 2004, 11:46:33 AM2/23/04
to
Hi Saurabh!

I have to make the same task too (add additional button to title bar)
I read yours interesting discussion, I've got a lot of useful info.
Do you solve all the problems? Do you have any additional tips or info?

Thanks in advance.

Saurabh

unread,
Feb 25, 2004, 6:24:01 AM2/25/04
to
Sorry I haven't solved all the problems. It works but doesn't draw properly.
As of now, I have stopped wrestling with it because some other important
stuff has come up. I'll be looking into it again soon when I get time. I had
posted most of the code in the newsgroup anyways... If you have any specific
question, ask on the group!

Thanks,

--Saurabh

"Yevgeny" <1q...@mail.ru> wrote in message
news:8452a379.04022...@posting.google.com...

Bob Powell [MVP]

unread,
Feb 25, 2004, 7:46:01 AM2/25/04
to
Just to let you know I'm looking into this situation.

I've just started an article on non-client operations for Well Formed. I'll
send you some code when I get done.

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Saurabh" <sau...@nagpurcity.net> wrote in message

news:eYn1qG5%23DHA...@TK2MSFTNGP11.phx.gbl...

Saurabh

unread,
Feb 25, 2004, 9:15:25 AM2/25/04
to
Thanks bob. that would be great!

--Saurabh
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message

news:%23IToh15%23DHA...@TK2MSFTNGP09.phx.gbl...

Yevgeny

unread,
Feb 29, 2004, 1:31:36 AM2/29/04
to
Hi guys again.

I just want to say that I found excellent tutorial on:
http://www.catch22.org.uk/tuts/titlebar.asp. This tutorial was also
mentioned in another discussion.
This code wrote on the C and it took me few days to "translate" it to
C#, but it works and that's all what I need. :-)

Yevgeny


"Saurabh" <sau...@nagpurcity.net> wrote in message news:<eeVicm6#DHA....@TK2MSFTNGP10.phx.gbl>...

Yevgeny

unread,
Feb 29, 2004, 1:32:04 AM2/29/04
to
Hi guys again.

I just want to say that I found excellent tutorial on:
http://www.catch22.org.uk/tuts/titlebar.asp. This tutorial was also
mentioned in another discussion.
This code wrote on the C and it took me few days to "translate" it to
C#, but it works and that's all what I need. :-)

Yevgeny


"Saurabh" <sau...@nagpurcity.net> wrote in message news:<eeVicm6#DHA....@TK2MSFTNGP10.phx.gbl>...

0 new messages