473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Mouse click on the frame of a form (C#)

Hello,

There is what I want to do. I worked on the problem since 6 hours but
I had a problem (probably stupid) and I dont know how to resolve it.
I need to have an Mouse Event Click when I click on the frame of a
window form ( like the title bar of a window).
If I handle the mousee events with my window's forme instance, i only
can handle it on the ClientSize area.

After, I tryed to handle the messages with WndProc and DefWndProc. The
problem is that this is the same messages.
What I what, it's only be notified when I click on the title bar of my
my application's window. The problem is how to handle this with C#
becose in old Win32 this is not a problem.
Any hints?
Tahnk for you help!
Salutations,
Nov 15 '05 #1
8 12594
NeoAsimov,

Actually, the messages are not the same. For non-client areas, you have
to process the WM_NC* versions of the messages. So, for the left button
event in the title bar, you have to process the WM_NCLBUTTONUP message.
Handling this in your WndProc implementation is all you need. Of course,
rembmer to call the base class implementation of WndProc.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"NeoAsimov" <ne*******@hotmail.com> wrote in message
news:db**************************@posting.google.c om...
Hello,

There is what I want to do. I worked on the problem since 6 hours but
I had a problem (probably stupid) and I dont know how to resolve it.
I need to have an Mouse Event Click when I click on the frame of a
window form ( like the title bar of a window).
If I handle the mousee events with my window's forme instance, i only
can handle it on the ClientSize area.

After, I tryed to handle the messages with WndProc and DefWndProc. The
problem is that this is the same messages.
What I what, it's only be notified when I click on the title bar of my
my application's window. The problem is how to handle this with C#
becose in old Win32 this is not a problem.
Any hints?
Tahnk for you help!
Salutations,

Nov 15 '05 #2
100
Hi NeoAsimov,
The problem is in windows, I believe. I'm using Win2k and it doesn't send
WM_NCLBUTTONUP message to the window. Instead windows sends WM_NCMOUSEMOVE.
I believe this is a *bug* in windows. I don't know if this problem exists in
WinXP, though.

B\rgds
100

"NeoAsimov" <ne*******@hotmail.com> wrote in message
news:db**************************@posting.google.c om...
Hello,

There is what I want to do. I worked on the problem since 6 hours but
I had a problem (probably stupid) and I dont know how to resolve it.
I need to have an Mouse Event Click when I click on the frame of a
window form ( like the title bar of a window).
If I handle the mousee events with my window's forme instance, i only
can handle it on the ClientSize area.

After, I tryed to handle the messages with WndProc and DefWndProc. The
problem is that this is the same messages.
What I what, it's only be notified when I click on the title bar of my
my application's window. The problem is how to handle this with C#
becose in old Win32 this is not a problem.
Any hints?
Tahnk for you help!
Salutations,

Nov 15 '05 #3
100
Hi,
I just found that I made a mistake docs says that WM_NCLBUTTONUP won't be
send if the windows has set the mouse capture. releasing the capture makes
the messages come but mess up with dragging the window

B\rgds
100

"100" <10*@100.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Hi NeoAsimov,
The problem is in windows, I believe. I'm using Win2k and it doesn't send
WM_NCLBUTTONUP message to the window. Instead windows sends WM_NCMOUSEMOVE. I believe this is a *bug* in windows. I don't know if this problem exists in WinXP, though.

B\rgds
100

"NeoAsimov" <ne*******@hotmail.com> wrote in message
news:db**************************@posting.google.c om...
Hello,

There is what I want to do. I worked on the problem since 6 hours but
I had a problem (probably stupid) and I dont know how to resolve it.
I need to have an Mouse Event Click when I click on the frame of a
window form ( like the title bar of a window).
If I handle the mousee events with my window's forme instance, i only
can handle it on the ClientSize area.

After, I tryed to handle the messages with WndProc and DefWndProc. The
problem is that this is the same messages.
What I what, it's only be notified when I click on the title bar of my
my application's window. The problem is how to handle this with C#
becose in old Win32 this is not a problem.
Any hints?
Tahnk for you help!
Salutations,


Nov 15 '05 #4
Hello everybody,
Thank for your help, it's really appreciated. I'll check what can
I do with your informations :)

