'formdesign'에 해당되는 글 1건

  1. 2009.09.09 c# 폼디자인, 컨트롤박스 만들기 by 아르다


업무일지 프로그램을 만드는 과정에서 트레이 버튼을 추가하려다가
제공되는 컨트롤박스에서는 트레이 버튼을 추가할 수 없어서 만들어 봤습니다.

응용해서 여러가지 만들 수 있을 거 같아서 남겨 놓습니다.


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

namespace WindowsApplication7
{
    public partial class Form1 : Form
    {

        //반투명 알파값
        private int percentAlpha = 70;
        //반투명 색상
        private Color pb = new Color();
        //그라데이션 브러쉬
        private LinearGradientBrush lineGBrush;
        //마우스로 폼 드래그 하기
        private Point mCurrentPosition = new Point(0, 0);
        //마우스 드래그시 상단 타이틀인지 체크
        private bool titleMove = false;

        public Form1()
        {
            InitializeComponent();
            Design_Setting();
        }

        private void Design_Setting()
        {
            //컨트롤박스 제거, 키프리뷰 설정, 하단그립 제거, 폼테두리 변경(FixedSingle), 더블버퍼링 설정
            //스타일 변경
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);

            //색상 설정
            pb = Color.FromArgb(percentAlpha * 255 / 100, Color.FromArgb(0, 128, 255));

            //그라데이션 설정
            lineGBrush = new LinearGradientBrush(new PointF(0, 0), new PointF(this.Width, 0), Color.SkyBlue, pb);

            //라인브러쉬 블랜드 적용
            float[] relativelntensities = { 0f, 0.5f, 1.0f };
            float[] reltivePositions = { 0.0f, 0.1f, 1.0f };
            Blend blend = new Blend();
            blend.Factors = relativelntensities;
            blend.Positions = reltivePositions;
            lineGBrush.Blend = blend;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //폼 배경을 흰색으로 설정
            e.Graphics.Clear(Color.Snow);

            // 브러쉬 설정
            SolidBrush sBrush = new SolidBrush(Color.FromArgb(127, 50, 0));
            sBrush.Color = Color.White;

            //화면 그리기
            Image memimage = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(memimage);

            string title = "http://redreans.tistory.com";
            g.FillRectangle(this.lineGBrush, 0, 0, this.Width, 20);
            g.DrawString(title, this.Font, sBrush, 5F, 5F);

            //화면으로 표현
            e.Graphics.DrawImage(memimage, ClientRectangle);

            g.Dispose();
            memimage.Dispose();
            sBrush.Dispose();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Y < 20)
            {
                mCurrentPosition = new Point(-e.X, -e.Y);
                this.titleMove = true;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && this.titleMove == true)
            {
                this.Location = new Point(
                    this.Location.X + (mCurrentPosition.X + e.X),
                    this.Location.Y + (mCurrentPosition.Y + e.Y));// 마우스의 이동치를 Form Location에 반영한다.
            }

        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            /* 마우스로 폼 드래그 */
            if (e.Button == MouseButtons.Left && this.titleMove == true)
            {
                this.Location = new Point(
                    this.Location.X + (mCurrentPosition.X + e.X),
                    this.Location.Y + (mCurrentPosition.Y + e.Y));// 마우스의 이동치를 Form Location에 반영한다.
                this.titleMove = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Paint(object sender, PaintEventArgs e)
        {
            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));
            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));
        }

    }
}
Posted by 아르다