网址拖放 [英] url drag and drop
问题描述
我想将链接从浏览器拖动到我的应用程序,并同时选择URL和与之关联的标题/名称.
我可以很容易地阅读该网址:
I want to drag a link from a browser to my application and pick up both the url and the title/name associated with it.
I can read the url easily enough:
object o = data.GetData(typeof(string));
if (o != null)
{
if (data.GetDataPresent(DataFormats.Html, true))
{
String htmlsnippet = (string)data.GetData(DataFormats.Html);
processHtml(htmlsnippet); // test code that looks at the html contents
pasteText((String)o); // pastes the url into my app - want title too!
问题在于html很少包含标题.
例如www.bbc.co.uk/news
将此拖放到我的应用中,我会看到网址
htmp片段为:
The problem is that the html rarely contains the title.
For example www.bbc.co.uk/news
drag this into my app and I see the url
and the htmp snippet is:
Version:0.9StartHTML:00000145EndHTML:00000282
StartFragment:00000179EndFragment:00000246
SourceURL:chrome://browser/content/browser.xul<html><body>
<!--StartFragment-->
<a href=\"http://www.bbc.co.uk/news/\">http://www.bbc.co.uk/news/</a><!--EndFragment-->
</body></html>
但是,如果我将此链接从浏览器拖到桌面上,则快捷方式是
叫做"BBC新闻-首页"
同样,尝试cnn.com并将快捷方式标记为''CNN.com International-
破碎的世界...''
谁能帮我找到标题信息?我不想检索页面,因为这会减慢拖放的速度,并且无论如何拖动到桌面都不会这样做,因为如果您脱机然后将链接从浏览器拖动到桌面,则它可以工作.
谢谢.
[修改:添加了前置标记...学习使用它们,它们是您的朋友!]
However if I drag this link from my browser to my desk top the short-cut is
called ''BBC News - Home''
Similarly try cnn.com and the shortcut is labelled ''CNN.com International -
Breaking, World ...''
Can anyone help me find the title information? I don''t want to retrieve the page as this will slow down the drag and drop, and anyway drag to desktop doesn''t do this since it works if you go off-line and then drag a link from the browser to desktop.
Thanks.
[Modified: added pre tags...learn to use them, they are your friend!]
推荐答案
MemoryStream fgdStream = (MemoryStream)e.Data.GetData("FileGroupDescriptor");
byte[] fgdBytes = new byte[fgdStream.Length];
fgdStream.Read(fgdBytes, 0, fgdBytes.Length);
fgdStream.Close();
StringBuilder fileName = new StringBuilder();
for (int i = 76; i < fgdBytes.Length; i++)
{
if (fgdBytes[i] != 0)
{
fileName.Append(Convert.ToChar(fgdBytes[i]));
}
else
{
break;
}
}
MessageBox.Show(fileName.Remove(fileName.Length - 4,4).ToString());
(删除"部分是因为名称以".URL"结尾)
(the Remove part is because the name ends in ".URL")
这篇关于网址拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!