C#

[C#]실행 파일을 최상단에 표시하기 위한 예제

선영아 사랑해 2016. 1. 28. 17:01


가끔 개발하다보면 프로그램이 항상 활성화 되어야 하는 경우가 있습니다.


아래의 예제는 타이머를 이용하여 프로그램 비활성화 되었을 경우 활성화 시키는 예제입니다.


using System.Runtime.InteropServices;


[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string StrWindowName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern void SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private const int SW_SHOWNORMAL = 1;


private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Enabled = false;

    IntPtr procHandler = FindWindow(null, this.Text);

    if (IntPtr.Zero != procHandler)
    {
        ShowWindow(procHandler, SW_SHOWNORMAL);

        SetForegroundWindow(procHandler);
    }

    timer1.Enabled = true;
}