Ejemplo n.º 1
0
		public static void Show()
		{
			var hWndTray = NativeMethods.FindWindow(TrayWndClassName, string.Empty);
			if (hWndTray == IntPtr.Zero)
			{
				throw new Win32Exception();
			}

			var hWndTrayNotify = NativeMethods.FindWindowEx(hWndTray, IntPtr.Zero, TrayNotifyWndClassName, string.Empty);
			if (hWndTrayNotify == IntPtr.Zero)
			{
				throw new Win32Exception();
			}

			// search clock window
			var cb = new NativeMethods.EnumChildCallback((IntPtr hWndChild, ref IntPtr lParam) =>
			{
				var className = new StringBuilder(128);
				NativeMethods.GetClassName(hWndChild, className, 128);

				if (className.ToString() != ClockWndClassName) return true;

				lParam = hWndChild;
				return false;
			});

			var hWndClock = IntPtr.Zero;
			NativeMethods.EnumChildWindows(hWndTray, cb, ref hWndClock);
			if (hWndClock == IntPtr.Zero)
			{
				throw new Win32Exception();
			}

			// get clock window position
			Rect rect;
			if (!NativeMethods.GetWindowRect(hWndClock, out rect))
			{
				throw new Win32Exception();
			}

			// send click, lParam contains window position
			//IntPtr wParam = new IntPtr(HTCAPTION);
			//IntPtr lParam = new IntPtr(rect.Top << 16 | rect.Left);
			//var ret = SendMessage(hWndTray, WM_LBUTTONDOWN, wParam, lParam);
			//SendMessage(hWndTray, WM_LBUTTONUP, wParam, lParam);

			var current = new Point();
			NativeMethods.GetCursorPos(ref current);
			NativeMethods.SetCursorPos(rect.Left + 10, rect.Top + 10);
			NativeMethods.mouse_event(MouseEventFlag.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
			NativeMethods.mouse_event(MouseEventFlag.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
			NativeMethods.SetCursorPos(current.X, current.Y);

			//Console.ReadKey();
		}
Ejemplo n.º 2
0
        public static void GetObjectByWindow()
        {
            // Use the window class name ("OpusApp") to retrieve a handle to Word's main window.
            // Alternatively you can get the window handle via the process id:
            // int hwnd = (int)Process.GetProcessById(wordPid).MainWindowHandle;
            //
            int hwnd = (int)NativeMethods.FindWindow("OpusApp", null);

            if (hwnd != 0)
            {
                int hwndChild = 0;

                // Search the accessible child window (it has class name "_WwG")
                // as described in http://msdn.microsoft.com/en-us/library/dd317978%28VS.85%29.aspx
                //
                NativeMethods.EnumChildCallback cb = new NativeMethods.EnumChildCallback(NativeMethods.EnumChildProc);
                NativeMethods.EnumChildWindows(hwnd, cb, ref hwndChild);

                if (hwndChild != 0)
                {
                    // We call AccessibleObjectFromWindow, passing the constant OBJID_NATIVEOM (defined in winuser.h)
                    // and IID_IDispatch - we want an IDispatch pointer into the native object model.
                    //
                    const uint OBJID_NATIVEOM = 0xFFFFFFF0;
                    Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
                    IDispatch ptr;

                    int hr = NativeMethods.AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out ptr);

                    if (hr >= 0)
                    {
                        object wordApp = ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null);

                        object version = wordApp.GetType().InvokeMember("Version", BindingFlags.GetField | BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, wordApp, null);
                        Console.WriteLine(string.Format("Word version is: {0}", version));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static void Show()
        {
            var hWndTray = NativeMethods.FindWindow(TrayWndClassName, string.Empty);

            if (hWndTray == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            var hWndTrayNotify = NativeMethods.FindWindowEx(hWndTray, IntPtr.Zero, TrayNotifyWndClassName, string.Empty);

            if (hWndTrayNotify == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            // search clock window
            var cb = new NativeMethods.EnumChildCallback((IntPtr hWndChild, ref IntPtr lParam) =>
            {
                var className = new StringBuilder(128);
                NativeMethods.GetClassName(hWndChild, className, 128);

                if (className.ToString() != ClockWndClassName)
                {
                    return(true);
                }

                lParam = hWndChild;
                return(false);
            });

            var hWndClock = IntPtr.Zero;

            NativeMethods.EnumChildWindows(hWndTray, cb, ref hWndClock);
            if (hWndClock == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            // get clock window position
            Rect rect;

            if (!NativeMethods.GetWindowRect(hWndClock, out rect))
            {
                throw new Win32Exception();
            }

            // send click, lParam contains window position
            //IntPtr wParam = new IntPtr(HTCAPTION);
            //IntPtr lParam = new IntPtr(rect.Top << 16 | rect.Left);
            //var ret = SendMessage(hWndTray, WM_LBUTTONDOWN, wParam, lParam);
            //SendMessage(hWndTray, WM_LBUTTONUP, wParam, lParam);

            var current = new Point();

            NativeMethods.GetCursorPos(ref current);
            NativeMethods.SetCursorPos(rect.Left + 10, rect.Top + 10);
            NativeMethods.mouse_event(MouseEventFlag.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            NativeMethods.mouse_event(MouseEventFlag.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            NativeMethods.SetCursorPos(current.X, current.Y);

            //Console.ReadKey();
        }