첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using Microsoft.Win32;
using System;
using System.Threading;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 경고 폼 보여주기 대리자 - ShowWarningFormDelegate(message)

        /// <summary>
        /// 경고 폼 보여주기 대리자
        /// </summary>
        /// <param name="message">메시지</param>
        private delegate void ShowWarningFormDelegate(string message);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 레지스트리 키
        /// </summary>
        private RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\USBSTOR", true);

        /// <summary>
        /// 경고 폼 보여주기 대리자
        /// </summary>
        private ShowWarningFormDelegate showWarningFormDelegate = null;

        /// <summary>
        /// 스레드
        /// </summary>
        private Thread thread = null;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 닫을 경우 처리하기 - Form_FormClosing(sender, e)

        /// <summary>
        /// 폼 닫을 경우 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(this.thread != null)
            {
                this.thread.Abort();
            }

            Application.ExitThread();
        }

        #endregion
        #region 적용 버튼 클릭시 처리하기 - applyButton_Click(sender, e)

        /// <summary>
        /// 적용 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void applyButton_Click(object sender, EventArgs e)
        {
            if(this.enableUSBPortCheckBox.Checked == true)
            {
                this.registryKey.SetValue("Start", (int)Permission.ALLOW);

                if(this.thread != null)
                {
                    this.thread.Abort();
                }
            }
            else if(this.disableUSBPortCheckBox.Checked == true)
            {
                this.registryKey.SetValue("Start", (int)Permission.DENY);

                this.showWarningFormDelegate = new ShowWarningFormDelegate(ShowWarningForm);

                if(this.realTimeCheckBox.Checked == true)
                {
                    this.thread = new Thread(ProcessThread);

                    this.thread.Start();
                }
            }
        }

        #endregion
        #region USB 포트 활성화 체크 박스 체크 변경시 처리하기 - enableUSBPortCheckBox_CheckedChanged(sender, e)

        /// <summary>
        /// USB 포트 
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void enableUSBPortCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            this.realTimeCheckBox.Checked = false;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 스레드 처리하기 - ProcessThread()

        /// <summary>
        /// 스레드 처리하기
        /// </summary>
        private void ProcessThread()
        {
            while(true)
            {
                if((int)registryKey.GetValue("Start") != 4)
                {
                    this.registryKey.SetValue("Start", (int)Permission.DENY);

                    Invoke(this.showWarningFormDelegate, "레지스트리 비정상 접근 차단");
                }
            }
        }

        #endregion
        #region 경고 폼 보여주기 - ShowWarningForm(message)

        /// <summary>
        /// 경고 폼 보여주기
        /// </summary>
        /// <param name="message">메시지</param>
        private void ShowWarningForm(string message)
        {
            WarningForm form = new WarningForm();

            form.Message = message;

            form.ShowDialog();
        }

        #endregion
    }
}

 

728x90

 

▶ WarningForm.cs

using System;
using System.Drawing;
using System.Timers;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 경고 폼
    /// </summary>
    public partial class WarningForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 타이머
        /// </summary>
        private System.Timers.Timer timer;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 메시지 - Message

        /// <summary>
        /// 메시지
        /// </summary>
        public string Message { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - WarningForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public WarningForm()
        {
            int left = Screen.PrimaryScreen.WorkingArea.Width  - Width - 20;
            int top  = Screen.PrimaryScreen.WorkingArea.Height - Height;

            DesktopLocation = new Point(left, top);

            Height = 2;

            InitializeComponent();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            Rectangle primaryScreenRectangle = Screen.PrimaryScreen.Bounds;

            Location = new Point
            (
                primaryScreenRectangle.Width  - Width,
                primaryScreenRectangle.Height - Height
            );

            this.messageLinkLabel.Text = Message;

            this.timer = new System.Timers.Timer(2);

            this.timer.Elapsed += timer_Elapsed1;

            this.timer.Start();
        }

        #endregion
        #region 타이머 경과시 처리하기 1 - timer_Elapsed1(sender, e)

        /// <summary>
        /// 타이머 경과시 처리하기 1
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void timer_Elapsed1(object sender, ElapsedEventArgs e)
        {
            if(Height < 130)
            {
                Height++;

                Application.DoEvents();

                Top--;

                Application.DoEvents();
            }
            else
            {
                this.timer.Stop();

                this.timer.Elapsed -= timer_Elapsed1;

                this.timer.Elapsed += timer_Elapsed2;

                this.timer.Interval = 3000;

                this.timer.Start();
            }
        }

        #endregion
        #region 타이머 경과시 처리하기 2 - timer_Elapsed2(sender, e)

        /// <summary>
        /// 타이머 경과시 처리하기 2
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void timer_Elapsed2(object sender, ElapsedEventArgs e)
        {
            while(Height > 2)
            {
                Height--;

                Application.DoEvents();

                Top++;

                Application.DoEvents();
            }

            Close();
        }

        #endregion
        #region 닫기 픽처 박스 마우스 이동시 처리하기 - closePictureBox_MouseMove(sender, e)

        /// <summary>
        /// 닫기 픽처 박스 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void closePictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            this.closePictureBox.Image = Properties.Resources.close_over;
        }

        #endregion
        #region 닫기 픽처 박스 마우스 DOWN 처리하기 - closePictureBox_MouseDown(sender, e)

        /// <summary>
        /// 닫기 픽처 박스 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 발생자</param>
        private void closePictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.closePictureBox.Image = Properties.Resources.close_down;
        }

        #endregion
        #region 닫기 픽처 박스 클릭시 처리하기 - closePictureBox_Click(sender, e)

        /// <summary>
        /// 닫기 픽처 박스 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void closePictureBox_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
        #region 닫기 픽처 박스 마우스 이탈시 처리하기 - closePictureBox_MouseLeave(sender, e)

        /// <summary>
        /// 닫기 픽처 박스 마우스 이탈시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void closePictureBox_MouseLeave(object sender, EventArgs e)
        {
            this.closePictureBox.Image = Properties.Resources.close_normal;
        }

        #endregion
        #region 메시지 링크 레이블 링크 클릭시 처리하기 - messageLinkLabel_LinkClicked(sender, e)

        /// <summary>
        /// 메시지 링크 레이블 링크 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void messageLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Close();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요