WPF 应用程序在按下鼠标右键时崩溃 [英] WPF application get crashed on pressing right mouse button

查看:30
本文介绍了WPF 应用程序在按下鼠标右键时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在尝试用 c# wpf 制作 Jarvis AI 程序,但我在代码中遇到异常.实际上,问题是在我正在运行的应用程序上按下鼠标右键时,应用程序会卡住并崩溃,任何人都可以帮助我解决这个问题.

I am actually trying to making an Jarvis AI program in c# wpf but i am getting exception in the codes. Actually the problem is when ever in press the right mouse button on my running application, the application gets stuck and crashed can any one can help me in solving this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace jarvis
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }



    }
}

推荐答案

我已经使用了这些代码并且确实有效

I have used these codes and it actually works

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

在你的表单鼠标按下事件中使用 ReleaseCapture 方法像这样

use the ReleaseCapture method in your form mouse down event Like this

if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            } 

这篇关于WPF 应用程序在按下鼠标右键时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