Exemplo n.º 1
0
        /// <summary>
        /// 获取与此控件关联的文本。
        /// </summary>
        /// <param name="hWnd">控件句柄。</param>
        /// <returns>与该控件关联的文本。</returns>
        public static string GetText(IntPtr hWnd)
        {
            if (!GetIsHandleCreated(hWnd))
            {
                return(string.Empty);
            }

            int windowTextLength = UnsafeNativeMethods.GetWindowTextLength(hWnd);

            if (UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_DBCSENABLED) != 0)
            {
                windowTextLength = (windowTextLength * 2) + 1;
            }
            StringBuilder lpString = new StringBuilder(windowTextLength + 1);

            UnsafeNativeMethods.GetWindowText(hWnd, lpString, lpString.Capacity);
            return(lpString.ToString());
        }