WPFでWndProcをオーバーライドするにはどうすればよいですか? 私のウィンドウが閉じると、私が使用しているファイルが変更されているかどうかを確認しようとします。もしそうなら、私はあなたに「変更を保存しますか?メッセージは、使用中のファイルとウィンドウを閉じます。しかし、ウィンドウが開いているときにユーザーが再起動/シャットダウン/ログオフしたときのケースを処理できません。WPFを使用して開発中ですのでWndProcをオーバーライドできません。this sample MSDN code .Thisは私が プライベートボイドloadedForm(オブジェクト送信者、RoutedEventArgs e)のXAMLファイルで {WPFでの処理システムのシャットダウン
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_QUERYENDSESION.)
{
OnWindowClose(this, new CancelEventArgs())
handled = true;
shutdown = true;
}
return IntPtr.Zero;
}
private void OnWindowClose(object sender, CancelEvetArgs e)
{
if (modified)
{
//show message box
//if result is yes/no
e.cancel = false;
//if cancel
e.cancel = true;
}
}
をやったことで、私もClosing = "OnWindowClose"
を使用し、私がはい/いいえ、私のアプリケーションは閉じない]をクリックしたときにしかし何も起こりません。閉じるボタンを使用してもう一度閉じようとすると、エラーが表示されますか?なぜこれはそうですか?それはフックのためですか? WPFでこれに相当するものは何ですか?
private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg==WM_QUERYENDSESSION)
{
systemShutdown = true;
}
// If this is WM_QUERYENDSESSION, the closing event should be
// raised in the base WndProc.
base.WndProc(m);
} //WndProc
private void Form1_Closing(
System.Object sender,
System.ComponentModel.CancelEventArgs e)
{
if (systemShutdown)
// Reset the variable because the user might cancel the
// shutdown.
{
systemShutdown = false;
if (DialogResult.Yes==MessageBox.Show("My application",
"Do you want to save your work before logging off?",
MessageBoxButtons.YesNo))
{
SaveFile();
e.Cancel = false;
}
else{
e.Cancel = true;
}
CloseFile();
}
}
私はWPFであなたが 'SystemEvents.SessionEnding' – mgttlinger