Además de la publicación de "Jan Goyvaerts": Uso SendMessage en lugar de PostMessage. La diferencia es que SendMessage espera que el comando sea captado por la ventana. Lo que significa que en los retornos SendMessages, sabes que el diálogo del tema está cerrado.
Así que si comienzas con el método monstruoso (pero genial) rundll32.exe sugerido por "Campbell". Debes esperar un segundo antes de enviar WM_CLOSE. De lo contrario, el tema no se establecerá y la aplicación se cerrará de inmediato.
El siguiente fragmento de código extrae un archivo del recurso (un paquete de temas). A continuación, ejecuta desk.cpl con rundll32.exe, espera 3 segundos, luego envía WM_CLOSE (0x0010), espera a que se procese el comando (el tiempo que tarda el tema en configurarse).
private Boolean SwitchToClassicTheme()
{
//First unpack the theme
try
{
//Extract the theme from the resource
String ThemePath = System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Resources\Themes\ClassicTheme.themepack";
//WriteFileToCurrentDirectory("ClassicTheme.theme", TabletConfigurator.Resources.ClassicTheme);
if(File.Exists(ThemePath))
{
File.Delete(ThemePath);
}
if(File.Exists(ThemePath))
{
throw new Exception("The file '" + ThemePath + "' exists and can not be deleted. You can try to delete it manually.");
}
using (BinaryWriter sw = new BinaryWriter(new FileStream(ThemePath, FileMode.OpenOrCreate)))
{
sw.Write(TabletConfigurator.Resources.ClassicTheme);
sw.Flush();
sw.Close();
}
if(!File.Exists(ThemePath))
{
throw new Exception("The resource theme file could not be extracted");
}
//Set the theme file as like a user would have clicked it
Boolean bTimedOut = false;
String ThemeOutput = StartProcessAndWait("rundll32.exe", System.Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\shell32.dll,Control_RunDLL " + System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\desk.cpl desk,@Themes /Action:OpenTheme /file:\"" + ThemePath + "\"", ref bTimedOut);
System.Threading.Thread.Sleep(3000);
//Wait for the theme to be set
IntPtr hWndTheming = FindWindow("CabinetWClass", null);
SendMessage(hWndTheming, (uint)WM_CLOSE, 0, 0);
//using (Bitmap bm = CaptureScreenShot())
//{
// Boolean PixelIsGray = true;
// while (PixelIsGray)
// {
// System.Drawing.Color pixel = bm.GetPixel(0, 0)
// }
//}
}
catch(Exception ex)
{
ShowError("An exception occured while setting the theme: " + ex.Message);
return false;
}
return true;
}
enfermo ... ¿Cómo te diste cuenta? – Claudiu
imma da esto una recompensa ya que es la respuesta real – Claudiu
Supongo que fue elaborado usando ProcessMonitor al cambiar el tema manualmente. –