'c#'에 해당되는 글 3건

  1. 2007/06/05 동적 어셈블리 로딩
  2. 2007/03/05 커스텀 컨트롤 만들기(코드프로젝트)
  3. 2007/02/13 트레이아이콘
sw2007/06/05 18:53

Dll로 되어 있는 어셈블리를 동적으로 로딩...

System.Reflection.Assembly assm = System.Reflection.Assembly.LoadFile(assmPath.ToString());
Type[] type = assm.GetTypes();           

foreach (Type t in type)
{
                if (t.BaseType.Name.ToString() == "Form")
                {
                    frm = (Form)Activator.CreateInstance(t);
                }
}
           
frm.Show();


Posted by redef
TAG c#

댓글을 달아 주세요

sw2007/03/05 13:27
사용자 삽입 이미지










Introduction

커스텀 컨트롤을 만드는 .NET 프레임워크의 짧고 간단한 데모입니다.
여기서 커스텀 컨트롤을 만들어서 윈도우 어플리케이션에서 컨트롤을 테스트 할것입니다.
컨트롤의 몇가지의 커스텀 프로퍼티를 구현할것입니다, 그래서 c#에서 구현을 어떻게 하는지 배울수 있습니다.

Building the Control

Visual Studio를 열고 새프로젝트를 시작합니다.
프로젝트는 Windows Control Library 템플릿의 기반이어야만 합니다.
ctlCuteButton 으로 프로젝트는 정하고 OK 클릭합니다.

프로젝트가 열리고 난후, 프로젝트에서 UserControl을 삭제합니다.
여기서 필요한게 'User Control'이 아니기 때문에 지워야 합니다.

'Project'메뉴에서 Project->Add User Control...로 가서 Custom Control 템플릿을 선택합니다.
이 예에서 우리가 필요한것이 'Custom Control' 입니다.
그것을 cuteButton으로 해야합니다.
OK 클릭합니다.
새로운 커스텀 컨트롤을 프로젝트에 추가하였습니다.

첫번째로 해야 할일은 cuteButton의 베이스 클래스를 바꿔야 합니다:

아래 줄을 :

public class cuteButton : System.Windows.Forms.Control

이와 같이:

public class cuteButton : System.Windows.Forms.Button

이제 컨트롤은 System.Windows.Forms.Button 클래스 기반입니다.

컨트롤을 위한 커스텀 프로퍼티들은 만들것입니다,
cuteButton 클래스에 다음 코드를 삽입합니다.

private Color m_color1 = Color.LightGreen; //first color
private Color m_color2 = Color.DarkBlue; // second color
private int m_color1Transparent = 64; // transparency degree
// (applies to the 1st color)
private int m_color2Transparent = 64; // transparency degree
// (applies to the 2nd color)

public Color cuteColor1
{
get { return m_color1; }
set { m_color1 = value; Invalidate(); }
}

public Color cuteColor2
{
get { return m_color2; }
set { m_color2 = value; Invalidate(); }
}

public int cuteTransparent1
{
get { return m_color1Transparent; }
set { m_color1Transparent = value; Invalidate(); }
}

public int cuteTransparent2
{
get { return m_color2Transparent; }
set { m_color2Transparent = value; Invalidate(); }
}

Invalidate() 메소드는 뷰나 모든 컨트롤의 내부에서 디자인을 리프레쉬 하곤합니다.
그리고 마지막으로 컨트롤을 컨파일 하기전에 Paint 이벤트를 오버라이드 해야한다.

다음처럼 :

// Calling the base class OnPaint
base.OnPaint(pe);
// Create two semi-transparent colors
Color c1 = Color.FromArgb(m_color1Transparent , m_color1);
Color c2 = Color.FromArgb(m_color2Transparent , m_color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,
c1, c2, 10);
pe.Graphics.FillRectangle (b, ClientRectangle);
b.Dispose();

Ctrl + Shift + B 를 눌러 컨트롤을 컴파일 할수 있습니다.

여기 완전한 cuteButton.cs 파일의 내용이 있습니다

코드 전체:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;


namespace ctlCuteButton
{
///
/// Summary description for cuteButton.
///

public class cuteButton : System.Windows.Forms.Button
{
private Color m_color1 = Color.LightGreen; // first color
private Color m_color2 = Color.DarkBlue; // second color
private int m_color1Transparent = 64; // transparency degree
// (applies to the 1st color)
private int m_color2Transparent = 64; // transparency degree
// (applies to the 2nd color)

public Color cuteColor1
{
get { return m_color1; }
set { m_color1 = value; Invalidate(); }
}

public Color cuteColor2
{
get { return m_color2; }
set { m_color2 = value; Invalidate(); }
}

public int cuteTransparent1
{
get { return m_color1Transparent; }
set { m_color1Transparent = value; Invalidate(); }
}

public int cuteTransparent2
{
get { return m_color2Transparent; }
set { m_color2Transparent = value; Invalidate(); }
}


public cuteButton()
{
}


protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);
// Create two semi-transparent colors
Color c1 = Color.FromArgb
(m_color1Transparent , m_color1);
Color c2 = Color.FromArgb
(m_color2Transparent , m_color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush
(ClientRectangle, c1, c2, 10);
pe.Graphics.FillRectangle (b, ClientRectangle);
b.Dispose();
}
}
}

Testing the Control

VS.NET을 새로 오픈합니다.
Windows Application 템플릿 프로젝트를 만듭니다.

새로운 Windows Form 프로젝트에서 toolbox에 컴파일한 커스텀 컨트롤을 추가할수 있습니다.
toolbox에서 마우스 오른쪽을 클릭해서 Customize Toolbox를 선택하고 .NET Framework Components 탭에서 Browse를 클릭하고 컨트롤 라이브러리 DLL위치를 선택합니다.
Toolbox에 cuteButton 컴포넌트가 나타날것입니다.
프로퍼티를 바꿀수 있습니다.(cuteColor1, cuteColor2, cuteTransparent1, cuteTransparent2)
Posted by redef
TAG c#, Control

댓글을 달아 주세요

sw2007/02/13 11:17

윈폼으로 트레이아이콘을 만들어 보자
우선 윈폼 디자이너에서 NotifyIcon과 ContextMenuStrip을 올려놓는다.
NotifyIcon의 Visible 프로퍼티를 True로 놓고 Icon을 예쁜것으로 골른 후,
ContextMenuStrip을 좀전에 올려놓은 것으로 지정한다.
이제 ContextMenuStrip의 Items 프로퍼티를 지정한다. 이 아이템들이 트레이 아이콘의 트레이상태의 메뉴가 될것들이다.
이제 폼의 Load 이벤트를 등록해 보자...
처음에 프로그램이 실행될때 폼이 나타나지 않게 하기 위해서
this.ShowTaskbar = false;
this.Visible = false;
를 코딩해 주자.
폼이 트레이 아이콘으로 보이고 폼상태로 보이고 하는건 다 이걸로 해준다고 보면 된다.

Strip에 등록한 아이템들의 이벤트와 폼의 최소화 버튼을 클릭했을때 위의 두줄 코딩을 true와 false만 바꿔서 적당하게 바꿔줘 보자.
그리고 참고로 최소화는 Resize 이벤트를 등록해줘야 한다...이것때문에 한참 찾았다..
resize이벤트 등록 후
...
if(this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Visible = false;
}

by Redef( http://www.redef.pe.kr )

Posted by redef
TAG c#

댓글을 달아 주세요