在面板c中移动图像# [英] Move image inside panel c#

查看:73
本文介绍了在面板c中移动图像#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试在面板控制中移动一个图像。

我有背景图片:

加载事件:

panel1。 BackgroundImage = img;

和图片:

绘制事件:

e.Graphics.DrawImage(img_player,new Point(0,0)) ;



如何从key_down事件发送int变量到paint事件并添加这样的代码

e.Graphics.DrawImage(img_player) ,新点(0 + x,0 + y));如何发送x和y变量?



Hello, i am trying to move an Image inside panel controle.
I have background image:
load event:
panel1.BackgroundImage = img;
and Image:
paint event:
e.Graphics.DrawImage(img_player, new Point(0, 0));

How can i send int variable from key_down event to paint event and add code like this one
e.Graphics.DrawImage(img_player, new Point(0+x, 0+y)); how can i send x and y variable?

private void Form1_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyData == Keys.Up)
           {
               int x = 10;
               int y = 0;

           }
and etc key.down,key.left,key.right same thing just x and y will be different..







img和img_player是全球性的:

图片img = Image.FromFile(@D:\C#\ Lavirint \\ \\ lavirint\images\map1.png);

图片img_player = Image.FromFile(@D:\C#\ Lavirint \ Lavirint \images \ player.png );




img and img_player are global:
Image img = Image.FromFile(@"D:\C#\Lavirint\Lavirint\images\map1.png");
Image img_player = Image.FromFile(@"D:\C#\Lavirint\Lavirint\images\player.png");

推荐答案

使用X和Y作为类的成员在KeyDown事件中更新它们,并使刚刚绘制图像的控件无效。



Use X and Y as member of your class update them in the KeyDown event and invalidate the control that paint the image just after.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
  if(e.KeyData == Keys.Up)
  {
    x=10;
    y=0;
    imgCtrl.Invalidate();
  }
}
private void imgCtrl_OnPaint(object sender, PaintEventArgs e)
{
  e.Graphics.DrawImage(img_player, new Point(x,y));
}





此代码应适应您的目的。



This code should be adapt to your purpose.


这篇关于在面板c中移动图像#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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