我有一个WinForms应用程序的问题;我想要将Outlook邮件拖放到RichTextBox中。我找到了许多关于拖放功能的文章,但它们都将Mailtext插入到rTB中(参见:链接)。实际上,我可以将桌面上的文档(如.txt、.jpg、Outlook邮件)插入到我的程序中。我的richTextBox会自动生成文件的图像,并将此图像插入到位置上。例如:
用户双击图片后,应该在Outlook浏览器中打开邮件。
更新:如果我使用TaW答案中的代码,则会输出以下内容:
在我双击图标后,邮件不会被打开...所以答案中的代码只是"图标创建"。
感谢您的支持!
。
用户拖放文件后,图像将在放置位置创建,如果用户双击图像,则文件将被打开。
问题:
程序工作正常,但是如果我尝试从Outlook中拖动邮件,则程序会将邮件正文插入到richTextBox中,而不是作为图像。
我在桌面上保存了一封邮件并尝试将该邮件插入到我的程序中。我的richTextBox中输出以下内容(完美):
来自桌面的Mailicon通过拖放:
否则,我试图从Outlook中拖放邮件,我的程序会输出以下内容(只看文本而不是图片):
通过Outlook拖放的邮件(问题!):
程序将cc/mail地址和邮件正文插入到rTB中。
这是代码后面的部分:(我的richTextBox是一个名为"MyRichTextBox"的自己创建的richTextBox,请在此处下载项目:link_RICHTEXTBOX。)
private void Form1DragDrop(object sender, DragEventArgs e)
{
Startup();
//Microsoft.Office.Interop.Outlook.ApplicationClass oApp =
// new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.Explorer oExplorer = _Outlook.ActiveExplorer();
Microsoft.Office.Interop.Outlook.Selection oSelection = oExplorer.Selection;
foreach (object item in oSelection)
{
Microsoft.Office.Interop.Outlook.MailItem mi = (Microsoft.Office.Interop.Outlook.MailItem)item;
rTB_test.Text = mi.Body.ToString();
string mailName = "Mail\n" + (mailList.Count + 1);
// load an image with enough room at the bottom to add some text:
Image img = Image.FromFile(Imagepath);
// now we add the text:
int width = img.Width;
using (Graphics G = Graphics.FromImage(img))
using (Font font = new Font("Arial", 7f))
{
SizeF s = G.MeasureString(mailName, font, width);
G.DrawString(mailName, font, Brushes.Black,
(width - s.Width) / 2, img.Height - s.Height - 1);
}
// adding the image is easy only if we use the clipboard..
Clipboard.SetImage(img);
// now insert image
rTB_test.Paste();
// now we can get a hashcode as a unique key..
// ..we select the image we have just inserted:
rTB_test.SelectionStart = rTB_test.TextLength - 1;
rTB_test.SelectionLength = 1;
// finally we need to store the mail itself with its key:
mailList.Add(rTB_test.SelectedRtf.GetHashCode(), mi);
// cleanup: unselect and set cursor to the end:
rTB_test.SelectionStart = rTB_test.TextLength;
rTB_test.SelectionLength = 0;
}
Microsoft.Office.Interop.Outlook.Application _Outlook = null;
Dictionary<int, Microsoft.Office.Interop.Outlook.MailItem> mailList =
new Dictionary<int, Microsoft.Office.Interop.Outlook.MailItem>();
private void rTB_test_DoubleClick(object sender, EventArgs e)
{
var ss = rTB_test.SelectionStart;
var sl = rTB_test.SelectionLength;
int hash = rTB_test.SelectedRtf.GetHashCode();
// a few checks:
if (sl == 1 && mailList.Keys.Contains(hash))
{
Microsoft.Office.Interop.Outlook.MailItem mi = mailList[hash];
// do stuff with the msgItem..
// ..
}
}
void lbl_MouseDoubleClick(object sender, MouseEventArgs e)
{
Microsoft.Office.Interop.Outlook.MailItem mi =
(Microsoft.Office.Interop.Outlook.MailItem)((Label)sender).Tag;
// code to process the doubleclicked mail item..
}
void Startup()
{
_Outlook = new Microsoft.Office.Interop.Outlook.Application();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
用户双击图片后,应该在Outlook浏览器中打开邮件。
更新:如果我使用TaW答案中的代码,则会输出以下内容:
在我双击图标后,邮件不会被打开...所以答案中的代码只是"图标创建"。
感谢您的支持!
mi.Display();
可以完成工作... - 看起来您没有删除显示正文文本的旧代码?带有“测试”文本的两个或三个(?)图标是什么?它们看起来与您之前展示的图标完全不同?而且它们看起来像是悬停在文本上方? - TaW