[C#] 포커스를 훔치지 않고 양식을 표시 하시겠습니까?

폼을 사용하여 알림을 표시하지만 (화면 오른쪽 하단에 나타남)이 폼을 표시하면 기본 폼에서 포커스를 훔칩니다. 초점을 훔치지 않고이 “알림”양식을 표시하는 방법이 있습니까?



답변

흠, 단순히 Form.ShowWithoutActivation을 재정의하는 것만으로는 충분하지 않습니까?

protected override bool ShowWithoutActivation
{
  get { return true; }
}

사용자가이 알림 창을 클릭하지 못하게하려면 CreateParams를 재정의 할 수 있습니다.

protected override CreateParams CreateParams
{
  get
  {
    CreateParams baseParams = base.CreateParams;

    const int WS_EX_NOACTIVATE = 0x08000000;
    const int WS_EX_TOOLWINDOW = 0x00000080;
    baseParams.ExStyle |= ( int )( WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW );

    return baseParams;
  }
}


답변

PInvoke.netShowWindow 메소드 에서 도난당했습니다 .

private const int SW_SHOWNOACTIVATE = 4;
private const int HWND_TOPMOST = -1;
private const uint SWP_NOACTIVATE = 0x0010;

[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(
     int hWnd,             // Window handle
     int hWndInsertAfter,  // Placement-order handle
     int X,                // Horizontal position
     int Y,                // Vertical position
     int cx,               // Width
     int cy,               // Height
     uint uFlags);         // Window positioning flags

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

static void ShowInactiveTopmost(Form frm)
{
     ShowWindow(frm.Handle, SW_SHOWNOACTIVATE);
     SetWindowPos(frm.Handle.ToInt32(), HWND_TOPMOST,
     frm.Left, frm.Top, frm.Width, frm.Height,
     SWP_NOACTIVATE);
}

(Alex Lyman이 대답했습니다. 코드를 직접 붙여 넣어 코드를 확장하고 있습니다. 편집 권한을 가진 사람이 코드를 복사하여 삭제할 수 있습니다.)


답변

Win32 P / Invoke 를 사용하려는 경우 ShowWindow 메서드를 사용할 수 있습니다 (첫 번째 코드 샘플은 원하는 것을 정확하게 수행함 ).


답변

이것이 나를 위해 일한 것입니다. 포커스가 거의없는 TopMost를 제공합니다.

    protected override bool ShowWithoutActivation
    {
       get { return true; }
    }

    private const int WS_EX_TOPMOST = 0x00000008;
    protected override CreateParams CreateParams
    {
       get
       {
          CreateParams createParams = base.CreateParams;
          createParams.ExStyle |= WS_EX_TOPMOST;
          return createParams;
       }
    }

Visual Studio 디자이너 또는 다른 곳에서 TopMost 설정을 생략해야합니다.

이것은 여기에서 도난 당하고, 실수로 차용되었습니다 (해결 방법을 클릭하십시오).

https://connect.microsoft.com/VisualStudio/feedback/details/401311/showwithoutactivation-is-not-supported-with-topmost


답변

이 작업은 해킹처럼 보이지만 작동하는 것 같습니다.

this.TopMost = true;  // as a result the form gets thrown to the front
this.TopMost = false; // but we don't actually want our form to always be on top

편집 : 참고로, 초점을 훔치지 않고 이미 만든 양식을 올릴 수 있습니다.


답변

Alex Lyman / TheSoftwareJedi의 답변에있는 pinvoke.net의 샘플 코드는 창을 “맨 위”창으로 만들며, 팝업 된 후에는 일반 창 뒤에 놓을 수 없습니다. 그가 사용하고자하는 것에 대한 Matias의 설명이 주어지면, 그것은 그가 원하는 것일 수 있습니다. 그러나 팝업 후 다른 창 뒤에 사용자가 창을 놓을 수있게하려면 샘플에서 HWND_TOPMOST (-1) 대신 HWND_TOP (0)을 사용하십시오.


답변

WPF에서는 다음과 같이 해결할 수 있습니다.

창에서 다음 속성을 입력하십시오.

<Window
    x:Class="myApplication.winNotification"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Notification Popup" Width="300" SizeToContent="Height"
  WindowStyle="None" AllowsTransparency="True" Background="Transparent" ShowInTaskbar="False" Topmost="True" Focusable="False" ShowActivated="False" >
</Window>

마지막 속성은 ShowActivated = “False”가 필요한 속성입니다.