472,338 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 software developers and data experts.

How do I set constant focus on windows form

sam
hi,
How to set constant focus on window form in c#.

cheers

Sam
Nov 23 '05 #1
4 14835
What do you mean by "constant focus"? Is that a window that can never be
hidden? (ie shows on top of other windows regardless)

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 23 '05 #2
sam
Yes, window that can never be hidden and lose focus. In another words i want
focus on my window form1 for all times even if mouse clicks on other
windows, focus should be on the window form1.
I hope it will be clear to u

cheers,

Sam

"vooose" <no****@microsoft.com> schreef in bericht
news:O4**************@TK2MSFTNGP12.phx.gbl...
What do you mean by "constant focus"? Is that a window that can never be
hidden? (ie shows on top of other windows regardless)

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***

Nov 23 '05 #3
sam,

I don't think this is possible. Back in the time of win16 applications there
was a "system modal" style dialogs. They was meant to be used exactly for
this. However this style was deprecated for Win32 because such application
may cause the whole system to lock up. Actually Windows tries to make sure
that any application can be deactivated so the control cannot be taken over.

Speaking of .NET there is no even managed solution for activating an
application. If one wants to do that one needs to resort to using PInvoke
and calling Win32 API methods.

I played little bit with this and the following is the code I get not too
bad results with. To play with this code create new empty winforms project,
set the TopMost property of the form to *true*, copy and paste this code
into the form's class, add a timer (timer1) and hook its Tick event to the
timer1_Tick method. You shold add *using* clause for
System.Runtime.InteropServices namespace.

private const int WM_ACTIVATEAPP = 0x001C;
[DllImportAttribute( "User32.dll" )]
private static extern int SetForegroundWindow( IntPtr hWnd );

protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
if(m.Msg == WM_ACTIVATEAPP && ((int)m.WParam) == 0)
{

this.timer1.Interval = 1;
this.timer1.Start();
}
}

private void timer1_Tick(object sender, System.EventArgs e)
{
this.timer1.Enabled = false;
SetForegroundWindow(this.Handle);
}

Using timer may look wierd, but my tests show that we need to give windows
some time after sending WM_ACTIVATEAPP to set up all the active/inactive
window stuff otherwise the result is not as good.

Again keep in mind that this is not the best solution possible. I believe
better results can be achieved installing WH_CBT hook. The problem is that
global CBT hook cannot be written in managed code, therefore the hook needs
to be written in native code and compiled in native DLL then the hook can
control activating and deactivating windows.

BTW why don't you create a full screen application that covers the whole
desktop. This way at least you can make activating via mouse clicks
inconvenient.
--

Stoitcho Goutsev (100) [C# MVP].

"sam" <s@s.com> wrote in message
news:e4****************@TK2MSFTNGP15.phx.gbl...
Yes, window that can never be hidden and lose focus. In another words i
want
focus on my window form1 for all times even if mouse clicks on other
windows, focus should be on the window form1.
I hope it will be clear to u

cheers,

Sam

"vooose" <no****@microsoft.com> schreef in bericht
news:O4**************@TK2MSFTNGP12.phx.gbl...
What do you mean by "constant focus"? Is that a window that can never be
hidden? (ie shows on top of other windows regardless)

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***


Nov 23 '05 #4
Sam have you tried ShowDialog()?

This will kind of do what you want if you want your form to be on top
and remain focused within your application *only*

(ie you can still click on other applications and give them focus)

If you require no other application to have focus then it certainly is
unusual, aggressive functionality!

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Scott Navarre | last post by:
Hi, I have Red Hat 8.0 and have the default Mozilla browser that comes with it. I am programming in javascript and have come across something...
0
by: Vinod. | last post by:
Hi all, I have added browser control to a windows form. I am loading pages having multiple frames. I have noticed that the focus in a text is not...
13
by: Mike L | last post by:
I have a child form frmDataEntry call up another child form frmDealerSearch. If the user clicks on cancel on frmDealerSearch, I want to close...
8
by: copyco | last post by:
I want to be able to test if my app's form has focus, (ie: not in background, behind other windows). How can I do this? I tried the Me.Focused,...
1
by: mongphong28 | last post by:
Hi, I'm using a treeview as a menu, and when the user clicks on a node I want the focus to set to a control (ie textbox) in a panel to the...
4
by: Jon Slaughter | last post by:
I've created some custom controls and forms that allow the feature to temporarily transfer focus to a control that has been entered by the mouse....
4
by: Roger | last post by:
Hi, I am confused about the differences between this.window.focus(), window.focus(), and this.focus(). I want to use the calls in a <body...
3
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and...
2
by: Joergen Bech | last post by:
Hope someone has a solution or some suggestions for this. This cannot be right?!? Problem: I have multiple non-modal forms open at the same...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.