如何使图片框可选? [英] How do i make a picturebox selectable?

查看:39
本文介绍了如何使图片框可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个非常基本的地图编辑器.我已经完成了一半,我遇到的一个问题是如何删除一个对象.

I am making a very basic map editor. I'm halfway through it and one problem i hit is how to delete an object.

我想按删除键,但图片框似乎没有 keydown 事件,看起来我只会在我的列表框上有它.

I would like to press delete but there appears to be no keydown event for pictureboxes and it will seem like i will have it only on my listbox.

在我的编辑器中删除对象的最佳解决方案是什么?

What is the best solution for deleting an object in my editor?

推荐答案

您需要 PictureBox 参与 Tab 键顺序并显示它具有焦点.这需要一些小手术.向您的项目添加一个新类并粘贴如下所示的代码.编译.将新控件从工具箱顶部拖放到表单上.实现 KeyDown 事件.

You'll want the PictureBox to participate in the tabbing order and show that it has the focus. That takes a bit of minor surgery. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Implement the KeyDown event.

using System;
using System.Drawing;
using System.Windows.Forms;

class SelectablePictureBox : PictureBox {
  public SelectablePictureBox() {
    this.SetStyle(ControlStyles.Selectable, true);
    this.TabStop = true;
  }
  protected override void OnMouseDown(MouseEventArgs e) {
    this.Focus();
    base.OnMouseDown(e);
  }
  protected override void OnEnter(EventArgs e) {
    this.Invalidate();
    base.OnEnter(e);
  }
  protected override void OnLeave(EventArgs e) {
    this.Invalidate();
    base.OnLeave(e);
  }
  protected override void OnPaint(PaintEventArgs pe) {
    base.OnPaint(pe);
    if (this.Focused) {
      var rc = this.ClientRectangle;
      rc.Inflate(-2, -2);
      ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
    }
  }
}

这篇关于如何使图片框可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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