'윈폼'에 해당되는 글 2건

  1. 2007.11.26 윈폼 이벤트 다루기 2
  2. 2007.11.26 MDI 폼 예제
Study - Programming/C#2007. 11. 26. 01:10

using System;
using System.Windows.Forms;

class Program : Form
{
    public Program(string strText)
    {
        this.Text = strText;
        this.Load += new System.EventHandler(this.Form_Load);
        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form_Closed);
        this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form_Click);
        this.MouseEnter += new System.EventHandler(this.Form_MouseEnter);
        this.Show();
    }

    public static void Main(string[] args)
    {
        Application.Run(new Program("이벤트!"));
    }

    private void Form_Load(object sender, System.EventArgs e)
    {
        Console.WriteLine("윈도우가 Load됩니다.");
    }
    private void Form_Closed(object sender, System.EventArgs e)
    {
        Console.WriteLine("윈도우가 Closed됩니다.");
    }

    private void Form_Click(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Console.WriteLine(e.Button);
    }

    private void Form_MouseEnter(object sender, System.EventArgs e)
    {
        Console.WriteLine("Mouse Entered!!");
    }

   
}

  실제 실행 결과는 여기서 이벤트가 발생하면 콘솔에 그에 대한 내용이 출력된다. 대표적으로 Load와 Closed이벤트가 있겠다. MouseEnter은 마우스가 폼에 진입했을때 발생하는 이벤트인데, 상당히 흥미로운 이벤트였다. 직접 구현했으면 복잡했을 것을 이렇게 간단히 지원해주니 좋을 따름.
 
  Click이벤트를 보면 두번째 인자가 좀 다른것을 볼 수 있다. System.Windows.Forms.MouseEventArgs인자의 Button속성은 누른 마우스 버튼이 무엇인지를 알려 준다.

  각 이벤트마다 등록해줘야 할 이벤트 등록 클래스가 각각 다르다. 그러므로 중요한것은 따로 암기해 두고, 필요할때는 MSDN 등을 통해 검색해 보고 사용해야 할 것이다. 예를 들면 Load와 FormClosed이벤트의 등록 클래스의 형태는 다르다.

  이벤트의 등록 방법은 다음과 같다.

[이벤트 이름] +- new System.EventHandler(메서드);

  이벤트의 제거는 -=를 이용해 주면 되고, 메서드는 알아서 만들어서 넣어 주면 되지만 그 형태는 다음과 같다.

private void 메서드이름(object sender,System.EventArgs e){}

물론 EventArgs는 MouseClick이벤트를 보듯이 다를 수도 있지만 대개 저런 형태이다.

sender를 다룰때는 어떤 특정한 컨트롤일 경우에는 그 컨트롤로 캐스팅을 시켜주는 경우가 많다. 예를 들어 앞 글에서 버튼을 다룰때 이런식의 코드가 있었다.

(button)sender

이렇게 object를 button으로 캐스팅을 하고 사용해 줘야 한다.

Posted by 머리
Study - Programming/C#2007. 11. 26. 01:00

사용자 삽입 이미지


using System;
using System.Windows.Forms;

class MyClass : Form
{
    Button[] btn = new Button[4];
    Form[] newMDICHild = new Form[10];
    string[] strData = { "수평", "수직", "계단식", "아이콘" };

    public MyClass(string strText)
    {
        this.Text = strText;
        this.IsMdiContainer = true;
        this.Load += new EventHandler(this.Form_Load);
        this.Closed += new EventHandler(this.Form_Closed);

        for (int i = 0; i < 4; i++)
        {
            btn[i] = new Button();
            btn[i].Text = strData[i];
            btn[i].SetBounds(50 * i, 10, 50, 50);
            btn[i].Click += new EventHandler(this.Btn_Click);
            this.Controls.Add(btn[i]);
        }

        this.Show();
    }

    public static void Main(string[] args)
    {
        Application.Run(new MyClass("MDI"));
    }

    private void Form_Load(object sender, System.EventArgs e)
    {
        Console.WriteLine("윈도우에 자식창을 생성");
        Form[] newMDIChild = new Form[10];
        for(int i = 0 ;i < 10 ; i++)
        {
            newMDIChild[i] = new Form();
            newMDIChild[i].Text = i + "번째 자식창";
            newMDIChild[i].MdiParent=this;
            newMDIChild[i].Closed += new System.EventHandler(this.Form_Closed);

            newMDIChild[i].Show();
        }
    }
    private void Form_Closed(object sender, System.EventArgs e)
    {
        Console.WriteLine(((Form)sender).Text + "윈도우가 Closed됩니다.");
    }

    private void Btn_Click(object sender, System.EventArgs e)
    {
        if ((Button)sender == btn[0])
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
            this.Text = "수평 바둑판 정렬";
        }
        else if ((Button)sender == btn[1])
        {
            this.LayoutMdi(MdiLayout.TileVertical);
            this.Text = "수직 바둑판 정렬";
        }
        else if ((Button)sender == btn[2])
        {
            this.LayoutMdi(MdiLayout.Cascade);
            this.Text = "계단식 정렬";
        }
        else if ((Button)sender == btn[3])
        {
            this.LayoutMdi(MdiLayout.ArrangeIcons);
            this.Text = "아이콘 정렬";
        }
    }
}

  후.. 요즘 윈도우 프로그래밍에 맛들여서.. 이런거 공부해 보는게 마냥 재미있네. 어쨋든 슬럼프였는데 갈길을 찾아서 다행이다. 시험 기간 제대로 돌입하기전에는 열심히 해봐야지.

  C#문법을 조금만 아는 사람이라면 금방 파악할 수 있을 것이다. 그만큼 쉽고 편리하게 구현하는게 가능하다. 실제 윈폼 구현은 다 IDE를 이용해서 하지만 콘솔로도 이렇게 간단하게 할 수가 있다. 특히 이벤트 구현을 저렇게 쉽게 해줄 수 있다니.. MFC/API를 사용할때와 비교해서 이렇게 차이가 난다. VB를 해보았다면 그때의 기억을 살리면서 신나게 해볼 수 있을 것이다. 아 앞으로 배울 것들이 엄청 기대된다.

Posted by 머리