【问题标题】:Drag & Drop background color拖放背景颜色
【发布时间】:2023-03-03 01:17:01
【问题描述】:

我希望在将文件从桌面拖放到窗体时更改 MainForm 的背景。 Photo of form, where I want to change BG这里是拖放功能的代码。

 private void ThisForm_DragEnter(object sender, DragEventArgs e)
    {
        
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.All;
            
        }
        else
            e.Effect = DragDropEffects.None;
    }
    private void ThisForm_DragDrop(object sender, DragEventArgs e)
    {
        
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        if (files != null && files.Length != 0)
        {
            if (Path.GetExtension(files[0]) == ".pdf")
            {
                TextBoxSelectPdf.Text = files[0];
            }
            else
            {
                MessageBox.Show("Galimas tik PDF formatas");
            }

        }

    }

我的问题 - 如何在删除文件时更改背景颜色。

【问题讨论】:

  • 您的问题是什么?表单有一个BackColor 属性,你可以设置任何你想要的颜色。
  • 并在 DragLeave 和/或 DragDrop 上重置..
  • @keco 我的例子是如何工作的?因为我已经尝试过了,但没有成功。
  • 当我尝试写 this.backcolor 时 - 我没有任何选择颜色的选项,只有错误。

标签: c# winforms drag-and-drop background-color


【解决方案1】:

根据您的最后评论,您的表单的 BackgroundColour 属性似乎存在问题。

您可以随时使用以下方法设置表单的 BackgroundColour:

this.Backcolor = Color.Red;

只要您在 Color 后键入一个点,您就会得到一个下拉菜单,建议您使用可用的颜色(可能仅适用于 Visual Studio)。然后,您可以设置所需的颜色。

即使您没有获得下拉菜单,红色、蓝色、黑色、白色等颜色也始终可用。

不要离开这部分! :)

如果您想在拖入某些内容时更改 BackgroundColour,请在此 e.Effect = DragDropEffects.None; 之后的 DragEnter 事件中将上述行添加到新行中。

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多