【问题标题】:Can we save the Selected Custom Colors in ColorIDialog?我们可以在 ColorIDialog 中保存选定的自定义颜色吗?
【发布时间】:2013-02-03 18:32:14
【问题描述】:

我在窗体关闭时显示颜色对话框有问题。我们可以在 VB.NET 的颜色对话框中保存自定义颜色选择吗?

【问题讨论】:

  • 我认为问题标题中有错字,额外的“I”,我认为您的意思是“ColorDialog”。另外我认为可以删除 VB.NET-2010 标记,这不是特定于 VB.NET 2010,我建议使用 [.net-4.0] 标记,也许还可以使用 [vb] 标记。

标签: winforms vb.net-2010


【解决方案1】:

您可以使用CustomColors 属性获取和设置自定义颜色。这是int 的数组,其中颜色格式为00BBGGRRB 是蓝色,G 是绿色,R 是红色。您可以将 .Net 颜色转换为这种格式:

Color myColor = Color.Green;
int ColorAsBGR = (((myColor.B << 16) | (myColor.G << 8)) | myColor.R);
dlgColor.CustomColors = new int[] { ColorAsBGR };

或不使用 .Net 颜色:

// Get the colors
int[] customColors = dlgColor.CustomColors;

// Set the custom colors
dlgColor.CustomColors = customColors;

您必须在 int 数组中存储和检索每种自定义颜色,并使用它设置 CustomColors 属性。

【讨论】:

    【解决方案2】:

    由于这个问题被标记为 VB.NET 2010,我将提供一个兼容的 VB.NET 答案。

    自定义颜色

    如果用户在使用ColorDialog 时添加了自定义颜色,您可以使用CustomColors 属性访问这些颜色。它以Integer() 的形式返回它们的颜色。

    使用My.Settings

    存储这些自定义颜色最方便的位置可能是My.Settings,如果您需要的话,它可以让您轻松存储每个用户的设置。

    如果您尝试使用 GUI 添加 Integer() 类型的设置,您会发现它不起作用,Visual Studio 不支持。

    幸运的是,您仍然可以通过手动编辑 Settings.settings 文件来完成这项工作。

    (感谢Jen-Ari for this related useful answer.

    1. 首先,使用“我的项目”中的 GUI,添加名为 CustomColorsString 类型设置,稍后我们将更改类型。
    2. 在解决方案资源管理器顶部,单击“显示所有文件”,展开“我的项目”。
    3. 您应该会看到一个 Settings.settings 文件,右键单击该文件,然后选择“打开方式”,选择 XML(文本)编辑器。

    文件的内容如下所示:

    Settings.settings

    <?xml version='1.0' encoding='utf-8'?>
    <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="My" GeneratedClassName="MySettings" UseMySettingsClassName="true">
      <Profiles />
      <Settings>
        <Setting Name="CustomColors" Type="System.String" Scope="User">
          <Value Profile="(Default)" />
        </Setting>
      </Settings>
    </SettingsFile>
    

    Type="System.String" 更改为Type="System.Int32[]",这样你就有了:

    <?xml version='1.0' encoding='utf-8'?>
    <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="My" GeneratedClassName="MySettings" UseMySettingsClassName="true">
      <Profiles />
      <Settings>
        <Setting Name="CustomColors" Type="System.Int32[]" Scope="User">
          <Value Profile="(Default)" />
        </Setting>
      </Settings>
    </SettingsFile>
    

    Form1.vb:

    下面是一些示例代码,展示了如何使用这种技术:

    Public Class Form1
    
        Private Sub btnChooseColor_Click(sender As Object, e As EventArgs) Handles btnChooseColor.Click
            'I'm assuming that dlgColorDialog has been placed using the Forms designer.
            dlgColorDialog.ShowDialog()
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Load the custom colors from My.Settings into the dialog when the form loads.
            dlgColorDialog.CustomColors = My.Settings.CustomColors
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            'Save the custom colors when the form closes.
            My.Settings.CustomColors = dlgColorDialog.CustomColors
            My.Settings.Save()
        End Sub
    
    End Class
    

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多