반응형

* C# API 윈도우 창 이름으로 윈도우 창 찾기 예제... (Window Form Search)

 

메인화면

 

전체 소스 코드

Form1.cs

 

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;

using System.Runtime.InteropServices;

namespace CSharp_API윈도우창_찾기
{
    public partial class Form1 : Form
    {

        [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_WINDOW_NORMAL = 1;
        private const int SW_WINDOW_MINIMIZED = 2;
        private const int SW_WINDOW_MAXIMIZED = 3;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 윈도우 타이틀명으로 핸들을 찾음.
            IntPtr hWnd = FindWindow(null, textBox1.Text);

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

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

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            //텍스트 박스에 Enter 키 입력이 들어 오면
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click(null, null);
            }
        }
    }
}

 

 

* 예제 결과

 

반응형

+ Recent posts