473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 14945
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 problematic. Given the following code: <HTML>...
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 maintained when the application is deactivated....
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 frmDealerSearch and put the focus on txtDealerNum on...
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, but that doesn't work. I think it's because a...
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 right. The problem I'm having is the focus will not...
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. Everything seems to work fine but the problem I...
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 onload="..."tag. What are the differences between...
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 hope someone might have an idea about what's going...
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 time. One or more of these forms have a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.