1

I have written a program that change the windows theme but after changing the theme personalization window remains open and I want to close it. I tried using process.kill() with familiar process name but it didn't work. Thank you.

The code for what I am doing is as below:

ProcessStartInfo theinfo = new ProcessStartInfo(themepath + "aero.theme");
theinfo.CreateNoWindow = true;
Process thepr = new Process();
thepr.StartInfo = theinfo;
thepr.Start();

where "themepath" is String location to aero.theme. I have even enabled CreateNoWindow to true then also it opens up Personalization to change theme but didn't close it automatically.

Aryan
  • 41
  • 5

2 Answers2

2

First use find window to get the window from their name by Using FindWindow..

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

It returns you the handle of the window you want now you can use send message to close it..

 [DllImport("User32.dll")]
    public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
    public const int WM_SYSCOMMAND = 0x0112;
    public const int SC_CLOSE = 0xF060;
        private void closeWindow()
         {
          // retrieve the handler of the window  
            int iHandle = FindWindow("CabinetWClass", "Personalization");
            if (iHandle > 0)
             {
                 SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);  
             }  
          }   
Anoop Mishra
  • 1,005
  • 2
  • 10
  • 33
0

You need to obtain the window handle by it's name and then send it a close message. This prevents having to kill any processes. See this article for information on obtaining the windows. See this one for closing windows from the handle.

After seeing the code and doing a little digging, you can accomplish this with two registry edits. You should read this article and just have your program edit the two registry keys in question.

Community
  • 1
  • 1
The Sharp Ninja
  • 1,041
  • 9
  • 18
  • I tried this but it is also not working. I am also little confused with the second article link you gave for closing windows from handle. BTW thanks for helping. – Aryan Dec 05 '15 at 12:45
  • Now that I see your code I see that you have no window to close. Since you are launching from the file association, and not a specific executable, your process is probably going to be explorer.exe which Windows is protecting you from killing. You should probably make your theme change via the registry and not try to automate Windows Explorer. – The Sharp Ninja Dec 05 '15 at 13:00