溫馨提示×

c# findwindowex錯誤處理

c#
小樊
99
2024-11-21 15:11:32
欄目: 編程語言

在C#中,FindWindowEx函數用于查找窗口句柄。為了處理可能的錯誤,你可以使用try-catch語句來捕獲異常。以下是一個示例:

using System;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("user32.dll")]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    static void Main()
    {
        try
        {
            // 替換以下字符串為你要查找的窗口類名和窗口標題
            string className = "ClassName";
            string windowTitle = "WindowTitle";

            IntPtr hwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, className, windowTitle);

            if (hwnd == IntPtr.Zero)
            {
                Console.WriteLine("未找到窗口");
            }
            else
            {
                Console.WriteLine($"找到窗口,句柄為: {hwnd}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"發生錯誤: {ex.Message}");
        }
    }
}

在這個示例中,我們使用try-catch語句來捕獲FindWindowEx函數可能引發的異常。如果發生異常,我們將在控制臺上顯示錯誤消息。這樣可以確保程序在遇到問題時不會崩潰,并且可以提供有關錯誤的詳細信息。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女