Keyboard focus: looking for a general strategy - c++

My application has a big graphics area with some controls (sliders, buttons, text edit controls) in a side panel. The graphics area understands some keyboard commands.
My problem is that when a control in the side panel is in focus, the main graphics area won't receive any keyboard commands, so this confuses the users. However, for some controls, this is intended, e.g. text edit controls.
What I want is the focus to automatically return to the graphics area at the earliest possible occasion (which I call "greedy" focus) -- e.g. when text editing is finished (Enter key), or when the user has selected an item from a combo box.
I am looking for a clean and robust strategy for dealing with the problem, either using Windows API or Borland Vcl.
I'd appreciate if you want to share your ideas.

I haven't fully solved the problem yet, but a very useful message to intercept on form level is CM_DIALOGKEY (Borland Vcl only). It gets sent for every key that is normally used for navigating within the UI. That is, cursor keys, tab and shift-tab, Enter and possibly others.
I've added an event handler for CM_DIALOGKEY that returns the focus to the graphics area and also forwards the key press to that component. This way the user can still control the UI elements via keyboard (important for text entry), but cursor keys are handled by the graphics area.

I know what you mean I had similar problem with some BIG apps lice CAD/CAM ...
My solution is simple, robust and I use it for years.
1.all keystrokes handling for that gfx area
must be done in events of the Form where the area is located
not in panels,paint box whatever...
2.create unfocus function (preferably member of form but it is not required)
this function will loose focus of any VCL item
so the focus goes to form itself which is what you want
I am using this:
//---------------------------------------------------------------------------
void main_unfocus()
{
Main->bt_unfocus->Visible=true;
Main->bt_unfocus->SetFocus();
Main->bt_unfocus->Visible=false;
}
//---------------------------------------------------------------------------
Main is the Form pointer
bt_unfocus is button (can be any focusable VCL component)
I place this button usually on the left upper corner of App and set its size to 2x2 pixels
it is invisible at start
the idea is to set focus to it (but first unhide it so no exception is thrown)
and then hide it so it loses focus
work well in BCB5 and BDS2006 (did not used it with any other IDE)
as you can see most of the time is this component invisible
4.now when to call main_unfocus ?
when you go with mouse from outside to inside of your gfx area (OnMouseMove event)
or when you click on it
also you can combine this with remembering if the focus is or not in gfx area
that can be done with events like OnExit ...
or when you hit Esc while focus is inside edit box ...\
I am sure you grasp the idea and adjust this to your needs
Hope it helps

Related

Qt MouseMoveEvent only triggers with a mouse button press

I have an odd problem here.
I'm working on an application, and within one of my classes I'm monitoring my mouse events.
The weird thing is, my mouse move event will only get called if any mouse button is pressed.
I'm not even filtering for any button presses within the method; the method itself doesn't even get called unless I click on this object itself (the one that's monitoring it).
What generally causes this type of error to happen?
I'm not sure if it's relevant, but I have 2 different things monitoring my mouse inputs: 1) the main program monitoring the global mouse coordinates, and 2) an object within my program monitoring the mouse coordinates within itself.
Edit
So the problem has to be because mouse move event is generally used when people are dragging the cursor along the screen right?
My reason for not needing it like that is because I'm building a custom context menu of sorts, and I need to know when an item is hovered over.
It turns out that I didn't truly set everything within my class to enable mouse tracking.
I somehow thought if the class itself was set to have it enabled, I wouldn't need to set it to all the sub objects, but now I see how that wouldn't make any sense at all.
So just to clarify my solution:
The items that I needed to track my cursor's position needed to have
setMouseTracking(true);

Capture mouse clicks on text boxes in a Win32 game

Using C++, I am making a Tic-Tac-Toe game using the Win32 API. To mark a square (X or O) I want the player to click the square which then changes to an X or O.
What I am doing right now is having a button click event which turns a static text box to X or O. However, when I place the button on top of the text box and make it not visible, I can't click it.
What I really need is an invisible button that still functions. So it's not set WS_VISIBLE, but you can still click it.
Is this possible or is there another way around this problem?
I can see a couple of reasonable possibilities here.
The first and most obvious would be to skip using a button at all, and just have the underlying window process the WM_LBUTTONDOWN message, and set the "X" or "O" in the correct location. For this, you don't even need static controls -- you can just detect the mouse clicks directly on the parent window, and draw your "X" or "O" in the corresponding square.
Another possibility would be a button that's marked as "visible", but happens to be transparent. IMO, this is a fairly poor choice though. To do it, you'd need to either create a transparent button control on your own, or subclass a button control to disable its drawing.
At least IMO, the obvious route would be to skip using the static control at all. Instead, just use the buttons directly -- a button normally has a caption. Start with that caption as an empty string. When the button is clicked, change its caption to "X" or "O" as appropriate. It should probably also disable itself in response to the button click, so clicking it again won't have any further effect.
There's no way to make an invisible button that still functions. Imagine all of the ways that could be abused if it were possible! Not to mention how confusing to have invisible, yet functional, UI.
What Mark Ransom posted is exactly right: you need to get your existing control to respond to mouse click events, just like a button does. Then you can do whatever you want in response to clicks. You don't need a button just to be clickable.
You say that you have a "static text box", but I'm not really sure what that is. There are text boxes (which are not static), and then there are static controls (which can display text). I'm going to assume that you have the latter.
In that case, you don't need to handle the WM_LBUTTONDOWN and WM_LBUTTONUP messages directly, which would require that you subclass the control. Although that's probably the best approach design-wise (separation of responsibilities and all that), it's also a lot more trouble.
Instead, you can handle the click events from the parent's window procedure by setting the SS_NOTIFY style for your static control (you can do this either in the Dialog Editor or in your call to CreateWindow, depending on how you create the control). This causes the control to notify its parent in four cases: when it is clicked (STN_CLICKED), when it is double-clicked (STN_DBLCLK), when it is enabled (STN_ENABLE), and when it is disabled (STN_DISABLE).
So at the parent, you need to process WM_COMMAND messages. The message you're looking for will have a HIWORD(wParam) of STN_CLICKED (indicating that a static control with the SS_NOTIFY style has been clicked), a LOWORD(wParam) corresponding to your static control's ID (set either in the Dialog Editor or specified as the hMenu parameter in your call to CreateWindow), and an lParam containing a handle to your static control.
If you use SW_HIDE, it doesn't just make the window invisible but makes it behave like that too. What you really wanted is probably just make the button transparent. I never did that, you may find this or this helpful.
You may just scrap the textbox just use the button, i mean a button-looking checkbox with ownerdraw or bitmaps. Or scrapping the button and handle the mouse events Like Mark suggests.

