如何检测动态绘制图形的点击? [英] How to detect a click of a dynamically drawn graphic?

查看:19
本文介绍了如何检测动态绘制图形的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制文件列表面板上的文件夹名称,我正在尝试集思广益检测用户是否以及何时单击文件/文件夹名称以及他们实际单击的文件或文件夹名称的最佳方法.

I am drawing a list of file & folder names on a panel and I'm trying to brainstorm the best way to detect if and when a user clicks a file/folder name, as well as what file or folder name they actually clicked.

以下是我目前编写的方法.我的第一个想法是用透明控件搭载每段文本,并以这种方式动态连接 onclick 事件.但这似乎是一种资源浪费.

Below are the methods I wrote so far. My first thought was to piggy back each piece of text with a transparent control and dynamically wire up an onclick event that way. But it seems like a waste of resources.

private void DisplayFolderContents(ListBox lb, string sPath)
    {
        lblPath.Text = sPath;
        const float iPointX = 01.0f;
        float iPointY = 20.0f;
        DirectoryContents = FileSystem.RetrieveDirectoriesAndFiles(sPath, true, true, "*.mp3");

        foreach (string str in DirectoryContents)
        {
            DrawString(FileSystem.ReturnFolderFromPath(str), iPointX, iPointY, 21, panListing);


            iPointY += 50;
        }
    }


private void DrawString(string textToDraw, float xCoordinate, float yCoordinate, int fontSize, Control controlToDrawOn)
    {

        Graphics formGraphics = controlToDrawOn.CreateGraphics();
        formGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        Font drawFont = new Font(
                "Arial", fontSize, FontStyle.Bold);

        SolidBrush drawBrush = new
                SolidBrush(Color.White);

        formGraphics.DrawString(textToDraw, drawFont, drawBrush, xCoordinate, yCoordinate);

        drawFont.Dispose();
        drawBrush.Dispose();
        formGraphics.Dispose();
    }

谢谢,凯文

推荐答案

首先,保留在面板上绘制的每个字符串或对象及其位置和大小的列表.

First of all, keep a list of every string or object drawn on your panel with their location and size.

之后,处理事件 MouseDown 或 MouseUp(取决于您想要的行为)

After that, handle the event MouseDown or MouseUp (depending of the behavior you want)

List<YourObject> m_list; //The list of objects drawn in the panel.

private void OnMouseDown(object sender, MouseEventArgs e)
{
    foreach(YourObject obj in m_list)
    {
        if(obj.IsHit(e.X, e.Y))
        {
            //Do Something
        }
    }
}

在类 YourObject 中实现函数 IsHit:

In the class YourObject implements the function IsHit:

public class YourObject
{

    public Point Location { get; set; }
    public Size Size {get; set; }

    public bool IsHit(int x, int y)
    {
        Rectangle rc = new Rectangle(this.Location, this.Size);
        return rc.Contains(x, y);
    }
}

没有必要每次都创建矩形,你可以有一个类变量来保存这些信息.当位置或大小发生变化时更新矩形很重要.

It is not necessary to create the rectangle every time, you could have a class variable to keep this information. It is just important to update your rectangle when the location or the size change.

这篇关于如何检测动态绘制图形的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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