| |

VerySource

 Forgot password?
 Register
Search
View: 2174|Reply: 16

How to achieve the form is always activated? And how to block keyboard combinations? Please come and see for free!

[Copy link]

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-6 17:10:01
| Show all posts |Read mode
How to achieve the form is always activated? That is, when the user's mouse or keyboard combination (Alt + Tab goes to another application or Alt + F4 closes this form) and wants to transfer to another form or application, the form can return directly to the original state, so The form remains active.
There are 2 issues:
1. How to make the form return to Activate state in time when the form is deactivated with the mouse?
2. How to shield various combinations of keyboard operation?
Why can't the following code achieve the first problem?
private void form1_Deactivate (object sender, EventArg e)
{
this.Activate ();
this.Focus ();
}
The following code is based on Yu Gong's code, but when this form is closed, an error "The target thread does not exist" will appear in ThreadFun.
private void InvokeForm ()
        {
            this.Activate ();
            this.Focus ();
        }

        private void ThreadFun ()
        {
            this.Invoke (new MethodInvoker (this.InvokeForm));
        }

        private void LoginForm_Deactivate (object sender, EventArgs e)
        {
            Thread thread = new Thread (new ThreadStart (this.ThreadFun));
            thread.Start ();
        }
How should I correct it?
Please greatly advise. !! !!
Reply

Use magic Report

0

Threads

11

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-6 20:24:01
| Show all posts
Friendly Bangding
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-6 20:33:01
| Show all posts
top! !! !!
LZ
SF
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-1-7 00:27:01
| Show all posts
Get focus when form loses focus
BlockInput () API can block most IO
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-9 12:18:01
| Show all posts
to苹果小刀:
BlockInput () is a function that can block the keyboard and mouse, but the result is that people can't move anything.
Oh, I can only ctrl + alt + delete to end the program. But thank you for your suggestions.
Reply

Use magic Report

0

Threads

23

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-1-10 16:45:01
| Show all posts
Winproc intercepts system messages
Reply

Use magic Report

1

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-1-18 16:27:02
| Show all posts
Can set a timer, interval = 100
Check if the title of the currently focused form is this program
'Get the current form handle
Public Declare Function GetActiveWindow Lib "user32" Alias ​​"GetActiveWindow" () As Long
'Get title
Private Declare Function GetWindowText Lib "user32" Alias ​​"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'Flash form
Public Declare Function FlashWindow Lib "user32" Alias ​​"FlashWindow" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
'Activate the form
Public Declare Function SetActiveWindow Lib "user32" Alias ​​"SetActiveWindow" (ByVal hwnd As Long) As Long

If you want to restrict the user to use a certain key combination or some mouse operations when this window is not activated, it is more complicated
But mainly use api function hook to monitor, use sendmessage, postmessage and other functions to block certain messages of the system, it is very complicated. . .

If it is active, keyboard events, use the form's keydown event (the form's keypreview property must be true) can be monitored and blocked, the mouse has to use the hook function
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Invalid IP Address

Post time: 2020-2-8 09:15:01
| Show all posts
test ok on .net2

public class T: Form
{
public T ()
{
}
Ranch
protected override void OnDeactivate (EventArgs e)
{
Thread t = new Thread (new ThreadStart (SetFocus));
t.Start ();
}
Ranch
public void SetFocus ()
{
this.Activate ();
}
Ranch
Ranch
public static void Main ()
{
Application.Run (new T ());
}

}
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-6-20 15:00:01
| Show all posts
toledmirage1:
Are you sure you have tested the above code?
In addition to not being able to achieve the functionality I wanted, there was an error. When a form deactivation event occurs, "Inter-thread operation is invalid: the thread that did not create the control "LoginForm" accessed it." error.
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Australia

Post time: 2020-7-4 07:45:02
| Show all posts
After doing the test, you can directly copy and compile and execute the source code.

I think something went wrong when you applied it to your project, because I didn't use LoginForm, post your code and see.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list