c# 폼디자인, 컨트롤박스 만들기
C#/Tip :
2009. 9. 9. 19:57
업무일지 프로그램을 만드는 과정에서 트레이 버튼을 추가하려다가
제공되는 컨트롤박스에서는 트레이 버튼을 추가할 수 없어서 만들어 봤습니다.
응용해서 여러가지 만들 수 있을 거 같아서 남겨 놓습니다.
001 | using System; |
002 | using System.Collections.Generic; |
003 | using System.ComponentModel; |
004 | using System.Data; |
005 | using System.Drawing; |
006 | using System.Text; |
007 | using System.Windows.Forms; |
008 | using System.Drawing.Drawing2D; |
009 |
010 | namespace WindowsApplication7 |
011 | { |
012 | public partial class Form1 : Form |
013 | { |
014 |
015 | //반투명 알파값 |
016 | private int percentAlpha = 70; |
017 | //반투명 색상 |
018 | private Color pb = new Color(); |
019 | //그라데이션 브러쉬 |
020 | private LinearGradientBrush lineGBrush; |
021 | //마우스로 폼 드래그 하기 |
022 | private Point mCurrentPosition = new Point(0, 0); |
023 | //마우스 드래그시 상단 타이틀인지 체크 |
024 | private bool titleMove = false ; |
025 |
026 | public Form1() |
027 | { |
028 | InitializeComponent(); |
029 | Design_Setting(); |
030 | } |
031 |
032 | private void Design_Setting() |
033 | { |
034 | //컨트롤박스 제거, 키프리뷰 설정, 하단그립 제거, 폼테두리 변경(FixedSingle), 더블버퍼링 설정 |
035 | //스타일 변경 |
036 | this .SetStyle(ControlStyles.UserPaint, true ); |
037 | this .SetStyle(ControlStyles.AllPaintingInWmPaint, true ); |
038 | this .SetStyle(ControlStyles.ResizeRedraw, true ); |
039 |
040 | //색상 설정 |
041 | pb = Color.FromArgb(percentAlpha * 255 / 100, Color.FromArgb(0, 128, 255)); |
042 |
043 | //그라데이션 설정 |
044 | lineGBrush = new LinearGradientBrush( new PointF(0, 0), new PointF( this .Width, 0), Color.SkyBlue, pb); |
045 |
046 | //라인브러쉬 블랜드 적용 |
047 | float [] relativelntensities = { 0f, 0.5f, 1.0f }; |
048 | float [] reltivePositions = { 0.0f, 0.1f, 1.0f }; |
049 | Blend blend = new Blend(); |
050 | blend.Factors = relativelntensities; |
051 | blend.Positions = reltivePositions; |
052 | lineGBrush.Blend = blend; |
053 | } |
054 |
055 | private void Form1_Paint( object sender, PaintEventArgs e) |
056 | { |
057 | //폼 배경을 흰색으로 설정 |
058 | e.Graphics.Clear(Color.Snow); |
059 |
060 | // 브러쉬 설정 |
061 | SolidBrush sBrush = new SolidBrush(Color.FromArgb(127, 50, 0)); |
062 | sBrush.Color = Color.White; |
063 |
064 | //화면 그리기 |
065 | Image memimage = new Bitmap( this .Width, this .Height); |
066 | Graphics g = Graphics.FromImage(memimage); |
067 |
068 | string title = "http://redreans.tistory.com" ; |
069 | g.FillRectangle( this .lineGBrush, 0, 0, this .Width, 20); |
070 | g.DrawString(title, this .Font, sBrush, 5F, 5F); |
071 |
072 | //화면으로 표현 |
073 | e.Graphics.DrawImage(memimage, ClientRectangle); |
074 |
075 | g.Dispose(); |
076 | memimage.Dispose(); |
077 | sBrush.Dispose(); |
078 | } |
079 |
080 | private void Form1_MouseDown( object sender, MouseEventArgs e) |
081 | { |
082 | if (e.Button == MouseButtons.Left && e.Y < 20) |
083 | { |
084 | mCurrentPosition = new Point(-e.X, -e.Y); |
085 | this .titleMove = true ; |
086 | } |
087 | } |
088 |
089 | private void Form1_MouseMove( object sender, MouseEventArgs e) |
090 | { |
091 | if (e.Button == MouseButtons.Left && this .titleMove == true ) |
092 | { |
093 | this .Location = new Point( |
094 | this .Location.X + (mCurrentPosition.X + e.X), |
095 | this .Location.Y + (mCurrentPosition.Y + e.Y)); // 마우스의 이동치를 Form Location에 반영한다. |
096 | } |
097 |
098 | } |
099 |
100 | private void Form1_MouseUp( object sender, MouseEventArgs e) |
101 | { |
102 | /* 마우스로 폼 드래그 */ |
103 | if (e.Button == MouseButtons.Left && this .titleMove == true ) |
104 | { |
105 | this .Location = new Point( |
106 | this .Location.X + (mCurrentPosition.X + e.X), |
107 | this .Location.Y + (mCurrentPosition.Y + e.Y)); // 마우스의 이동치를 Form Location에 반영한다. |
108 | this .titleMove = false ; |
109 | } |
110 | } |
111 |
112 | private void button1_Click( object sender, EventArgs e) |
113 | { |
114 | this .Close(); |
115 | } |
116 |
117 | private void button1_Paint( object sender, PaintEventArgs e) |
118 | { |
119 | e.Graphics.DrawLine( new Pen(Color.FromArgb(128, 64, 0), 1), new PointF(1.5F, 1.5F), new PointF( this .button1.Width - 1.5F, this .button1.Height - 1.5F)); |
120 | e.Graphics.DrawLine( new Pen(Color.FromArgb(128, 64, 0), 1), new PointF(1.5F, this .button1.Height - 1.5F), new PointF( this .button1.Width - 1.5F, 1.5F)); |
121 | } |
122 |
123 | } |
124 | } |
'C# > Tip' 카테고리의 다른 글
C# XML에서 자식노드 생성, 값 넣기 (2) | 2009.09.12 |
---|---|
C# XML문서 만들기 (0) | 2009.09.12 |
C# 에서 interop가 참조안될때 (0) | 2009.09.02 |
C# 파일 쓰기, 복사, 삭제, 이동 (0) | 2009.08.31 |
C# 자식창에서 부모창으로 값넘기기 (0) | 2009.08.12 |