Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

C# Question of the Day

Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
So I have embedded another exe within my C# coding inside a groupbox so that the buttons would still remain visible but, I would like to know how I can make the embedded window active when I click on any of my buttons. Ex.(Send Keystrokes to the NOTEPAD inside of the groupbox.)

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //required for APIs
using System.Threading;
using System.Diagnostics;

private void button1_Click(object sender, EventArgs e)
        {
             //Need to bring the focus of the "notepad.exe" below here and use send keys
             SendKeys.Send("{ENTER}");
        }
private void button2_Click(object sender, EventArgs e)
        {
            this.Size = new Size(1280, 900);
            Process p = Process.Start("notepad.exe");
            Thread.Sleep(500);
            p.WaitForInputIdle();
            SetParent(p.MainWindowHandle, groupBox1.Handle);
        }
Any help here would be much appreciated. Thank you.
 
Newbie Spellweaver
Joined
May 28, 2016
Messages
10
Reaction score
7
I'm a C/C++ Programmer, but i hope i can throw 'n right answer!

Use ShowWindowAsync( p.MainWindowHandle, 1 );
For the second parameter of ShowWindowAsync please see: .

If there is any problem with the position of the window you wanna display, use the
Code:
[COLOR=#2b91af]MoveWindow[/COLOR][COLOR=#303336]( HWND hWnd[/COLOR][COLOR=#2b91af], int x, int y, in nWidth, int nHeight, bool bRepaint [/COLOR][COLOR=#303336]);[/COLOR]
function above the ShowWindowAsync( p.MainWindowHandle, 1 ); call.



To get those functions running you've to import/add following:
Code:
[COLOR=#303336][[/COLOR][COLOR=#2B91AF]DllImport[/COLOR][COLOR=#303336]( [/COLOR][COLOR=#7D2727]"user32.dll" [/COLOR][COLOR=#303336])]
[/COLOR][COLOR=#101094]private [/COLOR][COLOR=#101094]static [/COLOR][COLOR=#101094]extern [/COLOR][COLOR=#101094]bool [/COLOR][COLOR=#2B91AF]ShowWindowAsync[/COLOR][COLOR=#303336]( [/COLOR][COLOR=#2B91AF]IntPtr[/COLOR][COLOR=#303336] hwnd[/COLOR][COLOR=#303336],[/COLOR][COLOR=#2B91AF]int[/COLOR][COLOR=#303336] nCmdShow [/COLOR][COLOR=#303336]);[/COLOR]

Hope it was right!
 
Last edited:
Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
I am quite confused. LOL. when I apply the coding as you show there, I get the error, p cannot be found the current context. I assume this is because it is static but, again unclear as what to do because of this issue. BTW, thank you for your attempts to help. It is greatly appreciated.
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
If p is static, access it through (classname).p. For example, if your class name is "Main" then access the "p" static by "Main.p".
 
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
Looks to me like p is his Process variable as shown in his code snippet:

Code:
[...]
private void button2_Click(object sender, EventArgs e)
        {
            this.Size = new Size(1280, 900);
            [B][COLOR="#FF0000"]Process p = Process.Start("notepad.exe");[/COLOR][/B]
            Thread.Sleep(500);
            p.WaitForInputIdle();
            SetParent(p.MainWindowHandle, groupBox1.Handle);
        }
[...]

That's also why I can't really reason his error saying it can't be found in the current context :s
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
Looks to me like p is his Process variable as shown in his code snippet:



That's also why I can't really reason his error saying it can't be found in the current context :s

Yeah I noticed but this was my only guess based on his post.

Anyway, @OP, this should work:

PHP:
[DllImport ("User32.dll")] static extern int SetForegroundWindow(IntPtr point);


Process p = Process.Start("notepad.exe");
p.WaitForInputIdle();
IntPtr h = p.MainWindowHandle;
SetForegroundWindow(h);
SendKeys.SendWait("k");

Source:
 
Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
Process p = Process.Start("notepad.exe");
This creates the issue as you are trying to show Solaire. I want to open the program embedded into the C# application with one button but, also to allow use of another button that will bring the focus of the embedded exe that will send a keystroke. By use of the "process.start" it will open a new process. I don't want to do this. I hope that you understand. My ultimate reasoning for this is to window a game that lacks borders and alter the config file to change the graphics size as well before the exe is opened. This will in turn be used to create a auto key presser and unify multiple theories into one tool.
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
This creates the issue as you are trying to show Solaire. I want to open the program embedded into the C# application with one button but, also to allow use of another button that will bring the focus of the embedded exe that will send a keystroke. By use of the "process.start" it will open a new process. I don't want to do this. I hope that you understand. My ultimate reasoning for this is to window a game that lacks borders and alter the config file to change the graphics size as well before the exe is opened. This will in turn be used to create a auto key presser and unify multiple theories into one tool.

Ah that's different than what I had in mind.

I wrote you a sample code, button2 has the implementation you want, button1 has the implementation I mentioned before. If you want button2 to always make use of the process that was started by button1 then you could save the main window handle to a class variable. Otherwise, GetProcessesByName should do the trick.

PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Temp
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// The DLL import used to focus the window
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        static extern int SetForegroundWindow(IntPtr point);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Start process
            Process process = Process.Start("notepad.exe");

            // Wait until the process started
            process.WaitForInputIdle();
            
            // Grab window handle and set foreground to that window
            SetForegroundWindow(process.MainWindowHandle);

            // Send key
            SendKeys.SendWait("k");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Find notepad
            // NOTE: no .exe here
            Process[] processes = Process.GetProcessesByName("notepad");

            // Check if any process was found
            if (processes.Length == 0)
            {
                return;
            }

            // Set foreground window
            SetForegroundWindow(processes[0].MainWindowHandle);

            // Send key
            SendKeys.SendWait("o");
        }
    }
}
 
Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
I have already attempted to use similar coding but, it doesn't focus on the exe. Can you give a small example of what you mean "save the main window handle to a class variable"? Thank you
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
I have already attempted to use similar coding but, it doesn't focus on the exe. Can you give a small example of what you mean "save the main window handle to a class variable"? Thank you

