C
C#4mo ago
Silver Black

How to distribute VisualStyles for dialogs Vista style - FIXED!

I created a self-contained package and it works. Problem is that in my project C# WinForms, I use System.Windows.Forms.VisualStyles to have dialogs in Vista style, but the sytle is not applied as if I used normal dialogs. What should I distribute to make it work? Note that the software works as expected if executed from Debug folder or from the IDE itself, but not from the Release folder after the self-contained packags is created.
12 Replies
reflectronic
reflectronic4mo ago
how do you use System.Windows.Forms.VisualStyles exactly
Silver Black
Silver BlackOP4mo ago
@reflectronic
public static bool EnMsgBoxYesNo(in string strMsg, in string strDescription = ClsStrings_ITA.STR_DLG_QUESTION, in string strTitle = ClsConstants.STR_PROJ_TITLE)
{
try
{
// Initialization:
Microsoft.WindowsAPICodePack.Dialogs.TaskDialog dialog = new()
{
Caption = strTitle,
InstructionText = strDescription,
Text = strMsg,
StandardButtons = TaskDialogStandardButtons.Yes | TaskDialogStandardButtons.No,
Icon = TaskDialogStandardIcon.Warning
};
// Show dialog:
TaskDialogResult result = dialog.Show();
// Check user input:
if (result == TaskDialogResult.Yes)
{
return true; // yes
}
else
{
return false; // no
}
}
catch (Exception)
{
return MsgBoxYesNo(strMsg, strTitle); // standard dialog
}
}
public static bool EnMsgBoxYesNo(in string strMsg, in string strDescription = ClsStrings_ITA.STR_DLG_QUESTION, in string strTitle = ClsConstants.STR_PROJ_TITLE)
{
try
{
// Initialization:
Microsoft.WindowsAPICodePack.Dialogs.TaskDialog dialog = new()
{
Caption = strTitle,
InstructionText = strDescription,
Text = strMsg,
StandardButtons = TaskDialogStandardButtons.Yes | TaskDialogStandardButtons.No,
Icon = TaskDialogStandardIcon.Warning
};
// Show dialog:
TaskDialogResult result = dialog.Show();
// Check user input:
if (result == TaskDialogResult.Yes)
{
return true; // yes
}
else
{
return false; // no
}
}
catch (Exception)
{
return MsgBoxYesNo(strMsg, strTitle); // standard dialog
}
}
reflectronic
reflectronic4mo ago
you're using .NET Framework, right? like .NET 4.x
jcotton42
jcotton424mo ago
$code is how you do that
MODiX
MODiX4mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
jcotton42
jcotton424mo ago
WindowsAPICodePack-Core 1.1.2
Core library for Windows API Code Pack
jcotton42
jcotton424mo ago
GitHub
GitHub - aybe/Windows-API-Code-Pack-1.1: Windows API Code Pack 1.1
Windows API Code Pack 1.1. Contribute to aybe/Windows-API-Code-Pack-1.1 development by creating an account on GitHub.
Silver Black
Silver BlackOP4mo ago
My target framework is ".NET 8.0". The weird thing is that if I run the executable from the "bin\Release\net8.0-windows\publish\self-contained" folder I have the glitch, while from the "bin\Debug\net8.0-windows" it runs ok, so also on the same development machine!
reflectronic
reflectronic4mo ago
if you are using .NET 8 i would suggest not using Microsoft.WindowsAPICodePack
reflectronic
reflectronic4mo ago
TaskDialog Class (System.Windows.Forms)
A task dialog allows to display information and get simple input from the user. It is similar to a MessageBox (in that it is formatted by the operating system) but provides a lot more features.
reflectronic
reflectronic4mo ago
i don't know exactly why you are seeing different behavior but you need to enable visual styles to use the task dialog, and you can do it either through calling Application.EnableVisualStyles() , or adding an app manifest with the reference to the right common controls library. if it's not working in your release build, then those things aren't happening for whatever reason and that needs to be debugged
Silver Black
Silver BlackOP4mo ago
@reflectronic Main main method is:
internal static class Program
{
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
internal static class Program
{
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
I also have a manifest file called <executable-file-name>.exe.manifest in the same folder of the executable itself: ...\bin\Release\net8.0-windows\publish\self-contained. The error message I receive in the exception is: Value cannot be null. (Parameter 'path1') There is no parmeter "path1" in my code:
public static bool EnMsgBoxYesNo(in string strMsg, in string strDescription = ClsStrings_ITA.STR_DLG_QUESTION, in string strTitle = ClsConstants.STR_PROJ_TITLE)
{
try
{
// Initialization:
Microsoft.WindowsAPICodePack.Dialogs.TaskDialog dialog = new()
{
Caption = strTitle,
InstructionText = strDescription,
Text = strMsg,
StandardButtons = TaskDialogStandardButtons.Yes | TaskDialogStandardButtons.No,
Icon = TaskDialogStandardIcon.Warning /* non trovo l'icona del punto interrogativo!!! :( */
};
// Show dialog:
TaskDialogResult result = dialog.Show();
// Check user input:
if (result == TaskDialogResult.Yes)
{
return true; // yes
}
else
{
return false; // no
}
}
catch (Exception ex)
{
MsgErrorHandler(ex.Message, ClsStrings_ITA.STR_ERR_MSGBOX_INFO); // ERROR: Value cannot be null. (Parameter 'path1')
return MsgBoxYesNo(strMsg, strTitle); // if error show the standard dialog
}
}
public static bool EnMsgBoxYesNo(in string strMsg, in string strDescription = ClsStrings_ITA.STR_DLG_QUESTION, in string strTitle = ClsConstants.STR_PROJ_TITLE)
{
try
{
// Initialization:
Microsoft.WindowsAPICodePack.Dialogs.TaskDialog dialog = new()
{
Caption = strTitle,
InstructionText = strDescription,
Text = strMsg,
StandardButtons = TaskDialogStandardButtons.Yes | TaskDialogStandardButtons.No,
Icon = TaskDialogStandardIcon.Warning /* non trovo l'icona del punto interrogativo!!! :( */
};
// Show dialog:
TaskDialogResult result = dialog.Show();
// Check user input:
if (result == TaskDialogResult.Yes)
{
return true; // yes
}
else
{
return false; // no
}
}
catch (Exception ex)
{
MsgErrorHandler(ex.Message, ClsStrings_ITA.STR_ERR_MSGBOX_INFO); // ERROR: Value cannot be null. (Parameter 'path1')
return MsgBoxYesNo(strMsg, strTitle); // if error show the standard dialog
}
}
What can I also check? As I said, the executable in bin\Debug\net8.0-windows runs without this issue. Ok, I fixed it. In Publish, I went to "Show all settings" and in the "Profile settings" window I clicked the "File publish option" at the bottom and unticked the "Produce single file" setting. And this did the trick!!! I got a folder with many files, but same overall size, and much smaller executable size, that can be useful in case I should deliver an update of the executable only in future.
Want results from more Discord servers?
Add your server