C# windows 窗体 MouseDown 事件未被调用

问题描述 投票:0回答:2

问题
我创建了一个工具来帮助为游戏创建关卡,当我单击图片框时出现问题,完全没有任何反应,我放置了一个断点,但它永远不会运行。

pictureBox 事件设置:

http://postimg.org/image/hllv4vwjz/

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Level_Editor
{
    public partial class tileWindow : Form
    {
        LevelControls myLevelControls;

        public float myStartIndex_X;
        public float myStartIndex_Y;
        public float myEndIndex_X;
        public float myEndIndex_Y;

        public tileWindow()
        {
            InitializeComponent();

            CPPFuncs.CreateNewWindow(displayWindow.Handle, 20, 20);
            CPPFuncs.AddTileSprite("Data/Tiles/blank.dds", "blank");
            Application.Idle += AppIdle;

            myLevelControls = new LevelControls();
            myLevelControls.AddParent(this);
            myLevelControls.Show();
        }

        private void AppIdle(object Sender, EventArgs e)
        {
            CPPFuncs.Render();
            this.Invalidate();
        }

        private void quitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void displayWindow_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            CPPFuncs.Input((short)e.KeyCode);
        }

        private void displayWindow_MouseDown(object sender, MouseEventArgs e)
        {
            myStartIndex_X = e.X;
            myStartIndex_Y = e.Y;
        }

        private void displayWindow_MouseUp(object sender, MouseEventArgs e)
        {
            myEndIndex_X = e.X;
            myEndIndex_Y = e.Y;

            if (myLevelControls.myCurrentSelectedTile != "")
            {
                CPPFuncs.SetTiles(myStartIndex_X, myStartIndex_Y, myEndIndex_X, myEndIndex_Y, myLevelControls.myCurrentSelectedTile);
            }
        }

    }

    static class CPPFuncs
    {
        const string EngineDLLName = "HGE Handle.dll";

        [DllImport(EngineDLLName, CallingConvention = CallingConvention.Cdecl)]
        public extern static unsafe int CreateNewWindow(IntPtr aHwnd, int aTileWidthCount, int aTileHeightCount);

        [DllImport(EngineDLLName, CallingConvention = CallingConvention.Cdecl)]
        public extern static unsafe int AddTileSprite(string aSprite, string aFileName);

        [DllImport(EngineDLLName, CallingConvention = CallingConvention.Cdecl)]
        public extern static unsafe int LoadLevel(string aDir);

        [DllImport(EngineDLLName, CallingConvention = CallingConvention.Cdecl)]
        public extern static unsafe int MoveCamera(float x, float y);

        [DllImport(EngineDLLName, CallingConvention = CallingConvention.Cdecl)]
        public extern static unsafe int Input(short aKey);

        [DllImport(EngineDLLName, CallingConvention = CallingConvention.Cdecl)]
        public extern static unsafe int SetTiles(float aStartX, float aStartY, float anEndX, float anEndY, string aTileSprite);

        [DllImport(EngineDLLName, CallingConvention = CallingConvention.Cdecl)]
        public extern static unsafe int Render();

        [DllImport(EngineDLLName, CallingConvention = CallingConvention.Cdecl)]
        public extern static unsafe int ScrollTiles(bool aMoveUpFlag);
    }
}

自动生成代码:

#region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.displayWindow = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.displayWindow)).BeginInit();
        this.SuspendLayout();
        // 
        // displayWindow
        // 
        this.displayWindow.Location = new System.Drawing.Point(-6, -2);
        this.displayWindow.Name = "displayWindow";
        this.displayWindow.Size = new System.Drawing.Size(1020, 788);
        this.displayWindow.TabIndex = 0;
        this.displayWindow.TabStop = false;
        this.displayWindow.MouseDown += new System.Windows.Forms.MouseEventHandler(this.displayWindow_MouseDown);
        this.displayWindow.MouseUp += new System.Windows.Forms.MouseEventHandler(this.displayWindow_MouseUp);
        this.displayWindow.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.displayWindow_PreviewKeyDown);
        // 
        // tileWindow
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.Lavender;
        this.ClientSize = new System.Drawing.Size(1011, 783);
        this.Controls.Add(this.displayWindow);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "tileWindow";
        this.Text = "TBS Level Editor";
        ((System.ComponentModel.ISupportInitialize)(this.displayWindow)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

请询问任何其他信息

c# picturebox mousedown mouseup
2个回答
0
投票

除了一些更改外,代码仍然相同,

我在图片框顶部添加了一个面板,用于检查输入

这已经解决了问题。

可能原因: 最有可能的是使用 HGE 渲染到窗口的 CPP 代码以某种方式吸收了所有事件。

感谢所有回复的人


0
投票

Windows 窗体过去的作用就像墙上的壁纸。如果你想在墙上乱写乱画,你必须先把壁纸撕掉。或者你可以在壁纸表面乱涂乱画。

形式就像一堵墙。如果添加面板,面板将位于表单上方。因此,必须为面板而不是窗体提供鼠标控制。同样,如果您在面板上添加任何标签或 GroupBox,则必须将鼠标控制权交给标签而不是面板。仅当您希望鼠标控制在特定位置时。

这不是.Net代码下面的C++代码的问题。

© www.soinside.com 2019 - 2024. All rights reserved.