반응형
Window를 상속해 클래스를 정의하는 것도 가능하다.
다음 예제는 세 개의 클래스가 있고, 세 개의 소스 코드 파일이 있다.
[InheritAppAndWindow.cs]
Main에서 MyApplication 타입의 객체를 생성하고, 이 객체의 Run을 호출한다.
using System;
using System.Windows;
using System.Windows.Input;
namespace InheritAppAndWindow
{
public class InheritAppAndWindow
{
[STAThread]
public static void Main()
{
MyApplication app = new MyApplication();
app.Run();
}
}
}
[MyApplication.cs]
OnStartup 메소드를 오버라이딩한 부분에서 MyWindow 타입의 객체를 생성하고 있다.
using System;
using System.Windows;
using System.Windows.Input;
namespace InheritAppAndWindow
{
public class MyApplication : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MyWindow win = new MyWindow();
win.Show();
}
}
}
[MyWindow.cs]
Window를 상속받는 클래스는 일반적으로 생성자에서 그 클래스를 초기화한다. 예제에서는 Title 프로퍼티만 초기화한다. 프로퍼티 이름 앞에 객체의 이름을 따로 쓰지 않았는데, MyWindow가 Window를 상속받기 때문이다.
using System;
using System.Windows;
using System.Windows.Input;
namespace InheritAppAndWindow
{
public class MyWindow : Window
{
public MyWindow()
{
this.Title = "Inherit App & Window";
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
string strMessage = string.Format("Window clicked with {0} button at point ({1})", e.ChangedButton, e.GetPosition(this));
MessageBox.Show(strMessage, Title);
}
}
}
반응형
'C# 개발 > WPF 관련 서적 예제' 카테고리의 다른 글
Charles Petzold의 WPF Chap. 01 - ThrowWindowParty 예제 소스 (0) | 2022.04.24 |
---|---|
Charles Petzold의 WPF Chap. 01 - InheritTheApp 예제 소스 (0) | 2021.11.14 |
Charles Petzold의 WPF Chap. 01 - HandleAnEvent 예제 소스 (0) | 2021.09.04 |
Charles Petzold의 WPF Chap. 01 - SayHello 예제 소스 (0) | 2021.09.04 |
Charles Petzold의 WPF Chap. 17 - ChooseFont 예제 소스 (0) | 2021.09.03 |