본문 바로가기
5_ 단짠단짠 자료실

[C#, Download] 프로젝트 기본 폼 (시작할때)

by 준환이형님_ 2024. 5. 25.

CSDragForm_20240525.zip
0.07MB

 

※ 이건 일단 제가 자주 쓰기 때문에 올리는 겁니다 (별게 아니다보니 한 5년 미뤘습니다)

 

다들 프로젝트 시작할때 코딩을 어떻게 하시는지 모르지만

저는 c# 짤때 100이면 100  파일불러오기 기능을 넣기 때문에 이걸 따로 하나 만들어두었습니다. 

 

- 텍스트(.txt) 파일을 끌어다 넣으면 콘솔에 출력되도록 하는 기능입니다.

- 이미지파일(.jpg, png) 파일은 드래그하면 picture box에 띄우고 마우스 휠 입력받아 확대/축소가 되도록 하였습니다

 

필요하신 분들 함께 써용~

 

 

using System;
using System.IO;

using System.Windows.Forms;
using System.Threading;
using System.Drawing;

namespace CSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // MouseWheel 이벤트 핸들러 추가
            this.MouseWheel += new MouseEventHandler(this.Form1_MouseWheel);
            this.pb.Paint += new PaintEventHandler(this.PictureBox_Paint);
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);

                foreach (string str in file)
                {
                    Console.WriteLine(str);

                    Thread thread = new Thread(() => Parser(str));

                    thread.Start();
                }
            }
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy | DragDropEffects.Scroll;
            }
        }


        Image image;

        public void Parser(string path)
        {
            string strSaveFilePath = path;

            //확장자분리
            string extension = Path.GetExtension(strSaveFilePath).ToLower();

            if (extension == ".txt")
            {
                StreamReader SRead = new StreamReader(strSaveFilePath, System.Text.Encoding.UTF8 /* .UTF8*/);

                // 텍스트 파일 출력
                string text = File.ReadAllText(strSaveFilePath);
                Console.WriteLine(text);
            }
            else if (extension == ".jpg" || extension == ".png")
            {
                // 이미지 파일 출력
                image = Image.FromFile(strSaveFilePath);
                this.pb.Image = image;
                pb.Invalidate();
            }
            else
            {
                MessageBox.Show("지원되지 않는 파일 형식입니다.");
            }
        }

 

        private float zoomFactor = 1.0f;
        private const float zoomIncrement = 0.1f;

        private void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                zoomFactor += zoomIncrement;
            }
            else if (e.Delta < 0)
            {
                zoomFactor = Math.Max(zoomIncrement, zoomFactor - zoomIncrement);
            }            
            pb.Invalidate(); // PictureBox 다시 그리기
        }        

        private void PictureBox_Paint(object sender, PaintEventArgs e)
        {
            if (image != null)
            {
                e.Graphics.Clear(Color.Black);

                double dbPbWid = pb.Size.Width/2;
                double dbPbHei = pb.Size.Height/2;

                double newWidth = image.Width * zoomFactor;
                double newHeight = image.Height * zoomFactor;

                e.Graphics.DrawImage(image, new Rectangle((int)(dbPbWid- newWidth/2),(int)(dbPbHei- newHeight/2), (int)newWidth, (int)newHeight));
            }
        }
    }
}