Beispiel #1
0
        public static void communicate(string data, IntPtr handle, uint dataType)
        {
            IntPtr ptrCopyData = IntPtr.Zero;

            try
            {
                NativeMethods.COPYDATASTRUCT copyData = new NativeMethods.COPYDATASTRUCT();
                copyData.dwData = new IntPtr(3);
                copyData.cbData = data.Length + 1;
                copyData.lpData = Marshal.StringToHGlobalAnsi(data);
                ptrCopyData     = Marshal.AllocCoTaskMem(Marshal.SizeOf(copyData));
                Marshal.StructureToPtr(copyData, ptrCopyData, false);
                SendMessage(handle, dataType, IntPtr.Zero, ptrCopyData);
            }

            catch (Exception ex)
            {
                Debug.WriteLine("Error at fctb_box. Brodcaset data");
            }
            finally
            {
                // Free the allocated memory after the control has been returned
                if (ptrCopyData != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ptrCopyData);
                }
            }
        }
        protected override void WndProc(ref Message m)
        {
            if (m_aeroEnabled)
            {
                var v = 2;
                DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
                MARGINS margins = new MARGINS()
                {
                    bottomHeight = 1,
                    leftWidth    = 0,
                    rightWidth   = 0,
                    topHeight    = 0
                }; DwmExtendFrameIntoClientArea(this.Handle, ref margins);
            }

            if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)
            {
                m.Result = (IntPtr)HTCAPTION;
            }



            ////////////////////////////////////////////////////////////////////////////////
            ///       IMPORTANT: THIS MESSAGE CODE IS FOR MUTEX USED IN PROGRAM.CS       ///
            ///           DELETING THIS WILL CHANGE HOW MULTIPROCESS IS HANDLED          ///
            ////////////////////////////////////////////////////////////////////////////////



            else if (m.Msg == NativeMethods.WM_COPYDATA)
            {   // Extract the file name
                NativeMethods.COPYDATASTRUCT copyData =
                    (NativeMethods.COPYDATASTRUCT)Marshal.PtrToStructure
                        (m.LParam, typeof(NativeMethods.COPYDATASTRUCT));
                int dataType = (int)copyData.dwData;
                if (dataType == 2)
                {
                    string fileName = Marshal.PtrToStringAnsi(copyData.lpData);
                    if (fileName != null)
                    {
                        open(fileName);
                    }
                    BringWindowToFront();
                }



                else if (dataType == 3)
                {
                    string tabdata = Marshal.PtrToStringAnsi(copyData.lpData);
                    if (tabdata != null)
                    {
                        String[] spearator = { "$" };
                        Int32    count     = 10;
                        String[] strlist   = tabdata.Split(spearator, count,
                                                           StringSplitOptions.RemoveEmptyEntries);

                        if (strlist[1] == Program.guid.ToString())
                        {
                            if (strlist[2] == NativeMethods.DRAG_RECEIVED)
                            {
                                // Write file
                                TabClosedStarted = true;
                                TabCloseTrigger  = true;
                                string tempPath = Path.GetTempPath();


                                tabbutton btn = frame_home.buttonList[int.Parse(strlist[3])];
                                int       ind = frame_home.access_home.getFctbBox(btn.Target);
                                tempPath = Path.Combine(tempPath, "Xpress_editorGarbage.txt");
                                File.WriteAllText(tempPath, frame_home.fctbList[ind].Text);
                                TabClosedStarted = false;

                                // Send data to open file to pid
                                string location = btn.fileLocation;
                                if (location == null)
                                {
                                    location = "null";
                                }
                                string com_data = Program.guid + "$" + strlist[0] + "$" + NativeMethods.DRAG_FINISH + "$" +
                                                  Path.Combine(Path.GetTempPath(), "Xpress_editorGarbage.txt") + "$" + btn.Text + "$" + location + "$" + Convert.ToInt32(fctbList[getFctbBox(btn.Target)].IsChanged);
                                Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
                                foreach (Process p in processes)
                                {
                                    if (p.Id.ToString() == strlist[0])
                                    {
                                        IntPtr windowHandle = p.MainWindowHandle;
                                        NativeMethods.communicate(com_data, windowHandle, NativeMethods.WM_COPYDATA);
                                    }
                                }
                            }
                            else if (strlist[2] == NativeMethods.DRAG_FINISH)
                            {
                                string guid      = strlist[0];
                                string grbg_path = strlist[3];
                                string name      = strlist[4];
                                string location  = strlist[5];
                                bool   ischanged = Convert.ToBoolean(Int32.Parse(strlist[6]));
                                if (location == "null")
                                {
                                    location = null;
                                }
                                int old = buttonList.Count;
                                new Thread(() => threadFun(name, location, ischanged)).Start();
                                Thread.Sleep(100);
                                newTab(grbg_path, -1);
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show(String.Format("Unrecognized data type = {0}.",
                                                  dataType), "SendMessageDemo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (m.Msg == Program.WM_ACTIVATEAPP)
            {
                // Maximizes and bring it to front.
                BringWindowToFront();
            }
            // END
            base.WndProc(ref m);
        }
Beispiel #3
0
        /// args  -  Filepath of garbage file, name of tab, if tab is saved - location of saved file
        static void Main(string[] args)
        {
            guid = "034c3adc-0056-4167-97e0-772f92d572fa";
            bool createdNew = true;

            if (args.Length == 4)
            {
                guid = Guid.NewGuid().ToString();
            }

            //by creating a mutex, the next application instance will detect it
            //and the code will flow through the "else" branch
            using (Mutex mutex = new Mutex(true, guid, out createdNew))//make sure it's an unique identifier (a GUID would be better)
            {
                if (createdNew)
                {
                    string filename;

                    if (args != null && args.Length > 0)
                    {
                        filename = args[0];
                    }
                    else
                    {
                        filename = null;
                    }


                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    frame_home frame = new frame_home(filename);

                    // check for modifications
                    if (args.Length == 4)
                    {
                        string location = args[2];
                        if (location == "null")
                        {
                            location = null;
                        }
                        frame.newWindowChange(args[1], location, Convert.ToBoolean(Int32.Parse(args[3])));
                    }

                    guid = Process.GetCurrentProcess().Id.ToString();
                    Debug.WriteLine("original-" + guid);
                    Application.Run(frame);
                }
                else
                {
                    //we tried to create a mutex, but there's already one (createdNew = false - another app created it before)
                    //so there's another instance of this application running
                    Process currentProcess = Process.GetCurrentProcess();

                    //get the process that has the same name as the current one but a different ID
                    foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
                    {
                        if (process.Id != currentProcess.Id)
                        {
                            IntPtr handle = process.MainWindowHandle;

                            //if the handle is non-zero then the main window is visible (but maybe somewhere in the background, that's the reason the user started a new instance)
                            //so just bring the window to front
                            if (handle != IntPtr.Zero)
                            {
                                IntPtr ptrCopyData = IntPtr.Zero;
                                try
                                {
                                    // Create the data structure and fill with data
                                    NativeMethods.COPYDATASTRUCT copyData = new NativeMethods.COPYDATASTRUCT();
                                    copyData.dwData = new IntPtr(2);
                                    if (args != null && args.Length > 0)
                                    {
                                        string fileName = args[0];
                                        // Just a number to identify the data type
                                        copyData.cbData = fileName.Length + 1;  // One extra byte for the \0 character
                                        copyData.lpData = Marshal.StringToHGlobalAnsi(fileName);

                                        // Allocate memory for the data and copy
                                        ptrCopyData = Marshal.AllocCoTaskMem(Marshal.SizeOf(copyData));
                                        Marshal.StructureToPtr(copyData, ptrCopyData, false);

                                        // Send the message
                                        NativeMethods.SendMessage(handle, NativeMethods.WM_COPYDATA, IntPtr.Zero, ptrCopyData);
                                    }
                                    else
                                    {
                                        PostMessage((IntPtr)HWND_BROADCAST, WM_ACTIVATEAPP, IntPtr.Zero, IntPtr.Zero);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.ToString(), "SendMessage Demo",
                                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                finally
                                {
                                    // Free the allocated memory after the control has been returned
                                    if (ptrCopyData != IntPtr.Zero)
                                    {
                                        Marshal.FreeCoTaskMem(ptrCopyData);
                                    }
                                }
                            }
                            else
                            {
                                //tough luck, can't activate the window, it's not visible and we can't get its handle
                                //so instead notify the process that it has to show it's window
                                PostMessage((IntPtr)HWND_BROADCAST, WM_ACTIVATEAPP, IntPtr.Zero, IntPtr.Zero);//this message will be sent to MainForm
                            }
                            break;
                        }
                    }
                }
            }
        }