加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:10:53 所属栏目:百科 来源:网络整理
导读:我的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 = "Mailn" + (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”.
先谢谢你!

解决方法

这是我在评论中的意思:

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 = "Mailn" + (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调用..

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读