Prevent my app from getting the focus

D

Dave

Hello,

I am writing an On-screen keyboard, similar to the one included in
windows 2k and XP.

The problem I am having is that I need my keyboard to never get the
focus, but still be able to process events such as mouse clicks.

I have tried setting focus back to the app that just lost the focus,
however in smoe application this does not work as the caret/cursor is
not returned to its original position. Therefore, I need to stop the
app that I'm writing to eg. IE or notepad, from ever losing the focus,
or rather, stop my app from activating/getting the focus.

I have tried various API's using various flags, but to no avail.

The closest I got was by using the SetWindowLong API with the
WS_EX_NOACTIVATE flag, which does stop my app from ever getting focus,
but it also makes it transparent and unable to receive any mouse
events.

I have been struggling with this for a couple of weeks now, so any
help would be greatly appreciated.

Cheers,

Dave :blush:)
 
G

Guest

You must override the WndProc and listen to the following messages

WM_ACTIVAT
WM_MOUSEACTIVAT

and all the mouse button messages. You must send yourself the messages to the OnMouseDown routines and not call the base class mouse routines (who performs a FocusInternal method). Also, in all of them you must not call the base.WndProc, instead set the m.Result = 0 and return. The only exception is WM_MOUSEACTIVATE where you must set the m.Result = 3, which means let the mouse events go thru but don't issue WM_SETFOCUS.

I use this method very much in my custom popup controls. The only problem is that if you add any child controls to the main form or control, you must derive each and everyone of them and do the same thing..

Here is an example of a button which when placed on a form will not cause that form to get focus when pressed. Note that in this case you can call the base class mouse routines. However, if you derive from a UserControl you have to avoid it. Also, this example has a logic bug, the DoubleClick should be replaced by the same code as the click, as a double click on a button makes no real sense

[ToolboxItem(false)
private class SpecialButton : Butto


protected override void WndProc(ref Message m

if (m.Msg==0x203)

m.Result = (IntPtr)0
this.OnDoubleClick(new EventArgs())
return

if (m.Msg==0x201)

m.Result = (IntPtr)0
Point p = this.PointToClient(Control.MousePosition)
this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, p.X, p.Y, 0))
if (this.GetStyle(ControlStyles.StandardClick)

base.OnClick(new EventArgs())

return

if (m.Msg == 0x202)

m.Result = (IntPtr)0
Point p = this.PointToClient(Control.MousePosition)
base.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, p.X, p.Y, 0))
return

if (m.Msg==0x21

m.Result = (IntPtr)3
return

if (m.Msg==0x6

m.Result = (IntPtr)0
return

base.WndProc(ref m)



hope this helps
iulia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top