Saurabh
Tue Dec 16 06:41:36 CST 2003
Hi Jon,
Here's the short but complete code which does show the same behaviour. Keep
clicking in the title bar area of the form and it throws the exception.
------------------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 const int WM_NCLBUTTONDOWN = 0x00A1;
public const int WM_NCLBUTTONUP = 0x00A2;
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)
{
base.WndProc (ref m);
switch (m.Msg)
{
case WM_NCPAINT:
{
IntPtr hDC = GetWindowDC(m.HWnd);
Graphics g = Graphics.FromHdc(hDC);
int CaptionHeight = Bounds.Height - ClientRectangle.Height;
Size CloseButtonSize = SystemInformation.CaptionButtonSize;
//position for buttons from left of close button
int X = Bounds.Width - 5 - CloseButtonSize.Width * 2;
int Y = 4;
ControlPaint.DrawButton(g, X, Y, 16, 16, ButtonState.Normal);
System.Drawing.Rectangle rect = new Rectangle(X + 2, Y + 2, 12, 12);
ControlPaint.DrawFocusRectangle(g, rect);
g.Dispose();
ReleaseDC(m.HWnd, hDC);
m.Result = IntPtr.Zero;
}
break;
case WM_NCLBUTTONUP:
{
Type t = Type.GetType("System.Object");
System.Type t1 = Type.GetType("System.Drawing.Point");
t1 = typeof(System.Drawing.Point);
String str = t1.AssemblyQualifiedName.ToString();
Type t2 = Type.GetType(str);
Object o = m.GetLParam(t2);
Point pt = (Point)o;
m.Result = IntPtr.Zero;
}
break;
default :
break;
}
}
#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.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
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-------------
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1a4904b495e68f10989c3d@msnews.microsoft.com...
> Saurabh <saurabh@nagpurcity.net> wrote:
> > Well you are right, I haven't posted enough code... but simlpy thats all
the
> > code which goes wrong.
>
> But chances are you've got other code which has set it up incorrectly.
>
> > Maybe I failed to explain the problem properly.
> > Anyways here's the other relevant bits of the code. I am also doubtful
about
> > the value of WM_NCLBUTTONUP 'coz my code seems to go there on double
click.
> >
> > This seems to work... sometimes. when I kept clicking in the NC area of
my
> > form, after some time it threw that exception. In debug mode, It seems
to
> > return the point sometimes and gives that error other times... Very
> > Unreliable. Can you suggest anything?
>
> Well, if you could post the full code, I could try to reproduce it...
> without the full code, I can't.
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too