public static void ForceWindowIntoForeground(IntPtr window) { const uint LSFW_LOCK = 1; const uint LSFW_UNLOCK = 2; const int ASFW_ANY = -1; // by MSDN uint currentThread = Native.GetCurrentThreadId(); IntPtr activeWindow = User32.GetForegroundWindow(); //uint activeProcess; uint activeThread = User32.GetWindowThreadProcessId(activeWindow, IntPtr.Zero); uint windowProcess; uint windowThread = User32.GetWindowThreadProcessId(window, IntPtr.Zero); if (currentThread != activeThread) { User32.AttachThreadInput(currentThread, activeThread, true); } if (windowThread != currentThread) { User32.AttachThreadInput(windowThread, currentThread, true); } uint oldTimeout = 0, newTimeout = 0; User32.SystemParametersInfo(User32.SPI_GETFOREGROUNDLOCKTIMEOUT, 0, ref oldTimeout, 0); User32.SystemParametersInfo(User32.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, ref newTimeout, 0); User32.LockSetForegroundWindow(LSFW_UNLOCK); User32.AllowSetForegroundWindow(ASFW_ANY); User32.SetForegroundWindow(window); User32.ShowWindow(window, User32.SW.SW_RESTORE); User32.SetFocus(window); User32.SystemParametersInfo(User32.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, ref oldTimeout, 0); if (currentThread != activeThread) { User32.AttachThreadInput(currentThread, activeThread, false); } if (windowThread != currentThread) { User32.AttachThreadInput(windowThread, currentThread, false); } }
private void Entry_Load(object sender, EventArgs e) { baseTT.ShowAlways = true; baseTT.SetToolTip(btnLoad, "Open .PLR file"); baseTT.SetToolTip(btnReload, "Reload the last .PLR file"); baseTT.SetToolTip(btnSave, "Save the currently open .PLR file (and overwrite the origional)"); btnReload.UseCompatibleTextRendering = true; pbCollection.AddRange(new List <PictureBox> { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8, pictureBox9, pictureBox10, pictureBox11, pictureBox12, pictureBox13, pictureBox14, pictureBox15, pictureBox16, pictureBox17, pictureBox18, pictureBox19, pictureBox20, pictureBox21, pictureBox22, pictureBox23, pictureBox24, pictureBox25, pictureBox26, pictureBox27, pictureBox28, pictureBox29, pictureBox30, pictureBox31, pictureBox32, pictureBox33, pictureBox34, pictureBox35, pictureBox36, pictureBox37, pictureBox38, pictureBox39, pictureBox40, pictureBox41, pictureBox42, pictureBox43, pictureBox44, pictureBox45, pictureBox46, pictureBox47, pictureBox48, pictureBox49, pictureBox50, pictureBox51, pictureBox52, pictureBox53, pictureBox54, pictureBox55, pictureBox56, pictureBox57, pictureBox58 }); //0-50 inv, 51 - 58 ammo / coins pnCollection.AddRange(new List <Panel> { hairPnl, skinPnl, eyesPnl, shirtPnl, undershirtPnl, pantsPnl, shoesPnl }); int cnt = 0; itemLV.View = View.Details; itemLV.Columns[0].Width = itemLV.Width - 25; imageList1.ImageSize = new Size(32, 32); itemLV.SmallImageList = imageList1; itemLV.BeginUpdate(); Stopwatch st = new Stopwatch(); st.Start(); foreach (baseItem itm in ih.globalTerrariaItems) { cbItem.Items.Add(itm.name); imageList1.Images.Add(itm.icon); ListViewItem tmp = new ListViewItem(); tmp.Text = itm.name; tmp.ImageIndex = cnt; itemLV.Items.Add(tmp); lvis.Add(tmp); cnt++; } st.Stop(); Console.WriteLine("load took " + st.Elapsed); itemLV.Sorting = SortOrder.Ascending; itemLV.Sort(); itemLV.EndUpdate(); this.Invoke(new MethodInvoker(delegate() { ld.Close(); })); //just show the f*****g form to the user User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id); User32.SetForegroundWindow(Handle); User32.ShowWindow(Handle, User32.SW_SHOWNORMAL); foreach (itemPrefix ipf in ih.globalItemPrefixes) { cbPrefixes.Items.Add(ipf.name); } cbItem.SelectedIndex = 0; cbPrefixes.SelectedIndex = 0; nudQuant.MouseWheel += new MouseEventHandler(this.ScrollHandlerFunction); nudHealthCur.MouseWheel += new MouseEventHandler(this.ScrollHandlerFunction); nudHealthMax.MouseWheel += new MouseEventHandler(this.ScrollHandlerFunction); nudManaCur.MouseWheel += new MouseEventHandler(this.ScrollHandlerFunction); nudManaMax.MouseWheel += new MouseEventHandler(this.ScrollHandlerFunction); }
public static void Run(string appId, LaunchAction action, string[] args) { Mutex m_mutex = new Mutex(false, @"Local\" + appId); RunningObjectTable rot = new RunningObjectTable(); for (int i = 0; i < 120; i++) // wait for ~60 seconds { if (m_mutex.WaitOne(1, false)) { // we are the first instance try { using (rot.Register(appId, new OleCommandTargetImpl(action))) { action(args, true); } return; } finally { m_mutex.ReleaseMutex(); } } else { // we are not the first instance IOleCommandTargetWithExecParams instance = rot.GetObject(appId) as IOleCommandTargetWithExecParams; if (instance != null) { try { object dummy = null; object processId = null; instance.Exec(Guid.Empty, 0, OLECMDEXECOPT.DODEFAULT, ref dummy, ref processId); User32.AllowSetForegroundWindow((int)processId); object objArgs = args; object result = null; instance.Exec(Guid.Empty, 1, OLECMDEXECOPT.DODEFAULT, ref objArgs, ref result); if (result is bool && (bool)result) { return; } } catch (Exception e) { Trace.WriteLine(e.ToString()); } finally { Marshal.ReleaseComObject(instance); } } } Thread.Sleep(500); } Trace.WriteLine(appId + " could not start!"); }