如何禁用拖/放时,对话框打开 [英] How to disable drag/drop when a dialog box is open

查看:96
本文介绍了如何禁用拖/放时,对话框打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在一个大的应用程序,我加入了一些拖/放功能吧。具体地讲,我允许用户拖动和一个文件拖放到主窗口中打开的文件。



的问题在于,拖/放操作仍允许当主窗口被显示对话框(例如,用于在当前打开的文件的项目的属性窗口)发生。我宁可不要让这种情况发生,如果主窗口正在显示一个模式对话框。这是因为加载应用程序中的新的文件,而对话框打开可能会引起程序崩溃:该代码调用对话框不会想到打开的文件进行更改,而对话框打开(这就是为什么对话框是模态...)。



主要的应用程序是用C ++写的,但我张贴一个C#示例。症状/行为是两个平台上是相同的,但我可以用C#更少的代码演示它。我很熟悉这两种语言/平台,所以我可以转换为所需要的相应的语言任何答案。



要证明我的样本代码的问题,编译并运行下面的C#代码。这将创建一个主窗口,是一个有效的放置目标。拖动并从Windows资源管理器文件拖放到主窗口:你应该看到一个下降的消息框。现在,单击窗体上的按钮,弹出一个对话框。同样,试图拖,而对话框打开一个文件拖放到主窗口。请注意,降允许的,即使一个模式对话框打开。我怎样才能防止这种情况发生时,对话框打开?



答案显然是临时设置的AllowDrop为假,而打开对话框。问题是,主要的应用是非常大的,因此有可打开的对话框中的许多地方。这将是很难找到打开一个对话框,每一个地方,添加此代码。此外,这里的每一个开发人员需要知道在每次打开模态窗口时间执行此操作;这是不可能的,每个人都会记得。我很担心,这不是一个很好的解决方案。



当然有,不需要在一个对话的每一个地方添加代码这样做的更易于维护的方法?即会打开

 使用系统;使用System.Windows.Forms的
;
使用System.Drawing中;

公共类MyDialog:表格{
公共MyDialog(){
文本=MyDialog;
}
}
公共类的MainForm:表格{
公众的MainForm(){
键BTN =新按钮();
btn.Location =新点(0,0);
btn.Text =ShowDialog的;
btn.Size =新尺寸(75,23);
btn.Click + =新的EventHandler(GoToDialog);

this.AllowDrop = TRUE;
this.Controls.Add(BTN);
this.Text =放下目标;
this.DragDrop + =新DragEventHandler(this.MyDragDrop);
this.DragEnter + =新DragEventHandler(this.MyDragEnter);
}
私人无效MyDragDrop(对象发件人,DragEventArgs E){
MessageBox.Show(下降);
}
私人无效MyDragEnter(对象发件人,DragEventArgs E){
e.Effect = DragDropEffects.Copy;
}
私人无效GoToDialog(对象发件人,EventArgs五){使用
(MyDialog AB =新MyDialog()){
ab.ShowDialog(本);
}
}
}
静态类节目{
[STAThread]
静态无效的主要(){
Application.Run(新的MainForm ());
}
}


解决方案

我说不准的事情在C#中是如何工作的,所以让我知道,如果这个答案是不正确。在C ++中的MFC,将显示一个对话框时,被禁用的主窗口。您可以测试,看看主窗口被禁用,忽略下降如果是这样。

 私人无效MyDragDrop(对象发件人,DragEventArgs E){
如果(CanFocus)
MessageBox.Show(下降);
}
私人无效MyDragEnter(对象发件人,DragEventArgs E){
如果(CanFocus)
e.Effect = DragDropEffects.Copy;
,否则
e.Effect = DragDropEffects.None;
}


I am working on a large application and am adding some drag/drop functionality to it. Specifically, I am allowing the user to drag and drop a file into the main window to open the file.

The problem is that the drag/drop operation is still allowed to happen when the main window is displaying a dialog box (for example, a properties window for an item in the currently-open file). I would rather not allow this to happen if the main window is displaying a modal dialog box. This is because loading the new file in the application while the dialog box is open would probably crash the program: the code calling the dialog box does not expect the open file to be changed while the dialog box is open (that is why the dialog box was modal...).

The main application is written in C++, but I am posting a C# sample. The symptom/behavior is the same on both platforms, but I can demonstrate it in much less code with C#. I am very familiar with both languages/platforms so I can translate any answers to the appropriate language as needed.

To demonstrate the problem with my sample code, compile and run the following C# code. It will create a "main window" that is a valid drop target. Drag and drop a file from Windows Explorer onto the main window: you should see a "dropped" message box. Now, click the button on the form to pop up a dialog box. Again, attempt to drag and drop a file onto the main window while the dialog box is open. Notice that the drop is allowed even though a modal dialog box is open. How can I prevent this from happening when the dialog is open?

The obvious answer is to temporarily set AllowDrop to false while opening the dialog box. The problem is that the main application is very large and so there are numerous places that open dialog boxes. It will be difficult to find every single place that opens a dialog and add this code. Plus, every developer here would need to know to perform this action every time they open a modal window; it is unlikely that everyone will remember. I am worried that this is not a very good solution.

Surely there is a more maintainable method of doing this that doesn't require adding code in every place that a dialog is opened?

using System;
using System.Windows.Forms;
using System.Drawing;

public class MyDialog : Form {
    public MyDialog() {
        Text = "MyDialog";
    }
}
public class MainForm : Form {
    public MainForm() {
        Button btn = new Button();
        btn.Location = new Point(0, 0);
        btn.Text = "ShowDialog";
        btn.Size = new Size(75, 23);
        btn.Click += new EventHandler(GoToDialog);

        this.AllowDrop = true;
        this.Controls.Add(btn);
        this.Text = "Drop Target";
        this.DragDrop += new DragEventHandler(this.MyDragDrop);
        this.DragEnter += new DragEventHandler(this.MyDragEnter);
    }
    private void MyDragDrop(object sender, DragEventArgs e) {
        MessageBox.Show("dropped");
    }
    private void MyDragEnter(object sender, DragEventArgs e) {
        e.Effect = DragDropEffects.Copy;
    }
    private void GoToDialog(object sender, EventArgs e) {
        using (MyDialog ab = new MyDialog()) {
            ab.ShowDialog(this);
        }
    }
}
static class Program {
    [STAThread]
    static void Main() {
        Application.Run(new MainForm());
    }
}

解决方案

I'm not sure how things work in C#, so let me know if this answer is incorrect. In C++ MFC, the main window is disabled when a dialog is displayed. You can test to see if the main window is disabled and ignore the drop if so.

private void MyDragDrop(object sender, DragEventArgs e) {
    if (CanFocus)
        MessageBox.Show("dropped");
}
private void MyDragEnter(object sender, DragEventArgs e) {
    if (CanFocus)
        e.Effect = DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;
} 

这篇关于如何禁用拖/放时,对话框打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