c# – 如何将Outlook-Mail拖放到richTextBox中

c# – 如何将Outlook-Mail拖放到richTextBox中,第1张

概述我的WinForms-Application出了问题;我想将Outlook邮件拖放到RichTextBox中.我发现了很多关于Drag& Drop功能的文章,但是他们都将Mailtext插入到rTB中(参见: Link).实际上我可以将.txt,.jpg,Outlook-mails等文件从桌面插入到我的程序中.我的richTextBox会自动为文件生成图像并将此图像插入到某个位置.喜欢: . 在用 我的WinForms-Application出了问题;我想将Outlook邮件拖放到RichTextBox中.我发现了很多关于Drag& Drop功能的文章,但是他们都将Mailtext插入到rTB中(参见: Link).实际上我可以将.txt,.jpg,Outlook-mails等文件从桌面插入到我的程序中.我的richTextBox会自动为文件生成图像并将此图像插入到某个位置.喜欢:

.

在用户拖放文件后,将在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

如果我使用TaW答案中的代码,则给出以下输出:

双击图标后,邮件将不会打开…所以答案中的代码只是一个“iconcreation”.@H_502_42@先谢谢你!

解决方法 这是我在评论中的意思:

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 imagerTB_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调用..

总结

以上是内存溢出为你收集整理的c# – 如何将Outlook-Mail拖放到richTextBox中全部内容,希望文章能够帮你解决c# – 如何将Outlook-Mail拖放到richTextBox中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1231069.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存