.
在用户拖放文件后,将在Drop位置创建一个图像,如果用户双击该图像,将打开该文件.
问题:
该程序工作正常,但如果我尝试将邮件拖出Outlook,该程序将邮件主体插入richTextBox而不是图像.
我在桌面上保存了一封邮件,并尝试将此邮件插入我的程序.我的richTextBox中给出了以下输出(将是完美的):
每个Drag& Drop桌面上的Mailicon:
否则我试图将一个mai从Outlook拖放到我的程序中并给出以下输出(只看文本而不是图像:
来自每次拖放的Outlook邮件(问题!!!):
该程序将cc / mailadress和Mailbody插入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,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; }
用户双击图片后,应在Outlookexplorer中打开邮件.
UPDATE
解决方法
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 = Explorer.Selection; foreach (object item in oSelection) { Microsoft.Office.Interop.Outlook.MailItem mi = (Microsoft.Office.Interop.Outlook.MailItem)item; // rTB_test.Text = mi.Body.ToString(); Label lbl = new Label(); lbl.AutoSize = false; lbl.Size = new Size( 80,50); // <-- your choice! lbl.Text = someText; // <-- your choice! lbl.TextAlign = ContentAlignment.BottomCenter; lbl.Image = someImage; // <-- your choice! lbl.ImageAlign = ContentAlignment.TopCenter; int count = rTB_test.Controls.Count; int itemsPerRow = rTB_test.Width / 80; lbl.Location = new Point( (count % itemsPerRow) * 80,count / itemsPerRow * 50); lbl.Tag = mi; // store the data object lbl.MouseDoubleClick += lbl_MouseDoubleClick; lbl.Parent = rTB_test; // add to the RTF's Controls } } 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.. }
这些标签将位于RTB中的任何文本之上,而不会干扰它.如果您愿意,您可以修改位置的代码以将其移开或以许多其他方式设置标签样式.
更新
在最后一次评论之后,问题变得更加清晰:应用程序的其他部分已经向RTB添加了邮件图标,我们将添加更多“相同”的内容.
// create some test text,maybe extract it from the mailheader?.. string mailName = "Mail\n" + (mailList.Count + 1); // load an image with enough room at the bottom to add some text: Image img = Image.FromFile(yourMailImageFile); // make the images unique by embedding a counter in a bright pixel: img = (Image)fingerPrintID((Bitmap)img,250 - mailList.Count); //*1* // 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,width); G.DrawString(mailName,img.Height - s.Height - 1); } // adding the image is easy only if we use the clipboard.. Clipboard.Setimage(img); // insert only at the end! rTB_test.SelectionStart = rTB_test.TextLength; rTB_test.SelectionLength = 0; // 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; // retrieve the counter id: string id = GetID(rTB_test.SelectedRtf); //*2* // finally we need to store the mail itself with its key: mailList.Add(id,mi); // cleanup: unselect and set cursor to the end: rTB_test.SelectionStart = rTB_test.TextLength; rTB_test.SelectionLength = 0
Dictionary<string,Microsoft.Office.Interop.Outlook.MailItem> mailList = new Dictionary<string,Microsoft.Office.Interop.Outlook.MailItem>(); // *3*
以下是我们如何访问DoubleClick活动中的邮件:
private void rTB_test_DoubleClick(object sender,EventArgs e) { var ss = rTB_test.SelectionStart; var sl = rTB_test.SelectionLength; string id = GetID(sr); //*4* // a few checks: if (sl == 1 && mailList.Keys.Contains(id) && sr.Contains(@"{\pict\") ) { Microsoft.Office.Interop.Outlook.MailItem mi = mailList[id]; // do stuff with the msgitem,e.g.. mi.display(); } }
以下是我使用的图像的结果:
请注意,除了添加图像之外,我们还将字典中的邮件数据和图像的位置存储在RTB中.
更新2:我已将图像的位置替换为其RtfText的HashCode的键;这对RTF其他内容的任何变化都很有用.但是,它依赖于图像的唯一性,因此建议在代码中添加索引. (或设置一些随机像素,可能基于GUID ..)
更新3& 4:(* 1 * – * 6 *)
我们发现,我们需要使键能够适应多种变化,例如图标周围的字体,这将影响Rtf代码,或者用户放大图像.
这是一个FingerPrint功能,通过在图像顶部设置几个像素,可以使我们添加的图像不显眼.三个设置标记,一个设置ID:
Bitmap fingerPrintID(Bitmap bmp,int key) //*5* { for (int i = 0; i < 3; i++) { bmp.SetPixel(i,Color.FromArgb(255,238,238)); // EE EE EE } bmp.SetPixel(3,key,key)); return bmp; }
要检索此函数,将从RTF代码中拉出3个十六进制数字作为字符串:
string GetID(string Rtf) //*6* { int x = Rtf.IndexOf("eeeeeeeeeeeeeeeeee"); // 238 238 238 if (x < 0) return ""; string hex = Rtf.Substring(x +18,6); return hex; }
我选择的像素相当明亮;如果你知道你使用哪个图像,你可以更多地优化颜色选择..使用新的字符串id,我们不需要GetHashcode调用..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。