I know that this might not be the best place to ask question on .net
but I have a problem regarding GUI programming in .NET
involving win32.

I have the following code on a NativeWindow subclass.
this.handle points to a window and the following code
is executed when a new window is initialized and shown,
hence making the window (this.handle) behind the new

However, the window behind cannot get the focus with the
following code. Could someone please help me how to make
the window behind has the focus so that I can show the

SendMessage(this.Handle, WM_NCACTIVATE, 1, IntPtr.Zero);
SendMessage(this.Handle, WM_SETFOCUS, 1, IntPtr.Zero);
CreateCaret(this.Handle, IntPtr.Zero, 0, 0);

Thank you very much in advance.
Posted on 2004-07-02 03:58:18 by atzplzw
You need to use SetFocus(). The WM_SETFOCUS message is sent as a notification that the focus has changed, but it does not change the focus itself.

Hope that helps :)
Posted on 2004-07-02 10:33:04 by QvasiModo
Hi QvasiModo,

Thank you for your reply.
I changed it to

SendMessage(this.Handle, WM_NCACTIVATE, 1, IntPtr.Zero);
CreateCaret(this.Handle, IntPtr.Zero, 0, 0);

But still it didn't work.
Do you know why?

Thank you in advance.
Posted on 2004-07-02 11:44:19 by atzplzw
I think WM_NCACTIVATE is also a notification, and there should be another API call for that. But I don't really know why it's not working... :confused:

Do you know if any of those calls are returning an error? That might give us a hint.

if( SetFocus(this.handle) == NULL )
MessageBox(this.handle, "Error in SetFocus().", "Error", MB_OK );
if( SetActiveWindow(this.handle) == NULL )
MessageBox(this.handle, "Error in SetActiveWindow().", "Error", MB_OK );
if( CreateCaret(this.Handle, IntPtr.Zero, 0, 0) == NULL )
MessageBox(this.handle, "Error in CreateCaret().", "Error", MB_OK );
if( ShowCaret(this.Handle) == NULL )
MessageBox(this.handle, "Error in ShowCaret().", "Error", MB_OK );
Posted on 2004-07-03 10:00:33 by QvasiModo