17 Aralık 2024 Yazarı yhackup 0

The Other App Maximize , Minimize and ForeGround Example

Merhabalar,

c# kanadında  , tarayıcıda açık olan whatsapp’ı simge durumundan kaldırmam veya eğer simge durumunda değilse ekranda açık olan pencerelerin tümünün önüne getirmem gerekiyordu, kendime basit bir sınıf yazarak hallettim sizlere de yararı olabileceğini düşünerek buraya ekliyorum.

 

new MyWindowsAPI().doWindow("WhatsApp - Google Chrome");

doWindow metodunu kullanırken Pencerenenin tam adını vermeniz gerekir “WhatsApp – Google Chrome” gibi.

 

doMaxWindowLike ve doMinWindowLike metodarında ise Pencere içerisinde geçen kelimeyi verebilirsiniz  sadece “WhatsApp” yazmanız yeterli , ilk bulduğuna pencereyi yakalayacaktır.

Saygılar.

new MyWindowsAPI().doMaxWindowLike("WhatsApp");

new MyWindowsAPI().doMinWindowLike("WhatsApp");

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;

namespace PWA
{
    class MyWindowsAPI
    {
        private string iCaption;
        IntPtr xHandle = IntPtr.Zero;
        private readonly int SW_RESTORE = 9;
        private readonly int SW_MINIMIZE = 6;
        private readonly int SW_MAXIMIZE = 3;

        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
        public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);

        [DllImport("user32.dll", EntryPoint = "AttachThreadInput")]
        public static extern bool AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, bool fAttach);

        [DllImport("Kernel32.dll", EntryPoint = "GetCurrentThreadId")]
        public static extern IntPtr GetCurrentThreadId();

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll", EntryPoint = "IsIconic")]
        public static extern bool IsIconic(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [ResourceExposure(ResourceScope.Process)]
        public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);

        internal delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "EnumWindowsProc")]
        public static extern bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);


        [DllImport("user32.dll", CharSet = CharSet.Unicode, BestFitMapping = true)]
        [ResourceExposure(ResourceScope.None)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        bool MyEnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
        {
            StringBuilder stringBuilder = new StringBuilder(256);
            GetWindowText(handle, stringBuilder, stringBuilder.Capacity);
            string ss = stringBuilder.ToString();
            if (!string.IsNullOrEmpty(ss))
            {
                if (ss.ToLower().Contains(iCaption.ToLower()))
                {
                    xHandle = handle;
                    return false;
                }
            }
            return true;
        }

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        public void doMaxWindowLike(string caption)
        {
            if (string.IsNullOrEmpty(caption))            
                throw new Exception("Caption cannot be null or empty..!");
            iCaption = caption;
            doWindowLike();
        }

        public void doMinWindowLike(string caption)
        {
            if (string.IsNullOrEmpty(caption))
                throw new Exception("Caption cannot be null or empty..!");
            iCaption = caption;
            doWindowLike(true);
        }

        public bool IsFind(string Caption)
        {
            IntPtr AHandle = FindWindow(null, Caption);
            return AHandle.ToInt32() != 0;
        }



        // private Methods //
        private void maximize(IntPtr hWnd)
        {
            ShowWindow(xHandle, SW_RESTORE);
            ShowWindow(xHandle, SW_MAXIMIZE);
        }
        private void minimize(IntPtr hWnd)
        {
            ShowWindow(xHandle, SW_RESTORE);
            ShowWindow(xHandle, SW_MAXIMIZE);
        }

        // Caption İçeren
        private void doWindowLike(bool minimized = false)
        {
            xHandle = IntPtr.Zero;
            EnumWindows(MyEnumWindowsCallback, IntPtr.Zero);
            if (xHandle != IntPtr.Zero)
            {
                var AThreadID = GetWindowThreadProcessId(xHandle, IntPtr.Zero);
                AttachThreadInput(GetCurrentThreadId(), AThreadID, true);
                try
                {
                    if (minimized)
                    {
                        minimize(xHandle);
                    }
                    else
                    {
                        if (IsIconic(xHandle))
                            maximize(xHandle);
                        SetForegroundWindow(xHandle);
                    }
                }
                finally
                {
                    AttachThreadInput(GetCurrentThreadId(), AThreadID, false);
                }
            }
        }

        // Tam eşleşme
        public void doWindow(string Caption , bool minimized = false)
        {
            IntPtr AHandle = FindWindow(null, Caption);
            if (AHandle.ToInt32() != 0)
            {
                var AThreadID = GetWindowThreadProcessId(AHandle, IntPtr.Zero);
                AttachThreadInput(GetCurrentThreadId(), AThreadID, true);
                try
                {
                    if (minimized)
                    {
                        minimize(xHandle);
                    }
                    else
                    {
                        if (IsIconic(xHandle))
                            maximize(xHandle);
                        SetForegroundWindow(xHandle);
                    }
                }
                finally
                {
                    AttachThreadInput(GetCurrentThreadId(), AThreadID, false);
                }
            }
        }
    }
}

Yürüyen Windows Api Kütüphanesi Tuğrul Abim’e teşekkürler 🙂