.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

Click แล้ววาดรูปทำยังไงหรอครับ Options · View
alphokung
Posted: Tuesday, June 17, 2008 1:55:58 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 4/16/2008
Posts: 5
Location: Bkk

ถ้าเกิดว่าต้องการจะทำปั่มขึ้นมาปุ่มนึงซึ่งมี Event ว่าถ้า Click แล้วให้วาดรูป สี่เหลี่ยมขึ้นมานี้จะทำยังไงหรอครับ

 

ตอนนี้ผมาร้าง method ที่เอาไว้วาดรูป สี่เหลี่ยมแล้ว แต่จะไปเรียกใช้มันต้องทำยังไงหรอครับ เพราะตอนทำ Event ของ Onclick มันจะ ใช้ EventArgs แต่การวาดรูปมันต้องใช้ PaintEventArgs แล้วผมจะส่งถ่าไปเรียก Method นั้นยังไงดีหรอครับ

 

ขอบคุนล่วงหน้านะครับ

paedotnet
Posted: Tuesday, June 17, 2008 4:04:03 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk

ลองดูตัวอย่างนะครับ

 

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 WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e) //เมื่อมีการคลิกที่ Button
        {
            Graphics g = this.CreateGraphics();
            MyPaint(g); //เรียกใช้เมธอด
        }
        protected void MyPaint(Graphics g)  //สร้างเมธอดขึ้นมาใหม่โดยรับค่าเป็น object ของคลาส Graphics
        {
           g = this.CreateGraphics();
           g.FillRectangle(Brushes.CadetBlue, new Rectangle(10, 10, 100, 100));
        }
    }
}
 
 
 
ผลลัพธ์
 
รูปก่อนที่จะ Click ที่ Button
 
 
รูปหลังจากที่ Click ที่ Button แล้ว
 

 

และถ้าต้องการส่งค่าอื่นๆเช่น ขนาดและสี ไปให้กับเมธอดเพื่อวาดรูป rectangle ตามค่าที่รับเข้ามาก็เขียนได้ดังนี้

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 WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            MyPaint(g,30,25,115,200,Brushes.Red); //เรียกใช้เมธอดและมีการส่งค่าขนาดของรูป Rectangle และสี Background
        }
        protected void MyPaint(Graphics g,int x1,int y1,int x2,int y2,Brush b)  //สร้างเมธอดขึ้นมาใหม่โดยรับค่าเป็น object ของคลาส Graphics และค่าอื่นๆเช่นขนาดของรูปและสี
        {
           g = this.CreateGraphics();
           g.FillRectangle(b, new Rectangle(x1,y1,x2,y2)); //วาดรูปตามค่าที่รับเข้ามา
        }
    }
}
 
 
ผลลัพธ์
 
รูปก่อนที่จะคลิกที่ Button
 
 
รูปหลังจากที่คลิกที่ Button แล้ว



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by