Thank alot,
Salutations,
Nov 15 '05 #5
100
Hi NeoAsimov,
I found probably one of the solutions of your problem.
This solution is not perfect and has flaws, but is good for a starter, I
believe.

private const int WM_NCMOUSEMOVE = 0xa0;
private const int WM_NCLBUTTONDOWN = 0xa1;
private const int WM_NCLBUTTONUP = 0xa2;
private const int HTCAPTION = 2;
private const int SC_MOVE = 0xF010;
private const int WM_SYSCOMMAND = 0x0112;

protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_NCLBUTTONDOWN:
if((int)m.WParam == HTCAPTION)
{
this.Capture = false;
m.Result = new IntPtr(0);
return;
}
break;

case WM_NCMOUSEMOVE:
if(MouseButtons == MouseButtons.Left)
{
m.Msg = WM_SYSCOMMAND;
m.WParam = new IntPtr(SC_MOVE + 2);
}
break;
case WM_NCLBUTTONUP:
if((int)m.WParam == HTCAPTION)
{
this.OnClick(EventArgs.Empty);
}
break;
}

base.WndProc (ref m);
}

I release the mouse capture when the left button is down over the caption.
this will make the OS to send mouse up message. However, releasing the
capture has the efect that the form cannot be moved with the mouse.
That's why we have to start the dragging procedure if necessary by hand.
This is done in responce to WM_NCMOUSEMOVE when the left button is down.
To start moving(dragging) a window with the mouse WM_SYSCOMMAND with SC_MOVE
should be pass to the DefWindowProc. the same command is send if the windows
is moved from the system meny with the keyboard. To distinguish between the
two methods windows uses the low for bits of the wParam. if the low four
bits are 0 - the system menu, 2 - the mouse. These numbers are not
documented and can be changed. Anyway they stay the same for quite long and
I don't thing they will be changed. I thing the number 2 comes form the
value of HTCAPTION returned by WM_NCHITTEST message when the mouse is over
the caption (HTCAPTION) and I thing it won't be changed. Can't be sure,
though.

How I said the solution has flaws:
1. If the left mouse button is pressed outside the window, moved over the
caption and released then there are prolems with form activation (the
caption to become blue).
2. The applpication may miss WM_NCBUTTONDOWN message if the mouse is moved
quickly out of the caption bar. The form will remain on the same position
instead of moving along with the mouse cursor. That makes the form picky
when ot comes for moving it up or down.

You can try register the form for WM_NCMOUSE LEAVE (TrackMouseEvent API
function) and maintain internal flag for mouse down over the caption bar.
Then you can use that flag in WM_NCMOUSEMOVE instead of MouseButtons ==
MouseButtons.Left. I believe this is more correct and you want miss mouse
moves. I haven't tried and can't say if it's gonna help or not. Anyways good
luck ;)

HTH
B\rgds
100

"NeoAsimov" <ne*******@hotmail.com> wrote in message
news:db*************************@posting.google.co m...
Hello everybody,
Thank for your help, it's really appreciated. I'll check what can
I do with your informations :)

Thank alot,
Salutations,

Nov 15 '05 #6
Hi NeoAsimov,
I'm just curious.
Did you find any solution?

--
B\rgds
100
"100" <10*@100.com> wrote in message
news:uc**************@TK2MSFTNGP09.phx.gbl...
Hi NeoAsimov,
I found probably one of the solutions of your problem.
This solution is not perfect and has flaws, but is good for a starter, I
believe.

private const int WM_NCMOUSEMOVE = 0xa0;
private const int WM_NCLBUTTONDOWN = 0xa1;
private const int WM_NCLBUTTONUP = 0xa2;
private const int HTCAPTION = 2;
private const int SC_MOVE = 0xF010;
private const int WM_SYSCOMMAND = 0x0112;

protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_NCLBUTTONDOWN:
if((int)m.WParam == HTCAPTION)
{
this.Capture = false;
m.Result = new IntPtr(0);
return;
}
break;

case WM_NCMOUSEMOVE:
if(MouseButtons == MouseButtons.Left)
{
m.Msg = WM_SYSCOMMAND;
m.WParam = new IntPtr(SC_MOVE + 2);
}
break;
case WM_NCLBUTTONUP:
if((int)m.WParam == HTCAPTION)
{
this.OnClick(EventArgs.Empty);
}
break;
}

base.WndProc (ref m);
}

