focus on form...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: focus on form...

  1. #1
    Join Date
    Mar 2011
    Posts
    27

    focus on form...

    Hi there a new riddle.
    I have a form with some controls on it and a gl panel. I am using mouse_wheel event for form 1 to capture the wheel. It works fine however i can go to a state that event doesn't fire. I guess it is because of loosing focus, only way to bring it back is to click to any button tab whatever control after that the mouse wheel works back. I was trying many options but can't solve it.

    Any ideas how to bring the focus ?
    Edit////
    I guess that focus is on gl panel because its mouse events are executed correctly.
    Last edited by smallbit; October 6th, 2011 at 03:45 AM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: focus on form...

    I think you're referring to this: http://www.codeproject.com/KB/miscct...px?msg=1726934. However, I'll mostly ignore the OpenGL part for now.

    Not long ago, I encountered a similar problem, though with a picture box instead of a panel. I solved it by setting the Enabled property of the picture box to false which effectively makes it "transparent" with respect to mouse events. This would have an adverse optical effect on the usual GUI elements like buttons, but it doesn't on the picture box.

    However, in my experiments the panel behaved differently: The panel itself only received MouseWheel events at all if the panel was enabled and a control that's a child of the panel had the focus and the panel itself was enabled. But, most important, in any case the form received the events as well.

    So I think the culprit is the native OpenGL window that intercepts the events. I didn't make any experiments with that, but if you don't need the user to directly interact with the OpenGL window, I suggest you try to create that disabled by specifying the additional window style WS_DISABLED.

    At any rate, if, as you say, your panel does receive the MouseWheel events, you can always forward them to the form's event handler, somewhat along the lines of this:

    Code:
    void Form1::panel1_MouseWheel(Object ^sender, MouseEventArgs ^e)
    {
      Form1_MouseWheel(sender, e);
    }
    Note that in this case the sender passed to the form's event handler actually will be the panel object of course. That can be useful to determine where the event actually came from in the form's event handler, but don't try to blindly cast the sender object to the form class in that scenario.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Mar 2011
    Posts
    27

    Re: focus on form...

    Thanks Eri
    Thats is a tricky solution but won't work, because the panel mousewhell event never fires. Other events i need to use (mose move etc) works correctly. I can't disable it for that reason.
    Code:
    private: System::Void VisualPanel_MouseWheel(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
    			 Form1_MouseWheel(sender, e);
    		 }
    and
    Code:
    this->VisualPanel->MouseWheel += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::VisualPanel_MouseWheel);
    The GlPanel is a System::Windows::Forms::Panel so the mousewheel event should obviously work.

    One solution I have found is to force the focus to one of the form elements in example a button in glpanel Mouse move event. With that it works correctly.
    Code:
    button1->Focus();
    This is not very elegant but works. I have tried to give focus to the form itself but since I "am" in the glpanel this->Focus(); won't work. And any other attempt fails too. I must admin I have no idea how to adress the Form there in order to make it work .

    Thanks for your time,
    Best Regards
    Last edited by smallbit; October 7th, 2011 at 02:09 AM.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: focus on form...

    Quote Originally Posted by smallbit View Post
    Thats is a tricky solution but won't work, because the panel mousewhell event never fires. [...]
    Oh... I interpreted this sentence from your OP:

    Quote Originally Posted by smallbit View Post
    I guess that focus is on gl panel because its mouse events are executed correctly.
    like the MouseWheel event of the panel would work, just like the panel's other mouse evets.

    Quote Originally Posted by smallbit View Post
    [...] Other events i need to use (mose move etc) works correctly. I can't disable it for that reason.
    Perhaps we have a misunderstanding here: I didn't mean to disable the panel, but the native window it hosts, i.e. the COpenGL instance, by creating it with the additional window style WS_DISABLED.

    [...]

    The GlPanel is a System::Windows::Forms::Panel so the mousewheel event should obviously work.
    Agreed.

    One solution I have found is to force the focus to one of the form elements in example a button in glpanel Mouse move event. With that it works correctly.
    Code:
    button1->Focus();
    This is not very elegant but works.
    Actually, I was considering an approach like that too when writing my last post, but refrained from posting it because it would very likely irritate the user by every now and then having the focus jump to that button for no apparent reason. In the meantime, however, I had an idea how to improve the approach: Give every control that's candidate to resetting the focus to it an event handler like this:

    Code:
    void Form1::button1_GotFocus(Object ^sender, EventArgs ^e)
    {
      m_ctlLastFocused = safe_cast<Control ^>(sender);
    }
    where m_ctlLastFocused is a form class member variable of type Control ^. Of course you can also use the control variable (button1 in the above case) instead of the cast sender parameter, but then each control would need its own event handler. The way I've written it above, you can use the same singular handler for all the controls in question. Should any of the controls already have a specific handler for that event, you can forward the event from there to the generic handler by simply calling it, much like forwarding the MouseWheel event as I described above.

    Then, when you want to reset the focus, you use a line quite similar to the one you seem to be using at the moment:

    Code:
    m_ctlLastFocused->Focus();
    Alternatively, you may use the Enter event for that purpose instead of GotFocus, which according to MSDN is the preferable option. See http://msdn.microsoft.com/en-us/libr....gotfocus.aspx.

    I have tried to give focus to the form itself but since I "am" in the glpanel this->Focus(); won't work. And any other attempt fails too. I must admin I have no idea how to adress the Form there in order to make it work .
    If you "are" in the panel, I suggest to try Parent->Focus(); instead. Surprisingly simple, isn't it? However, I'm afraid the Focus() method may not have the desired effect when called on a form (didn't test that, though). But as it's really little effort, I think it's worth a try anyway.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center