在面板上绘制X和Y轴 [英] Draw X and Y axes on the panel

查看:77
本文介绍了在面板上绘制X和Y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我想在面板上绘制X和Y轴.我想使用图形类.任何人都可以举一个例子.


谢谢

Hello,


I want to draw X and Y axes on a panel. I want to use graphics class. Can anyone give an example for the same.


Thank You

推荐答案

处理Panel.Paint事件并从PaintEventArgs参数获取Graphics对象.
然后,您可以使用Graphics.DrawLine方法绘制轴-使用面板的Width和Height属性,为每条线创建所需的两个点应该很简单,并以少量偏移以留出边距.如果需要,可以使用Graphics.DrawString方法标记轴.
Handle the Panel.Paint event and get the Graphics object from the PaintEventArgs parameter.
You can then use the Graphics.DrawLine method to draw your axes - it should be simple to create the two points you need for each line, using the Width and Height properties of the panel, offset by a small amount to leave a margin. You can if you need use the Graphics.DrawString method to label your axes.


您好,
这是一个简单的Draw子例程,可以完成您需要的操作,并且可以在任何Windows窗体应用程序中使用,必须在窗体上具有一个Panel控件,并将Panel的背景色设置为White,然后将ock属性设置为fill或dock到父容器.
如果您分析代码,则应使用Graphics类学习一些绘图基础知识.
尝试阅读有关图形类的MSDN,以了解更多信息.

MSDN图形类链接 [
Hello,
here is simple Draw subroutine that do what you asked for, and can be used in any windows form app, it is necessary to have one Panel control on your form, and set background color of panel to White, and dock property to fill or dock to parent container.
If you analize code you shall learn some basics of drawing by using Graphics class.
Try reading MSDN about Graphics class to learn much more.

MSDN Graphics Class Link[^]

All the best,
Perić Željko

void Draw()
{
	//
	// Declaration of variables
	//
				
		Graphics table;
					
		Pen pencil;
					
		Brush brush;
						
		Font font;
					
		Color rubber;
					
		PointF start_line;
		PointF end_line;
					
		int panel_height;
		int panel_width;
					
	//
	//
	//
			
			
 //
 // Set new graphics that uses panel as drawing surface
 //
 table = panel1.CreateGraphics();
 //
 // Set new PointF(x,y),  start line point 
 // 
 start_line = new PointF();
 start_line.X = 0;
 start_line.Y = 0;
 //
 // Set new PointF(x,y) end line point
 // 
 end_line = new PointF();
 end_line.X = 0;
 end_line.Y = 0;
 //
 // Set new Pen, 
 // pencil that will be used for 
 // drawing with Red color and 2 pixels tick
 //
 pencil = new Pen(Color.Red,2);
 //
 // Set new Brush
 // brush that will be used for drawing letters
 //
 brush = new SolidBrush(Color.Black);
 //
 // Set new Font
 // font that shall be used for drawing strings
 //
 font = new Font("LucidaConsole",12,FontStyle.Bold);
 //
 // Set new Color, 
 // rubber that will be used for erasing table
 //
 rubber = new Color();
 rubber = Color.White;
 //
 // Get Panel height and width
 //
 panel_height = panel1.Height;
 panel_width = panel1.Width;
 //
 // Clear table surface with rubber, 
 // rubber contains value of collor 
 // and it must be the same as panel1 background color
 //
 table.Clear(rubber);
 //
 // Set start and end of X oordinate line
 //
 start_line.X = 0;
 start_line.Y = panel_height-20;
			
 end_line.X = panel_width;
 end_line.Y = start_line.Y;
 //
 // draw X oordinate
 //
 table.DrawLine(pencil,start_line,end_line);
 //
 // draw "x" letter at the end of oordinate
 //
 end_line.X-=20;// decrement x by 20
 table.DrawString("x",font,brush,end_line);
 //
 // Set start and end of Y oordinate line
 //
 start_line.X = 20;
 start_line.Y = 0;
			
 end_line.X = start_line.X;
 end_line.Y = panel_width;
 //
 // draw Y oordinate
 //
 table.DrawLine(pencil,start_line,end_line);
 //
 // draw "y" letter at the end of oordinate
 //
 table.DrawString("y",font,brush,start_line);
 //
 // arrows are up to you to draw it
 //
}


您好试试这个,

Hello try this one,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{


    public class ImagePanel : Panel
    {
        private Image image;
        private Panel panel1;

        public Image Image
        {
            get { return image; }
            set
            {

                image = value;
                Refresh();
            }
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(200, 100);
            this.panel1.TabIndex = 0;
            this.ResumeLayout(false);

        }
    }


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ImagePanel obj = new ImagePanel();

        private void Form1_Load(object sender, EventArgs e)
        {
            obj.Image = Image.FromFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\nike.jpg");
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (obj.Image != null)
            {
                e.Graphics.DrawImage(obj.Image, new Point(100,100));
            }
            base.OnPaint(e);
        }
    }
}



否则请像这样使用:

听起来您还没有挂上面板的绘画事件:



Otherwise use like this:

It sounds like you haven''t hooked up the paint event of the panel:

panelArea.Paint += new PaintEventHandler(panelArea_Paint);



如果panelArea是表单的名称,则只需将其更改为您的面板即可:



If panelArea is the name of your form, then just change it to your panel:

panel1.Paint += new PaintEventHandler(panel1_Paint);



然后将绘画逻辑移动到该方法:



and then move your painting logic to that method:

private void panel1_Paint(object sender, PaintEventArgs e) {
  // the rest of your drawing
}







问候
sarva







regards
sarva


这篇关于在面板上绘制X和Y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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