How to drag Outlook Mail-Mail to richTextBox

I am having a problem with my WinForms-Application; I want to drag and send Outlook mail to a RichTextBox. I found many articles about the Drag & Drop function, but they all embed Mailtext in rTB (see link ). In fact, I can insert a document as .txt, .jpg, Outlook mail from the desktop ... into my program. My richTextBox automatically creates an image for the file and inserts that image into position. How:

rTB.

After the user drags the file, the image will be created in the “Drop” position, and if the user double-clicks the image, the file will be opened.

Problem:

The program works fine, but if I try to drag and drop mail from Outlook, the program inserts the mailbox into a richTextBox and not as an image.

I saved one Mail on the desktop and try to paste this letter into my program. The following output is in my richTextBox (would be perfect):

Mailicon from the desktop to Drag & Drop:

MailIconfromdesk

Otherwise, I tried to drag and drop May from Outlook into my program, and the following output is given (just look at the text, not the images:

Mail from Outlook for Drag & Drop (PROBLEM !!!): InsertFromOutlook

The program inserts cc / mailadress and Mailbody into rTB.

Here is the code behind: (My richTextBox is my own created richTextBox called MyRichTextBox. Download the project under: link_RICHTEXTBOX .

CODE

    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;
        }

, , Outlookexplorer.

UPDATE

TaW, :

DragDropfromOutlook

, , ... - "iconcreation". !

+4
1

:

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 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, 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);
// 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();

    }
}

, :

enter image description heremail

, , RTB .

2: HashCode RtfText; RTF. , , . ( , , GUID.)

3 4: (* 1 * - * 6 *)

, , , , , Rtf , .

FingerPrint, , , , . :

Bitmap fingerPrintID(Bitmap bmp, int key)  //*5*
{
    for (int i = 0; i < 3; i++)
    {
        bmp.SetPixel(i, 0, Color.FromArgb(255, 238,238,238)); // EE EE EE
    }
    bmp.SetPixel(3, 0, Color.FromArgb(255, key, key, key));
    return bmp;
}

3 RTF-:

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;
}

; , , . GetHashcode.

+1

All Articles