IT TIP

Alt + F4 닫기 양식을 비활성화하는 방법은 무엇입니까?

itqueen 2020. 10. 17. 12:41
반응형

Alt + F4 닫기 양식을 비활성화하는 방법은 무엇입니까?


사용자가 양식을 닫지 못하도록 ac # win 양식에서 Alt+ 를 비활성화하는 가장 좋은 방법은 무엇입니까 F4?

진행률 표시 줄을 표시하는 팝업 대화 상자로 양식을 사용하고 있으며 사용자가 닫을 수 없도록하고 싶습니다.


이것은 일을합니다 :

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
}

편집 : pix0rs 우려에 대한 응답으로-예, 프로그램 적으로 앱을 닫을 수 없다는 것이 맞습니다. 그러나 양식을 닫기 전에 form_closing 이벤트에 대한 이벤트 핸들러를 간단히 제거 할 수 있습니다.

this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Close();

당신이 보면 FormClosingEventArgs e.CloseReason양식을 폐쇄하는 이유, 그것은 당신을 말할 것이다. 그런 다음 수행 할 작업을 결정할 수 있으며 가능한 값은 다음과 같습니다.

회원 이름 -설명


없음 -폐쇄 원인이 정의되지 않았거나 확인할 수 없습니다.

WindowsShutDown- 운영 체제가 종료하기 전에 모든 응용 프로그램을 닫습니다.

MdiFormClosing- 이 MDI (다중 문서 인터페이스) 양식의 상위 양식이 닫힙니다 .

UserClosing- 사용자가 사용자 인터페이스 (UI)를 통해 양식을 닫고 있습니다. 예를 들어 양식 창에서 닫기 단추를 클릭하거나 창의 제어 메뉴에서 닫기를 선택하거나 ALT+를 누릅니다 F4.

TaskManagerClosing -Microsoft Windows 작업 관리자가 응용 프로그램을 닫고 있습니다.

FormOwnerClosing- 소유자 양식이 닫힙니다 .

ApplicationExitCall -Application 클래스의 Exit 메서드가 호출되었습니다.


나는 이것이 올바른 방법이라고 믿습니다.

protected override void OnFormClosing(FormClosingEventArgs e)
{
  switch (e.CloseReason)
  {
    case CloseReason.UserClosing:
      e.Cancel = true;
      break;
  }

  base.OnFormClosing(e);
}

응용 프로그램이 자체적으로 닫히는 것을 완전히 방지하는 것은 잘못된 형식으로 간주됩니다. Closing 이벤트에 대한 이벤트 인수를 확인하여 응용 프로그램을 닫도록 요청한 방법과 이유를 확인해야합니다. Windows 종료로 인한 경우 닫기가 발생하지 않도록 방지해서는 안됩니다.


FormClosing이벤트를 처리 하고로 설정할 FormClosingEventArgs.Canceltrue있습니다.


진행률 표시 줄을 표시하는 팝업 대화 상자로 양식을 사용하고 있으며 사용자가 닫을 수 없도록하고 싶습니다.

사용자가 alt + f4를 누를만큼 충분히 지식이있는 앱을 종료하기로 결정했다면 작업 관리자를 실행하고 대신 애플리케이션을 종료 할 수있을만큼 충분히 지식이있을 것입니다.

At least with alt+f4 your app can do a graceful shutdown, rather than just making people kill it. From experience, people killing your app means corrupt config files, broken databases, half-finished tasks that you can't resume, and many other painful things.

At least prompt them with 'are you sure' rather than flat out preventing it.


This is a hack to disable Alt + F4.

private void test_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this.ModifierKeys == Keys.Alt || this.ModifierKeys == Keys.F4) 
    { 
        e.Cancel = true; 
    }    
}

This does the job:

bool myButtonWasClicked = false;
private void Exit_Click(object sender, EventArgs e)
{
  myButtonWasClicked = true;
  Application.Exit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  if (myButtonWasClicked)
  {
    e.Cancel = false;
  }
  else
  {
    e.Cancel = true;
  }


}

Would FormClosing be called even when you're programatically closing the window? If so, you'd probably want to add some code to allow the form to be closed when you're finished with it (instead of always canceling the operation)


Subscribe FormClosing event

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = e.CloseReason == CloseReason.UserClosing;
}

Only one line in the method body.


Hide close button on form by using the following in constructor of the form:

this.ControlBox = false;

참고URL : https://stackoverflow.com/questions/14943/how-to-disable-alt-f4-closing-form

반응형