detecting a right-click

Posted on 2001-08-21
Last Modified: 2012-06-21
I wanted to make a program that doesn't have a common windows title bar and that can be dragged by clicking anywhere in the form (winamp style).
So I used the following function :

void __fastcall TForm1::WMNCHitTest(TMessage &Msg)
    Msg.Result = HTCAPTION;

This funtion traps the WM_NCHITTEST message and by returning HTCAPTION allows the form to be dragged (just like if I clicked in the title bar).
As said in a help file : "The WM_NCHITTEST message is sent to a window when the cursor moves, or when A mouse button is PRESSED or RELEASED ". So the "OnMouse" events don't work anymore. The problem is, I want to detect a right-click in order to show a popup menu and as all the mouse events are trapped by that function, how can I know if a right button was clicked ?

Question by:wizcool
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 6411985
I forgot to say that I'm using C++Builder.

Expert Comment

ID: 6412112
Click on the form, go to the properties box on the left, select (double click) the OnMouseDown event, and then in your code examine the provided objects (Button and Shift) to determine which button was pressed.

BTW, there is an standard way of getting a pop-up menu in C++ Builder, although I can't remember exactly where you find it. I'll look this afternoon and let you know.

Accepted Solution

BeyondWu earned 200 total points
ID: 6412265
I'm familiar with C++Builder.I use VC.
I think you should not catch the WM_NCHITTEST.(remove it)
void __fastcall TForm1::WMNCHitTest(TMessage &Msg)
   Msg.Result = HTCAPTION;

you can only catch WM_LBUTTONDOWN, if you want to move the window when the user press the left button.
void __fastcall TForm1::WMLBUTTONDOWN(TMessage &Msg)
   //Msg.Result = HTCAPTION;
   // MAKELPARAM(x,y) is the current mouse point.
   // maybe you can find it in the TMessage.

Now you can catch the right-click.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6412268
Sorry, it should be "I'm not familiar with C++Builder"
LVL 14

Expert Comment

ID: 6412909
1. Usually use next code
void __fastcall TForm1::WMNCHitTest(TMessage &Msg)
Msg.Result = HTCAPTION;
Msg.Result = HTCLIENT;
2. For detect Right click from WMNCHitTest
(or from any other function) you can use
GetAsyncKeyState API:
if (GetAsyncKeyState(VK_RBUTTON)<0 ) //right button?

Author Comment

ID: 6420304
Although both BeyondWu's and AlexVirochovsky's solutions worked I can only accept one. So I accepted BeyondWu's because i liked it better and because he also answered first.

Thanks to you all.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question