A way to quickly show / hide WinForms GUI C #

I am creating a stream application. I started using the GUI (Announce: Form) as a separate thread.

This window will be very minimalistic with one input field and possibly with a button. In another thread, Tcp Client will be launched, and when it receives information from TcpServer, it should pass what falls into this input field and show gui (and the topmost windows). After a couple of seconds, gui should hide and wait for another tcp msg and so on.

    public void setTextBox(string varText) {
        if (InvokeRequired) {
            textBox.BeginInvoke(new textBoxCallBack(setTextBox), new object[] {varText});
        } else {
            textBox.Text = varText;
        }
    }

This code is used to populate a text field from a Tcp stream. The only problem now is to display the window and hide it. I tried many solutions and always something went wrong. How:

    private void windowStateChange(string varState) {
        if (InvokeRequired) {
            Invoke(new WindowStateChangeCallBack(windowStateChange), new object[] {varState});
        } else {
            if (varState == "Hide") {
                //Hide();
                // TopMost = false;
                //TopMost = varState != FormWindowState.Minimized;
            } else {
                //Show();
                //MessageBox.Show("TEST1");
            }
        }
    }

    public void windowStateChangeDiffrent(FormWindowState varState) {
        if (InvokeRequired) {
            Invoke(new WindowStateChangeCallBack(windowStateChange), new object[] {varState});
        } else {
            WindowState = varState;
           // Hide();
            TopMost = varState != FormWindowState.Minimized;
        }
    }

What would be the best way to do this (and faster, in time)?

Answer 1, which seems to work:

    private static void windowStateChange(string varState) {
        if (mainAnnounceWindow.InvokeRequired) {
            mainAnnounceWindow.BeginInvoke(new StateCallBack(windowStateChange), new object[] {varState});
        } else {
            if (varState == "Hide") {
                mainAnnounceWindow.Hide();
                mainAnnounceWindow.TopMost = false;
            } else {
                mainAnnounceWindow.Show();
                mainAnnounceWindow.TopMost = true;
            }
        }
    }

Is there anything bad about this?

+3
5

form.Hide() .

, , . , , - :

string RunningProcess = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(RunningProcess);

int SW_SHOW = 5, SW_HIDE = 0, SW_RESTORE = 9, SW_SHOWNORMAL = 1;

    for (int a = 0; a < processes.Length; a++)
        {
         IntPtr hWnd = processes[a].MainWindowHandle;

         ShowWindowAsync(hWnd, SW_RESTORE);
         ShowWindowAsync(hWnd, SW_SHOWNORMAL);
         ShowWindowAsync(hWnd, SW_SHOW);
         SetForegroundWindow((int)hWnd);
        }

        //Required Win32 API imports           
        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        static extern bool ShowWindowAsync(IntPtr windowHandle, int cmd);

        [System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
        private static extern IntPtr SetForegroundWindow(int hWnd);
+1

TCP. , RecievedData(...), , InvokeRequired ..
: #
http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx

+1

form.Hide();

, / ,

+1
this.Invoke(new MethodInvoker(this.hide()));
+1

Form.Hide() - . , Form.Show(), , Form.Activate(), .

You already correctly deal with sorting of streams (InvokeRequired and Invoke). You can also use Form.BeginInvoke (), which is an asynchronous version of Form.Invoke. It could be faster.

0
source

Source: https://habr.com/ru/post/1730662/


All Articles