본문 바로가기
C#.Net

외부 프로그램 실행 및 위치, 사이즈 조정

by 호야호잇 2024. 3. 5.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace UnitConnTest
{
    public partial class Form5 : Form
    {

        [DllImport("user32")]
        private static extern bool SetForegroundWindow(IntPtr handle);
        [DllImport("user32")]
        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
        [DllImport("user32")]
        public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
        [DllImport("user32.dll", SetLastError = true)]static extern bool GetWindowRect(IntPtr hWnd, ref RECT Rect);

        private const int SWP_NOSIZE = 0x0001;
        private const int SWP_NOZORDER = 0x0004;
        private const int SWP_SHOWWINDOW = 0x0040;

        int firstProgPoX;
        int firstProgPoY;
        int firstProgwidth;
        int firstProgheight;
        int secondProgPoX;
        int secondProgPoY;
        int secondprogwidth;
        int secondProgheight;
        int thirdProgPoX;
        int thirdProgPoY;
        int thirdprogwidth;
        int thirdProgheight;


       
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }


        public Form5()
        {
            InitializeComponent();

            this.Load += Form5_Load;
        }


        private void button1_Click(object sender, EventArgs e)
        {

            //외부 프로그램 실행여부 체크 후 실행
            string[] procName = { "EXCEL", "POWERPNT", "WINWORD" };

            for (int i = procName.Length - 1; i >= 0; i--)
            {

                Process[] processList = Process.GetProcessesByName(procName.ToString());

                for (int j = processList.Length - 1; j >= 0; j--)
                {
                    processList[j].Kill();
                    processList[j].Close();

                    processList[j].WaitForExit(1000);
                }

                //Windows 시스템 기본폴더 경로(Test)
                string Syspath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
                Syspath += @"\Microsoft Office\root\Office16";

                //띄울 창 3개 선정 테스트
                Process ps = new Process();
                ps.StartInfo.FileName = procName[i] + ".EXE";
                ps.StartInfo.WorkingDirectory = Syspath;
                ps.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                ps.Start();

                ps.WaitForExit(1000);

                          
                Process[] processes = Process.GetProcessesByName(procName[i]);
                foreach (Process p in processes)
                {
                    IntPtr handle = p.MainWindowHandle;
                    RECT Rect = new RECT();
                    if (GetWindowRect(handle, ref Rect))
                    {

                        // MoveWindow(handle, firstProgPoX, firstProgPoY, firstProgwidth, firstProgheight, true);

                        if (procName[i] == "WINWORD")
                        {
                            MoveWindow(handle, firstProgPoX, firstProgPoY, firstProgwidth, firstProgheight, true);
                        }
                        else if (procName[i] == "POWERPNT")
                        {
                            MoveWindow(handle, secondProgPoX, secondProgPoY, secondprogwidth, secondProgheight, true);
                        }
                        else if (procName[i] == "EXCEL")
                        {
                            MoveWindow(handle, thirdProgPoX, thirdProgPoY, thirdprogwidth, thirdProgheight, true);
                        }
                    }
                }



                //테스트 값 확인
                richTextBox1.Clear();
                richTextBox1.Text += $"핸들:{ps.Handle},{firstProgPoX}, {firstProgPoY}, {firstProgwidth}, {firstProgheight}" + Environment.NewLine;
                richTextBox1.Text += $"핸들:{ps.Handle},{secondProgPoX}, {secondProgPoY}, {secondprogwidth}, {secondProgheight}" + Environment.NewLine;
                richTextBox1.Text += $"핸들:{ps.Handle},{thirdProgPoX}, {thirdProgPoY}, {thirdprogwidth}, {thirdProgheight}" + Environment.NewLine;
            }
                        
        }


        private void button2_Click(object sender, EventArgs e)
        {

            richTextBox1.Clear();

            Screen[] sc = Screen.AllScreens;
            int width_size = sc[0].WorkingArea.Width;
            int height_size = sc[0].WorkingArea.Height;

            int width = 0;
            sc.ToList().ForEach(a => width += a.WorkingArea.Width);



            richTextBox1.Text = $"가로 크기: {width_size.ToString() + Environment.NewLine}";
            richTextBox1.Text += $"세로 크기: {height_size.ToString() + Environment.NewLine}";
            richTextBox1.Text += $"가로 가운데 위치: {(width_size / 2).ToString() + Environment.NewLine}";
            richTextBox1.Text += $"세로 가운데 위치: {(height_size / 2).ToString() + Environment.NewLine}";

            firstProgPoX = 0;
            firstProgPoY = 0;
            firstProgwidth = width_size / 2;
            firstProgheight = height_size / 2;

            secondProgPoX = width_size / 2 + 1;
            secondProgPoY = 0;
            secondprogwidth = width_size / 2;
            secondProgheight = height_size / 2;

            thirdProgPoX = 0;
            thirdProgPoY = height_size / 2 + 1;
            thirdprogwidth = width_size;
            thirdProgheight = height_size / 2;


            richTextBox1.Text += $"{firstProgPoX}, {firstProgPoY}, {firstProgwidth}, {firstProgheight}" + Environment.NewLine;
            richTextBox1.Text += $"{secondProgPoX}, {secondProgPoY}, {secondprogwidth}, {secondProgheight}" + Environment.NewLine;
            richTextBox1.Text += $"{thirdProgPoX}, {thirdProgPoY}, {thirdprogwidth}, {thirdProgheight}" + Environment.NewLine;

        }


        private void ScreenDetect()
        {
            Screen[] sc = Screen.AllScreens;

            if (sc.Length > 1)
            {
                Screen screen = (sc[0].WorkingArea.Contains(this.Location)) ? sc[0] : sc[1];

                richTextBox1.Text += screen.ToString() + Environment.NewLine + Environment.NewLine;
            }
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            ScreenDetect();
        }
    

        }
    }
}