728x90
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PopupFront
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            Run();
        }

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

        private const int SW_SHOWNORMAL = 1;
        private const int SW_SHOWMINIMIZED = 2;
        private const int SW_SHOWMAXIMIZED = 3;

        async void Run()
        {
            //10초뒤 재실행
            await Task.Delay(1000 * 10);
            //MessageBox.Show(new Form { TopMost = true }, "테스트"); //항상 최상위로 나옴
            //this.BringToFront();

            // 윈도우 타이틀명으로 핸들을 찾는다
            //IntPtr hWnd = FindWindow(null, "Form1");
            IntPtr hWnd = FindWindow(null, this.Text);

            if (!hWnd.Equals(IntPtr.Zero))
            {
                // 윈도우가 최소화 되어 있다면 활성화 시킨다
                //ShowWindowAsync(hWnd, SW_SHOWNORMAL);

                // 윈도우에 포커스를 줘서 최상위로 만든다
                SetForegroundWindow(hWnd);
            }

            MessageBox.Show(this, "테스트"); //항상 최상위로 나옴
        }
    }
}

다른 프로그램들을 보도 있더라도 

10초뒤에 최상위에 팝업이 나오는 코드입니다.

 

* 테스트 하다보니 최상위 팜업 안되는 경우가 있는데 'TopMost = True' 설정 하면 됩니다!

 

참초: https://okky.kr/article/927279

https://gent.tistory.com/97

728x90

+ Recent posts