好像设置topmost没有效果
比如后台程序(msn)之类的,探出消息框

解决方案 »

  1.   

    从后台弹出窗体是什么意思。Topmost 
    或者 ShowDialog()方法来显示窗体。消息框就是 MessageBox.Show()的各种重载。
      

  2.   

    一个测试程序
    Program.cs
    创建TestForm窗体using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;namespace TestTopMost
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new TestForm());
            }
        }
    }
      

  3.   

    TestForm.cs
    每1秒产生一个TestMessage窗口using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;namespace TestTopMost
    {
        public partial class TestForm : Form
        {
            private System.Timers.Timer m_timer = new System.Timers.Timer(1000);        private static bool m_bThreadRunning = false;
            private static object privateObjectLock = new object();        static bool GetThreadRunning()
            {
                bool bReturn = false;
                lock (privateObjectLock)
                {
                    bReturn = m_bThreadRunning;
                }
                return bReturn;
            }        static void SetThreadRunning(bool run)
            {
                lock (privateObjectLock)
                {
                    m_bThreadRunning = run;
                }
            }        public TestForm()
            {
                InitializeComponent();
                m_timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerHandler);            m_timer.AutoReset = true;
                m_timer.Enabled = true;
            }        public void TimerHandler(object source, System.Timers.ElapsedEventArgs e)
            {
                Thread thread = new Thread(ThreadFunc);
                thread.Start();
            }        public static void ThreadFunc()
            {
                if (GetThreadRunning() == false)
                {
                    SetThreadRunning(true);                TestMessage msg = new TestMessage();
                    msg.ShowDialog();                SetThreadRunning(false);
                }
            }
        }
    }
      

  4.   

    TestMessage.cs
    弹出1秒钟之后销毁自己using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace TestTopMost
    {
        public partial class TestMessage : Form
        {
            private System.Timers.Timer m_timer = null;        public TestMessage()
            {
                InitializeComponent();            m_timer = new System.Timers.Timer(1000);
                m_timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerHandler);
                m_timer.AutoReset = false;
                m_timer.Enabled = true;            TopMost = true;
            }
            public void TimerHandler(object source, System.Timers.ElapsedEventArgs e)
            {
                Close();
            }
        }
    }
      

  5.   

    这个测试代码无法在当前窗口为其他程序的时候,弹出TestMessage窗体并置顶
      

  6.   

    后来在form load的时候调用api setforegroundwindow + setwindowpos,可以了        private System.Timers.Timer m_timer = null;
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern System.IntPtr GetForegroundWindow();
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool SetForegroundWindow(IntPtr hWnd);this.Load += delegate 
                {
                    if (Handle != GetForegroundWindow())
                        SetForegroundWindow(Handle);
                    SetWindowPos(Handle, -1, 0, 0, 0, 0, 1 | 2);
                };
      

  7.   

    Form frm=new Form1();
    frm.TopMost=True;
    frm.Show();
      

  8.   

    winForm里这样:Form frm=new Form1(); 
    frm.TopMost=True; 
    frm.Show();webForm里这样:            System.Text.StringBuilder sb = new StringBuilder();
                sb.Append("<script>");
                sb.Append("window.open('BBSTieSearch.aspx"','','fullscreen =0,scrollbars=0,toolbar=0,resizable=1,top=100px,left=100px,width=800px,height=500px')");
                sb.Append("</script>");
                Page.ClientScript.RegisterStartupScript(typeof(string), "k1", sb.ToString());