반응형
C #을 사용하여 현재 활성 창의 제목을 어떻게 얻습니까?
C #을 사용하여 현재 활성 창 (즉, 포커스가있는 창)의 창 제목을 가져 오는 방법을 알고 싶습니다.
여기에서 전체 소스 코드로이를 수행하는 방법에 대한 예를 참조하십시오.
http://www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
private string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
더 나은 정확성을 위해 @Doug McClean 주석으로 편집 되었습니다.
WPF에 대해 이야기하고 있다면 다음을 사용하십시오.
Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.IsActive);
반복 Application.Current.Windows[]
해서 IsActive == true
.
Windows API를 사용하십시오. 에게 전화하십시오 GetForegroundWindow()
.
GetForegroundWindow()
hWnd
활성 창에 대한 핸들 (이름 )을 제공합니다.
문서 : GetForegroundWindow 함수 | 마이크로 소프트 문서
GetForegroundWindow 함수 기반 | Microsoft 문서 :
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);
private string GetCaptionOfActiveWindow()
{
var strTitle = string.Empty;
var handle = GetForegroundWindow();
// Obtain the length of the text
var intLength = GetWindowTextLength(handle) + 1;
var stringBuilder = new StringBuilder(intLength);
if (GetWindowText(handle, stringBuilder, intLength) > 0)
{
strTitle = stringBuilder.ToString();
}
return strTitle;
}
UTF8 문자를 지원합니다.
If it happens that you need the Current Active Form from your MDI application: (MDI- Multi Document Interface).
Form activForm;
activForm = Form.ActiveForm.ActiveMdiChild;
you can use process class it's very easy. use this namespace
using System.Diagnostics;
if you want to make a button to get active window.
private void button1_Click(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
TextBox1.Text = currentp.MainWindowTitle; //this textbox will be filled with active window.
}
반응형
'Programming' 카테고리의 다른 글
Bootstrap 3에서 반응 형 그리드를 얻는 방법은 무엇입니까? (0) | 2020.08.07 |
---|---|
ASP.NET Core에서 현재 HttpContext에 액세스 (0) | 2020.08.07 |
Firebase에서 데이터를 구조화하는 가장 좋은 방법은 무엇인가요? (0) | 2020.08.07 |
SharedPreferences에 액세스하는 것은 UI 스레드에서 수행해야합니까? (0) | 2020.08.07 |
플렉스 항목이 부모 컨테이너의 너비가 아닌 콘텐츠 너비를 갖도록합니다. (0) | 2020.08.07 |