使用C Sharp的动态图形 [英] Dynamic Graphics Using C Sharp

查看:92
本文介绍了使用C Sharp的动态图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
我正在做一个需要在其中创建视觉动态点的项目.要求当我单击该点时,它会从一个简单的矩形点变成一个填充的矩形点,并且当我拖动它时,它必须动态更改其位置.我必须使用C Sharp严格执行所有这些操作.我不能使用WPF.目前,我正在使用Windows.System.Drawing实用程序,但我不知道如何进一步.请帮我.任何支持将不胜感激.
谢谢

Hello Friends,
I am working on a project where in I need to create a visual dynamic point. It is required that when I click on the point it turns from a simple rectangular point to a filled rectangular point and when I drag it, it changes its position dynamically. I am required to do all this strictly in C sharp. I cannot use WPF. Currently I am using Windows.System.Drawing utility but I don''t know how to move further. Please help me. Any support will be appreciated.
Thank You

推荐答案

那肯定是可能的.我正在写的应用程序中正在做类似的事情,我将尝试制定一个您可以实际使用的答案.

就我而言,我创建了一个从Panel控件派生的自定义控件.然后,我覆盖了Paint方法,并在代码中放入了在指定位置绘制图像的代码,并添加了MouseMove方法来实际更改指定位置的代码.这是我的代码中存在的大多数Mouse处理程序代码(请记住,您将不得不对其进行按摩以满足自己的需求,尤其是绘画部分):

That''s certainly possible. I''m doing something like that in an app I''m writing, and I''ll try to formulate an answer you can actually use.

In my case, I created a custom control derived from the Panel control. I then overrode the Paint method, and put code in to draw an image in the specified location, and the MouseMove method to actually change the specified position. Here''s most of the Mouse handler code as it exists in my code (keep in mind that you WILL have to massage it to fit your own needs, especially the painting part):

//--------------------------------------------------------------------------------
protected override void OnMouseDown(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && m_weapon != null)
    {
        m_moving = true;
    }
    base.OnMouseDown(e);
}
 
//--------------------------------------------------------------------------------
protected override void OnMouseEnter(EventArgs e)
{
    if (m_weapon != null)
    {
        Cursor = Cursors.SizeAll;
    }
    base.OnMouseEnter(e);
}

//--------------------------------------------------------------------------------
protected override void OnMouseLeave(EventArgs e)
{
    Cursor = Cursors.Default;
    base.OnMouseLeave(e);
}

//--------------------------------------------------------------------------------
protected override void OnMouseUp(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        m_moving = false;
    }
    base.OnMouseUp(e);
}

//--------------------------------------------------------------------------------
protected override void OnMouseMove(MouseEventArgs e)
{
    if (m_moving)
    {
        m_origin.X = e.Location.X - m_offset.X;
        m_origin.Y = e.Location.Y - m_offset.Y;
        m_bmpRect.X = m_origin.X;
        m_bmpRect.Y = m_origin.Y;
        Refresh();
    }
    else
    {
        m_offset.X = e.Location.X - m_bmpRect.X;
        m_offset.Y = e.Location.Y - m_bmpRect.Y;
    }
    base.OnMouseMove(e);
}



适用的油漆处理程序代码如下所示:



The applicable paint hander code looks like this:

//--------------------------------------------------------------------------------
protected override void OnPaint(PaintEventArgs e)
{
    Rectangle imgRect = new Rectangle(m_origin.X, m_origin.Y, m_weapon.Width, m_weapon.Height);
    e.Graphics.DrawImage(m_weapon, imgRect, 0, 0, m_weapon.Width, m_weapon.Height, GraphicsUnit.Pixel, imageAttributes);
    base.OnPaint(e);
}



顺便说一句,Google是一个很好的资源.



Google is a great resource, btw.


问题尚不清楚,但是不用担心:您只需要了解使用System.Windows.Forms.

我解释了好几次.请在这里查看我过去的解决方案:

Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在mdi子窗体之间画线 [在面板上捕获图形 [如何清除旧面板绘图 [ ^ ].

祝你好运,
—SA
The question is not clear, but don''t worry: you just need to understand basic principles of making interactive graphics using System.Windows.Forms and System.Drawing.

I explained them several times. Please see my past solutions here:

What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

See also How do I clear a panel from old drawing[^].

Good luck,
—SA


这篇关于使用C Sharp的动态图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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