2019년 4월 5일 금요일

C# - 이미지 리스트 박스 (ImageListBox)



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

namespace CustomControls
{
/// <summary>
/// 이미지 리스트 박스
/// </summary>
public partial class ImageListBox : ListBox
{
private readonly Font normalFont = new Font("돋움", 9F, (FontStyle)(FontStyle.Regular), GraphicsUnit.Point, ((byte)(129)));
private readonly Font selectFont = new Font("돋움", 9F, (FontStyle)(FontStyle.Bold), GraphicsUnit.Point, ((byte)(129)));
private readonly Color normalColor = Color.FromArgb(72, 72, 72);
private readonly Color selectColor = Color.FromArgb(227, 63, 2);

public ImageListBox()
{
// 이 호출은 Windows.Forms Form 디자이너에 필요합니다.
InitializeComponent();
this.DrawMode = DrawMode.OwnerDrawVariable;
}

protected override void OnPaint(PaintEventArgs pe)
{
// TODO: 사용자 지정 그리기 코드를 여기에 추가합니다.
// 기본 클래스 OnPaint를 호출하고 있습니다.
base.OnPaint(pe);
}

/// <summary>
/// 그려질 Item에 대한 처리를 담당하는 메서드
/// Item이 그려질때마다 이 메서드가 호출이 되게 된다.
/// </summary>
protected override void OnDrawItem(DrawItemEventArgs e)
{
// 만약 Item이 아무것도 없다면 바로 빠져나간다.
if (this.Items.Count == 0)
{
base.OnDrawItem(e);
return;
}

// OptionListItem 객체를 얻어온다
ImageListBoxItem item = (ImageListBoxItem)this.Items[e.Index];

// 비트맵을 제작한다.
Bitmap bmOffscreen = new Bitmap(e.Bounds.Width, e.Bounds.Height);

// 비트맵으로 부터 Graphics객체를 얻어내고
Graphics gfx = Graphics.FromImage(bmOffscreen);

// 현재 그릴 Item이 선택된 상태인지를 얻어온다.
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);

// 시스템 브러쉬를 선언
Brush brBackground = SystemBrushes.Window;

// 그려질 영역을 선언
Rectangle bounds = new Rectangle(0, 0, e.Bounds.Width, e.Bounds.Height);

// Text를 쓸 브러쉬를 선언
//Brush itemTextBrush = SystemBrushes.ControlText;

/*
if(selected)
{
// 만약 현재 그릴 Item이 선택된 Item이라면
// 텍스트는 선택반전된 색을 선택
itemTextBrush = SystemBrushes.HighlightText;

// 배경을 그릴 브러쉬 색은 선택된 배경색의 Alpha값을 128로 주어서 약간 흐릿하게 만든다.
brBackground = new SolidBrush(Color.FromArgb(128, SystemColors.Highlight));

// 배경을 칠하고
gfx.FillRectangle(brBackground, bounds);

// 선택된 포커스 상자 (점선 상자)를 그린다.
e.DrawFocusRectangle();
}
else
{
// 선택된 Item이 아니라면
// 원래의 배경색을 선택
brBackground = new SolidBrush(this.BackColor);

// 배경색을 칠한다.
gfx.FillRectangle(brBackground, bounds);
}

// (20, 5) 위치에 글자를 텍스트 브러쉬로 그린다.
//gfx.DrawString(item.Text, item.Font, itemTextBrush, 20.0F, 5.0F);

// 만약 Icon이 null이 아닌경우에 Icon을 그려준다.
//if (item.Icon != null)
////gfx.DrawIcon(item.Icon, 1, (e.Bounds.Height - item.Icon.Height) / 2);
//gfx.DrawIcon(item.Icon, new Rectangle(1, 2, 16, 16));
*/



// 선택된 Item이 아니라면 원래의 배경색을 선택하여 배경색을 칠한다.
gfx.FillRectangle(new SolidBrush(this.BackColor), bounds);

if(item.Bitmap != null)
{
if(selected)
gfx.DrawString(item.Text, selectFont, new SolidBrush(selectColor), item.Bitmap.Width + 1, GetYLocation(item.Bitmap.Width));
else
gfx.DrawString(item.Text, normalFont, new SolidBrush(normalColor), item.Bitmap.Width + 1, GetYLocation(item.Bitmap.Width));

gfx.DrawImage(item.Bitmap, new Rectangle(1, 2, item.Bitmap.Width, item.Bitmap.Height));
}
else if(item.Icon != null)
{
if(selected)
gfx.DrawString(item.Text, selectFont, new SolidBrush(selectColor), item.Icon.Width + 1, GetYLocation(item.Icon.Width));
else
gfx.DrawString(item.Text, normalFont, new SolidBrush(normalColor), item.Icon.Width + 1, GetYLocation(item.Icon.Width));
gfx.DrawIcon(item.Icon, new Rectangle(1, 2, item.Icon.Width, item.Icon.Height));
}

// 그려진 비트맵을 그리고자 하는 위치에 사이징 하지 않고 그대로 그려준다.
e.Graphics.DrawImageUnscaled(bmOffscreen, e.Bounds.X, e.Bounds.Y);

bmOffscreen.Dispose();
}

private int GetYLocation(int height)
{
if(1 <= height && height <= 19)
return 5;
else if(20 <= height && height <= 29)
return 8;
else if(30 <= height && height <= 39)
return 11;
else if(40 <= height)
return 13;
else
return 0;
}
}


public class ImageListBoxItem
{
private Icon icon = null;
private Bitmap bitmap = null;
private string text = "";

public ImageListBoxItem(string text)
{
this.Text = text;
}

public ImageListBoxItem(string text, Icon icon) : this(text)
{
this.Icon = icon;
}

public ImageListBoxItem(string text, Bitmap bitmap) : this(text)
{
this.Bitmap = bitmap;
}

/// <summary>
/// 보여줄 아이콘
/// </summary>
public Icon Icon
{
get { return this.icon; }
set { this.icon = value; }
}

/// <summary>
/// 보여줄 아이콘
/// </summary>
public Bitmap Bitmap
{
get { return this.bitmap; }
set { this.bitmap = value; }
}

/// <summary>
/// 보여줄 텍스트
/// </summary>
public string Text
{
get { return this.text; }
set { this.text = value; }
}
}

}


댓글 없음:

댓글 쓰기

javascript - SQL 예약어 제거

  <script language="javascript"> //특수문자, 특정문자열(sql예약어) 제거 function checkSearchedWord(obj){ obj.value = obj.value+&quo...