What OS are you using? I've tested the code on Windows 10 and it worked fine.
 
Custom Title Activated
Loyal Member
Joined
Mar 26, 2012
Messages
1,465
Reaction score
131
I am using Windows 7 Ultimate 64 bit and maybe I am doing something wrong but, it is not focusing on the exe embedded inside of the C# application. When I click on the button to perform the action, nothing happens.

Here is my button to focus on the embedded exe.
PHP:
private void button3_Click(object sender, EventArgs e)
        {
             // Find HTLauncher.exe which is the actual game file
            // NOTE: no .exe here
            Process[] processes = Process.GetProcessesByName("HTLauncher");

            // Check if any process was found
            if (processes.Length == 0)
            {
                return;
            }
            SetForegroundWindow(processes[0].MainWindowHandle);
            System.Threading.Thread.Sleep(500);
            // Send key F1 is the key set to make my character dance as an example
            SendKeys.Send("{F1}");
}
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
I am using Windows 7 Ultimate 64 bit and maybe I am doing something wrong but, it is not focusing on the exe embedded inside of the C# application. When I click on the button to perform the action, nothing happens.

Here is my button to focus on the embedded exe.
PHP:
private void button3_Click(object sender, EventArgs e)
        {
             // Find HTLauncher.exe which is the actual game file
            // NOTE: no .exe here
            Process[] processes = Process.GetProcessesByName("HTLauncher");

            // Check if any process was found
            if (processes.Length == 0)
            {
                return;
            }
            SetForegroundWindow(processes[0].MainWindowHandle);
            System.Threading.Thread.Sleep(500);
            // Send key F1 is the key set to make my character dance as an example
            SendKeys.Send("{F1}");
}

Does it work on notepad though? Because it if does, then it might have something to do with your application.
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
There were issues reported with that WINAPI function using Windows 7. Could you try a different OS or computer?
 
Back
Top