I release the mouse capture when the left button is down over the caption.
this will make the OS to send mouse up message. However, releasing the
capture has the efect that the form cannot be moved with the mouse.
That's why we have to start the dragging procedure if necessary by hand.
This is done in responce to WM_NCMOUSEMOVE when the left button is down.
To start moving(dragging) a window with the mouse WM_SYSCOMMAND with SC_MOVE should be pass to the DefWindowProc. the same command is send if the windows is moved from the system meny with the keyboard. To distinguish between the two methods windows uses the low for bits of the wParam. if the low four
bits are 0 - the system menu, 2 - the mouse. These numbers are not
documented and can be changed. Anyway they stay the same for quite long and I don't thing they will be changed. I thing the number 2 comes form the
value of HTCAPTION returned by WM_NCHITTEST message when the mouse is over
the caption (HTCAPTION) and I thing it won't be changed. Can't be sure,
though.

How I said the solution has flaws:
1. If the left mouse button is pressed outside the window, moved over the
caption and released then there are prolems with form activation (the
caption to become blue).
2. The applpication may miss WM_NCBUTTONDOWN message if the mouse is moved
quickly out of the caption bar. The form will remain on the same position
instead of moving along with the mouse cursor. That makes the form picky
when ot comes for moving it up or down.

You can try register the form for WM_NCMOUSE LEAVE (TrackMouseEvent API
function) and maintain internal flag for mouse down over the caption bar.
Then you can use that flag in WM_NCMOUSEMOVE instead of MouseButtons ==
MouseButtons.Left. I believe this is more correct and you want miss mouse
moves. I haven't tried and can't say if it's gonna help or not. Anyways good luck ;)

HTH
B\rgds
100

"NeoAsimov" <ne*******@hotmail.com> wrote in message
news:db*************************@posting.google.co m...
Hello everybody,
Thank for your help, it's really appreciated. I'll check what can
I do with your informations :)

Thank alot,
Salutations,


Nov 15 '05 #7
Hello,
Hi NeoAsimov,
I'm just curious.
Did you find any solution?

No, it seem that the only way that windows xp (not sure for 2000) to
send the WM_NCLBUTTONDOWN message is when the focus of the mouse is
off(like when app's window if full sreen) but after you can't drag the
window.... not really usefull.

Salutations
Nov 15 '05 #8
Hello,
I'll try this out and give you feedbacks and improvements if I found somes ;)

thank!
Salutations,

"100" <10*@100.com> wrote in message news:<uc**************@TK2MSFTNGP09.phx.gbl>...
Hi NeoAsimov,
I found probably one of the solutions of your problem.
This solution is not perfect and has flaws, but is good for a starter, I
believe.


....
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: BadOmen | last post by:
I want my program to send a mouse click to the window at the current mouse position, how do I do that? Example: I have my mouse over a button in Word and then my program is sending the left...
2
by: Serge | last post by:
Hi, in my windows.forms application I have added a goupBox control to my windows.form. Now I want to know the x,y-position from the mouse cursor when I click on the groupBox. The strange thing...
2
by: S³awomir Speth | last post by:
hi all How trap mouse click outside my control - like close popup menu
4
by: Prateek | last post by:
Hi All, I have created an ASP.NET page that basically consists of a table having text boxes in all cells. The table is created using client side java script. There are some calculations being...
2
by: anthonyungerman2000 | last post by:
Is it possible to simulate a mouse click at a certain point within a frame using JavaScript? Thanks
3
by: akowald | last post by:
There's another topic like this but the code posted is in VB.NET. Well I need some help detecting mouse clicks outside the form. An event handler would be great but I can work with anything. ...
4
by: Abhishek | last post by:
Hi, I have a activex web browser embedded in a windows form and on a click of a form button i need the mouse to go the position for example 100, 100 and click the link that will be there on that...
5
by: sagar | last post by:
is there any way to find the position of mouse click on a form actually the problem is i m having more than one controls on a form i want to find which control is selected using mouse down any...
5
by: moonie | last post by:
I have an msn style c# windows application with a form and panel on it. A news list is drawn from the database and dynamically added via labels, link lables during form loading. In this...
0
by: katiezhu | last post by:
I'm working a project. It's about screen control. I write the program using c/c++ in visual studio 2008. I just use my hand to replace mouse to control screen. The code is given below: //Must...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.