반응형
* 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);
}
}
}
}
* 예제 결과
반응형
'C# Programming' 카테고리의 다른 글
[C#] 시간 체크 (Time Check) (0) | 2019.12.27 |
---|---|
[C#] PC 비프음 (Beep) (0) | 2019.12.27 |
[C#] [API] 다른 응용 프로그램 실행 시키기 : ShellExecute () (0) | 2019.12.18 |
[C#] Params 키워드를 이용한 가변 전달 인자 예제 (0) | 2019.12.13 |
[C#] Json Read 를 이용한 로또(Lotto) 당첨 번호 읽기 예제 (0) | 2019.12.09 |