Using the Win32 API for process communication---through the clipboard

Source: Internet
Author: User

The principle of invoking the//windows message mechanism in C # Win32 API//clipboard viewer chain. using system;using system.collections.generic;using System.componentmodel;using system.data;using system.drawing;using system.text;using System.Windows.Forms;using System.runtime.interopservices; namespace useclipboard{     Public partial class Frmmain:form     {        private int nextwindow;        [DllImport ("User32.dll")]&nb Sp       public static extern int setclipboardviewer (int h);        [DllImport ("User32.dll ")]        public static extern int SendMessage (int hwnd, int wmsg, int wParam, int lParam);  &NB Sp     protected override void WndProc (ref Message m)         {        &NBSP ;   Switch (m.msg)             {                CA Se 0x308://wm_drawclipboard                     //string message = string. Format ("Message received!" parameter: {0},{1} ", M.wparam, m.LParam);                   //mes Sagebox.show (message);                    if (Nextwindow! = 0)   &NB Sp                 {                  &N Bsp     SendMessage (Nextwindow, Int32.Parse (m.msg.tostring ()), M.wparam.toint32 (), M.lparam.toint32 ());                   }                &N Bsp   Pastefromclipboard ();                    break;    &NBS P           case 0x30d://wm_changecbchain                &N Bsp   String message2 = StrinG.format ("Message received!" parameter: {0},{1} ", M.wparam, m.LParam);                    Mes Sagebox.show (message2);                    if (Nextwindow = = M.wparam.to Int32 ())                         Nextwindow = M.lparam.toint32 ( );                    ELSE if (Nextwindow! = 0)       &NBS P             {                      &N Bsp SendMessage (Nextwindow, Int32.Parse (m.msg.tostring ()), M.wparam.toint32 (), M.lparam.toint32 ());                   }                    BR eak;                default:              &NBSP ;     base. DEfwndproc (ref m);                    break;        & nbsp  }       }        public frmmain ()         {  &nbsp ;         InitializeComponent ();            Nextwindow = Setclipboardviewe R (Handle.toint32 ());       }       //image         private image bmp        {            get            {& nbsp               return picturebox1.image;           }&NBSP ;           set            {                pictureBox1.Image = value;           }       }  &N Bsp &nbSp  //Picture description         private string info        {        &NBSP ;   get            {                return Txtima geinfo.text;           }            set      & nbsp     {                Txtimageinfo.text = value;      &NBS P    }       }                 Private Voi d Btnloadpic_click (object sender, EventArgs e)         {            Choos Eimagefile ();       }        //Select image         private void Chooseimagefile ()         {            if (Openfiledialog1.showdialog () = = DialogResult.ok)             {                BMP = new Bit Map (openfiledialog1.filename);           }       }       //Create objects based on user-defined information         private mypic createmypicobj ()         {  &nbsp ;         Mypic obj = new Mypic ();            Obj.pic = bmp;            Obj.picinfo = info;            return obj;      & nbsp }        //Copy objects to clipboard         private void CopyToClipboard ()     &NB Sp   {           //Create Mypic object             Mypic obj = Createm Ypicobj ();           //register type to Clipboard in the format "project name. Data format name"             Da Taformats.foRmat  userdefformat = Dataformats.getformat ("Useclipboard.mypic");           // Creating a data object, loading objects of type Mypic             IDataObject dataobj = new DataObject (obj);    &N Bsp      //other types of data can also be loaded into data Objects             Dataobj. SetData (Dataformats.unicodetext, info);            Dataobj. SetData (Dataformats.bitmap, BMP);           //Copy to Clipboard, the second parameter indicates that the program exits without emptying the Clipboard     & nbsp       Clipboard.setdataobject (dataobj,true);       }       &NBSP ; private void Btnexit_click (object sender, EventArgs e)         {          &NBS P Close ();                  }         Private VO ID Btncopytoclipboard_click (object sender, EventArgs e)         {            CopyToClipboard ();       }         Get data from clipboard         private void Pastefromclipboard ()         {           //clipboard have the data I need?             if (Clipboard.containsdata ("useclipboard.mypic") = = False)       & nbsp         return;           //Read data           &NBS P IDataObject clipobj = Clipboard.getdataobject ();           //Convert data to required type     &NBS P       Mypic mypicobj = clipobj. GetData ("Useclipboard.mypic") as mypic;           //Break down data from data Objects       &NB Sp     info = mypicobj.picinfo;            pictureBox1.Image = mypicobj.pic;  &N Bsp    }       &NBSp private void Btnpastefromclipboard_click (object sender, EventArgs e)         {      &NBS P     Pastefromclipboard ();       }        }}

Using the Win32 API for process communication---through the clipboard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.