How can I receive or be able to process mouseMoveEvent(s) outside my widget window?

I am writing Qt application which plays a fade-in animation whenever the mouse is moved to a certain area in the screen, and a fade out animation whenever the mouse is moved out of that same area.
I've already found a similar question here in stack overflow, however, it does not quite answer my question. (similar question here)
If I install an event filter to the application, will I be able to see ALL the events in the application even if it's outside my widget window?
If not, I am aware of an alternative involving QWidget::grabMouse() inside a reimplementation of leaveEvent(). But if I do so, will I be able to interact with anything OUTSIDE my application?
edit: though i am using the Qt library, my application is only for deployment to Windows.
I'm fairly the certain the answer is no, because events outside of your widgets are handled by the OSs window manager (and propagated onto whatever application is in that space).
However you can get the mouse position anywhere on the screen by calling QCursor::pos(), you could poll at regular intervals to find out where the mouse is.
You could try creating a completely transparent window that stays on top of the area where you want to receive mouse events, with the Qt::WindowStaysOnTopHint, Qt::FramelessWindowHint and Qt::ToolTip flags (the last one might prevent the window from receiving the focus), the Qt::WA_TranslucentBackground attribute and the mouse tracking enabled.
If you are on Windows, you can create a global hook to receive every mouse message (right before it's sent to the window under the mouse pointer). Unfortunately I don't know whether this functionality exists in other OSs.

effective Overlapping of dialog windows in Visual C++ 6

Hello there I have issue with overlapping of child windows,I have created a software with menu driven interface( IDR_MAINFRAME - CFormView
etc) and upon clicking one of the menu items another child-window appears( Dialog based ) where I do the calculations as a normal
calculator.Now if I open any other entry say conversion of metrics which is also in menu entry then on overlapping with any other such
window the background windows gets horribly disfigured and if i move about the calculator or the metrics conversion calculator randomly
they get disfigured and its a mess.Also I have put up a bitmap image on the background.Upon moving the calculator the background image also
gets erased.
Please let me know about how to handle this issue.I have googled and found that handling of paint messages or WM_ERASEBKGND helps ..but I
have tried this piece of code which just doesn't help in OnEraseBkGnd();
BOOL COfficesoftDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CRect Rect;
GetClientRect(Rect);
//ClientToScreen(&Rect);
//this->ScreenToClient(&Rect);
this->InvalidateRect(Rect);
return CDialog::OnEraseBkgnd(pDC);
}
how can i achieve the smooth overlapping of different windows like a notepad overlapping a word document or even a calculator or even a VC6
IDE in my project.
Please explain it with an example .I am just a newbie and I need to understand in detail...thanks and regards
Override OnEraseBkgnd and return true so it stops erasing the background you're painting. Returning TRUE says that you've done the work. If you simply call the base class implementation, it's going to do this for you, and you'll lose the background until it gets a chance to paint.
You're not getting paint messages to the parent window for some reason. Make sure you're calling the modal in the correct manner. DoModal() works fine. Make sure you're not just creating the modal and showing it.
If your windows are children on the same dialog/window and they overlap or you have children on either dialog/window, make sure that you use clipchildren and clipsiblings (if children on a window overlap). Otherwise they'll get to paint in any order they choose artifacting all over the place.
Ensure that you're painting to memory and bitblting back to your dialog, otherwise you'll get a flashing effect.

vc++ forms window

i have created a form using visual c++. all im trying to do is this when a button is clicked the form is gone and new window will be showen. i don't know if i should repaint the window or there is another solution? please help
Rami
What I would do is display your game in the client area of the MainWindow of your app. In (possibly) Init_Instance() also call your form for the login. When the user has filled it in, just close that window and your game will already be behind it. You may need to refresh the screen afterwards so you don't get any artifacts.
Also, just noticed the "how to paint the whole thing" question.
myWindow.Invalidate();
http://msdn.microsoft.com/en-us/library/2dtwaxz7(VS.71).aspx
myWindow.InvalidateRect(LPCRECT lpRect, BOOL bErase = TRUE);
http://msdn.microsoft.com/en-us/library/62w186xw(VS.71).aspx
Do not put it in the actual paint event, OnPaint().
I'm just answering the question.
Trap the button-click event, IOW, create a function for it and close the current window. Then call / create the new window in the same event / function. You'll most likely need a class level pointer to "hold" the current window.
No offense, but this would be a fairly bad design, unless you are making a "Wizard" type of construct. That is one window closing actually should bring up the next window.