Sora
Sora

Reputation: 23

C# windows forms MouseDown event not being called

Problem:
I have created a tool to help create levels for a game, the problem occurs when I click in the pictureBox, absolutely nothing happens, I place a breakpoint and it is never run.

pictureBox event settings:

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

Code:

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);
    }
}

Auto generated code:

#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

Please just ask for any other information

Upvotes: 0

Views: 2391

Answers (2)

Rocker
Rocker

Reputation: 96

Windows form used to work like a wallpaper on the wall. If you want to scribble in the wall, you have to tear the wallpaper first. Else you can scribble on the surface of the wallpaper.

Form is like a wall. If you add a Panel, Panel will come above the form. So, the mouse control has to be given for Panel not the Form. Similarly, if you add any Label or GroupBox on the panel, you have to give the mouse control on the label not the Panel. Only if you want your mouse control on specific place.

This is not the problem of C++ code underneath the .Net code.

Upvotes: 2

Sora
Sora

Reputation: 23

Code is still the same except a few changes,

I have added a panel on top of the pictureBox which does the checks for input

This has resolved the problem.

Likely cause: Most probably the CPP code underneath which used HGE to render to the window was absorbing all of the events somehow.

Thanks to all those who responded

Upvotes: 0

Related Questions