【发布时间】:2011-04-11 18:28:08
【问题描述】:
我有一些代码允许我将超链接从网页拖放到 windows 窗体上,它将 URL 和标题分开并将它们放在两个不同的文本框中。
这在 windows XP 上运行良好,但在 windows 7 上不再运行。我不确定区别在哪里。
object data = e.Data.GetData("UniformResourceLocator");
数据总是空的,但是当我使用时
string[] fmts = e.Data.GetFormats();
其中一个 fmts 将始终是 UniformResourceLocator,还有一堆我似乎永远无法从中获取任何数据的其他 fmts。如果有人有资源可以指点我或我将不胜感激,这对我来说真的很困惑。
谢谢。
更新:添加了以前可以工作的方法代码
string hyperLinkUrl = null;
string hyperLinkText = null;
hyperLinkUrl = e.Data.GetData(typeof(string)) as string;
// some browser deliver url and text
// in UniformResourceLocator (Firebird)
string[] tokens = null;
if (hyperLinkUrl != null)
{
tokens = hyperLinkUrl.Split('\n');
}
if (tokens != null && tokens.Length > 1)
{
hyperLinkUrl = tokens[0];
hyperLinkText = tokens[1];
}
// we have to read FILEGROUPDESCRIPTOR to get the text (IE)
else
{
System.IO.Stream ioStream =
(System.IO.Stream)e.Data.GetData("FileGroupDescriptor");
byte[] contents = new Byte[512];
try
{
ioStream.Read(contents, 0, 512);
}
catch (Exception x)
{
}
ioStream.Close();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//The magic number 76 is the size of that part of the
//FILEGROUPDESCRIPTOR structure before
// the filename starts - cribbed
//from another usenet post.
for (int i = 76; contents[i] != 0; i++)
{
sb.Append((char)contents[i]);
}
if (!sb.ToString(sb.Length - 4, 4).ToLower().Equals(".url"))
{
throw new Exception("filename does not end in '.url'");
}
hyperLinkText = sb.ToString(0, sb.Length - 4);
}
tbLinkTitle.Text = hyperLinkText;
tbLinkAddress.Text = hyperLinkUrl;
【问题讨论】:
-
我添加了有点草率的方法代码
-
对不起。当代码显示
the magic number 76 is the size of that part of the FILEGROUPDESCRIPTOR structure before the filename starts之类的内容时,您尝试在与编写代码的平台不同的平台上使用它,所有的赌注都没有了。 -
怎么不行,是不是触发了异常?解析关闭了吗?细节。细节。详情。
-
@KyleGobel:您检查过
contents数组的值是否有任何内容?也许位置已经关闭,不再是 76 了。 -
对不起,我应该提到,ioStream 将为空,我实际上找不到 e.Data.GetData 不返回空值的方法,即使 e.Data.GetFormats 将返回一堆不同的格式可用。如果我可以将任何数据放入内容字节数组中,我可能会修复它。
标签: c# winforms drag-and-drop