在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → <亲测>C# 将其它应用程序设置为当前窗口(可以是qq/微信/钉钉等)

<亲测>C# 将其它应用程序设置为当前窗口(可以是qq/微信/钉钉等)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.19M
  • 下载次数:38
  • 浏览次数:922
  • 发布时间:2018-08-25
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: QQ C# 微信 窗口

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

using HWND = System.IntPtr;

namespace SetWindowPosition
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            preloadProcesses();
        }

        // Define the FindWindow API function.
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

        // Define the SetWindowPos API function.
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);

        [DllImport("USER32.DLL")]
        private static extern IntPtr GetShellWindow();

        private delegate bool EnumWindowsProc(HWND hWnd, int lParam);

        [DllImport("USER32.DLL")]
        private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);

        [DllImport("USER32.DLL")]
        private static extern bool IsWindowVisible(HWND hWnd);

        [DllImport("USER32.DLL")]
        private static extern int GetWindowTextLength(HWND hWnd);

        [DllImport("USER32.DLL")]
        private static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32")]
        private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);

        public static Int32 GetWindowProcessID(Int32 hwnd) {
            Int32 pid = 1;
            GetWindowThreadProcessId(hwnd, out pid);
            return pid;
        }

        // Define the SetWindowPosFlags enumeration.
        [Flags()]
        private enum SetWindowPosFlags : uint
        {
            SynchronousWindowPosition = 0x4000,
            DeferErase = 0x2000,
            DrawFrame = 0x0020,
            FrameChanged = 0x0020,
            HideWindow = 0x0080,
            DoNotActivate = 0x0010,
            DoNotCopyBits = 0x0100,
            IgnoreMove = 0x0002,
            DoNotChangeOwnerZOrder = 0x0200,
            DoNotRedraw = 0x0008,
            DoNotReposition = 0x0200,
            DoNotSendChangingEvent = 0x0400,
            IgnoreResize = 0x0001,
            IgnoreZOrder = 0x0004,
            ShowWindow = 0x0040,
        }

        /// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary>
        /// <returns>A dictionary that contains the handle and title of all the open windows.</returns>
        public static HashSet<WindowInfo> GetOpenWindows() {
            HWND shellWindow = GetShellWindow();
            
            HashSet<WindowInfo> windows = new HashSet<WindowInfo>();

            EnumWindows(delegate (HWND hWnd, int lParam)
            {
                if (hWnd == shellWindow) return true;
                if (!IsWindowVisible(hWnd)) return true;

                int length = GetWindowTextLength(hWnd);
                if (length == 0) return true;

                StringBuilder builder = new StringBuilder(length);
                GetWindowText(hWnd, builder, length   1);

                Int32 pid = GetWindowProcessID(hWnd.ToInt32());
                Process p = Process.GetProcessById(pid);
                string appName = p.ProcessName;

                WindowInfo window = new WindowInfo(hWnd, appName, builder.ToString());

                windows.Add(window);
                return true;

            }, 0);

            return windows;
        }

        private void preloadProcesses() {
            HashSet<WindowInfo> allWindows = GetOpenWindows();
            foreach (WindowInfo windowData in allWindows) {
                comboBox1.Items.Add(windowData);
            }
        }

        // Size and position the application.
        private void btnSet_Click(object sender, EventArgs e)
        {

            // Get the target window's handle.
            IntPtr target_hwnd = //FindWindowByCaption(IntPtr.Zero, txtAppTitle.Text);
                ((WindowInfo) comboBox1.SelectedItem).hWnd;
            if (target_hwnd == IntPtr.Zero)
            {
                MessageBox.Show(
                    "Could not find a window with the title \""  
                    ((WindowInfo) comboBox1.SelectedItem).title  "\"");
                return;
            }
            

            // Set the window's position.
            int width = int.Parse(txtWidth.Text);
            int height = int.Parse(txtHeight.Text);
            int x = int.Parse(txtX.Text);
            int y = int.Parse(txtY.Text);
            SetWindowPos(target_hwnd, IntPtr.Zero, x, y, width, height, 0);
        }

        
    }
}

标签: QQ C# 微信 窗口

实例下载地址

<亲测>C# 将其它应用程序设置为当前窗口(可以是qq/微信/钉钉等)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警