The prompt box c # in the lower-right corner of the imitation QQ member prompts that the form is not focused

Source: Internet
Author: User

Although there are examples of forms that do not obtain the focus, I don't know how to implement them, so I still want to "do not get the focus of the form" first, then, return the focus to the previous focus form.

You don't need APIs at the beginning. You can use Application. OpenForms to get all the forms of your program, and then display yourself. Of course, you will get the focus, and then the previous

If there is a focus, there will be events that lose the focus, but they will not come out. For API changes, the idea will be the same.

APIs used

[System. Runtime. InteropServices. DllImport ("user32.dll")]
Public static extern IntPtr GetActiveWindow (); // obtain the current active form
[System. Runtime. InteropServices. DllImport ("user32.dll")]
Public static extern IntPtr SetActiveWindow (IntPtr hwnd); // sets the activity form

The prompt box of QQ can have a focus, but it is not worth the focus.

So what I want to do is "Don't grab the Focus" when I display it for the first time"

Write member variables

Private IntPtr actform = IntPtr. Zero; // Save the handle that owns the active form before getting the focus

Private bool isFristShow = true; // identify whether it is loaded for the first time

Add in Constructor

Actform = GetActiveWindow ();

Override the OnActivated method of Form

Protected override void OnActivated (EventArgs e)
{
Base. OnActivated (e );


If (hwnd! = IntPtr. Zero & isFristShow)
{
SetActiveWindow (hwnd );

// It is no longer the first time loading, so you can focus on it next time.

IsFristShow = false;
}
}

I only think so much, who has a better way to teach me QQ: 122281069

Another question

In the above-mentioned link, there is a "codingsilence" saying this

"Using Windows APIS is much better than you, and the code is one in ten"

I also want to implement it, but I don't know if it is the same as what he thinks. Of course, there are advantages and disadvantages. First, let's give the code and talk about the gains and losses.

Write the API first

// Function: This function can have special effects when displaying and hiding windows. There are two types of animation effects: Scroll animation and slide animation.

[System. Runtime. InteropServices. DllImport ("user32.dll")]
Public static extern void AnimateWindow (IntPtr hwnd, int stime, int style );

This function can be used in loading and disabling other events.

Here, we only write an example of the effect when opening. Because it is simple and not much to write, add this in the form.

Protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e );

// The second parameter is the gradient time.
AnimateWindow (this. Handle, 2000, 0x80000/* AW_BLEND */);
}

Ha, strong, it's so complicated in a single sentence, but his disadvantage is that it's best to use multithreading for a long time.

Because this function needs to be executed for a long time, it will cause other interfaces on the same thread to not respond to user operations during this time period, but it is still better in general.

You see this. Can you pin it down...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.