Nice programing

신청서 창을 앞으로 가져 오려면 어떻게해야합니까?

nicepro 2020. 10. 17. 12:20
반응형

신청서 창을 앞으로 가져 오려면 어떻게해야합니까?


신청서 창을 앞으로 가져 오는 방법은 무엇입니까? 예를 들어 내 앱에주의가 필요한 경우.

이것은 내 개인 프로그램입니다. 그 기능이 필요합니다.

이것이 내가 얻은 것입니다. 하지만 100 % 작동 하지 않습니다 .

public void BringToFrontToEnterCaptha()
{
    if (InvokeRequired)
    {
        Invoke(new Action(BringToFrontToEnterCaptha));
    }
    else
    {
        this.TopMost = true;
        this.Focus();
        this.BringToFront();
        this.textBox1.Focus();
        this.textBox1.Text = string.Empty;
        System.Media.SystemSounds.Beep.Play();
    }
}

public void BringToBackAfterEnterCaptha()
{
    if (InvokeRequired)
    {
        Invoke(new Action(BringToBackAfterEnterCaptha));
    }
    else
    {
        this.TopMost = false;
    }
}

그리고 나는 백그라운드 작업자에게서 그들을 부릅니다.

BringToFrontToEnterCaptha();
while (!ready)
{
    Thread.Sleep(100);
}
BringToBackAfterEnterCaptha();
Thread.Sleep(300);

그리고 "Accept"버튼을 누르면 bool ready가 true로 설정됩니다.

나는 훌륭하게 일하지만 항상 그런 것은 아닙니다.


사용 Control.BringToFront:

myForm.BringToFront();

다음은 나를 위해 일한 코드입니다.

this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;

항상 원하는 창을 다른 모든 창 앞으로 가져옵니다.


사용 Form.Activate()또는 Form.Focus()방법.


나는 모든 사람에게 동의하지만 이것은 좋지 않은 행동이며 다음은 코드입니다.

[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);   


SetForegroundWindow(Handle.ToInt32());

최신 정보

David is completely right, for completeness I include the bullet point here +1 for David!

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

this works:

if (WindowState == FormWindowState.Minimized)
    WindowState = FormWindowState.Normal;
else
{
    TopMost = true;
    Focus();
    BringToFront();
    TopMost = false;
}

Before stumbling onto this post, I came up with this solution - to toggle the TopMost property:

this.TopMost = true;
this.TopMost = false;

I have this code in my form's constructor, eg:

public MyForm()
{
    //...

    // Brint-to-front hack
    this.TopMost = true;
    this.TopMost = false;

    //...
}

I use SwitchToThisWindow to bring the application to the forefront as in this example:

static class Program
{
    [DllImport("User32.dll", SetLastError = true)]
    static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);



    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        bool createdNew;
        int iP;
        Process currentProcess = Process.GetCurrentProcess();
        Mutex m = new Mutex(true, "XYZ", out createdNew);
        if (!createdNew)
        {
            // app is already running...
            Process[] proc = Process.GetProcessesByName("XYZ");

            // switch to other process
            for (iP = 0; iP < proc.Length; iP++)
            {
                if (proc[iP].Id != currentProcess.Id)
                    SwitchToThisWindow(proc[0].MainWindowHandle, true);
            }

            return;
        }

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new form());
        GC.KeepAlive(m);

    }

참고URL : https://stackoverflow.com/questions/5282588/how-can-i-bring-my-application-window-to-the-front

반응형