Front-End Window in. NET CF

Source: Internet
Author: User
In the UI that contains multi-level pages, you should be allowed to select the top-level window to work. However,. Net CF does not provide a method to force the window to the front end, but Win32API provides this method -- SetForegroundWindow function: http://msdn.microsoft.com/en-us/library/ms940024.aspx
This function is suitable when there are multiple forms in the program and the form to be controlled is visible to the user.
The BringWindowToTop http://msdn.microsoft.com/en-us/library/aa452899.aspx provides similar functionality.
In addition, FindWindow http://msdn.microsoft.com/en-us/library/aa453070.aspx can find the form you want, just know the name of the form (2nd parameters) can be, but the premise is that the form is alive, but it runs below.
The following is the test code:
1. create two new forms and add a ButtonCode on Form1
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;

Namespace toplevel
{
Public partial class Form1: Form
{
Private Timer timer1;
Private Form2 fm2;

Public Form1 ()
{
InitializeComponent ();

Fm2 = new Form2 ();
// Fm2.Visible = true;


This. timer1 = new Timer ();
This. timer1.Interval = 5000;
This. timer1.Tick + = new EventHandler (timerstmtick );

This. timer1.Enabled = true;
}

[DllImport ("coredll. dll")]
Public static extern IntPtr GetFocus ();
[DllImport ("coredll. dll")]
Public static extern int SetForegroundWindow (IntPtr hWnd );
[DllImport ("coredll. dll")]
Public static extern IntPtr FindWindow (string lp1, string lp2 );
[DllImport ("coredll. dll")]
Public static extern int BringWindowToTop (IntPtr hWnd );
 


Private void timereffectick (object sender, EventArgs e)
{

IntPtr hWnd = FindWindow (null, "Form2 ");

If (hWnd = IntPtr. Zero)
MessageBox. Show ("window is null ");
SetForegroundWindow (hWnd); // OK
// BringWindowToTop (hWnd); // OK

}

Private void button#click (object sender, EventArgs e)
{
Fm2.Show ();
}

}



}

Click Button1 to display Form2 and then minimize it and return to Form1. After the Timer time is reached, Form2 will be placed on it.
In addition, pay attention to fm2.Show () instead of fm2.ShowDialog (), because ShowDialog () will execute the next command until the form is closed.

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.