2018년 11월 28일 수요일

[C#] 속성코스

Crash Course on C#
nonezerok@gmail.com

----------------------------------------
0.기본
----------------------------------------
객체: 변수, 함수
Form 객체
- 변수, 함수
- 속성, 이벤트 (이 점이 윈도우 프로그래밍을 기가 막히게 쉽게 만드는 요인)

----------------------------------------
1.프로그램종료
----------------------------------------
Application.Exit();

----------------------------------------
2.마우스클릭 + 메세지박스
----------------------------------------
MessageBox.Show(" ");

----------------------------------------
3.마우스무브 이벤트 + 글자 출력
----------------------------------------
private void Form1_MouseMove(object sender, MouseEventArgs e)
        {         
Graphics g = CreateGraphics();
String drawString = "Sample Text";
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
PointF drawPoint = new PointF(150.0F, 150.0F);
g.DrawString(drawString, drawFont, drawBrush, drawPoint);
g.Dispose();

/*
           Graphics g = CreateGraphics();

            Font drawFont = new Font("FixedSys", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            float x = 0.0F;
            float y = 0.0F;
            float width = 200.0F;
            float height = 50.0F;
            RectangleF drawRect = new RectangleF(x, y, width, height);

            // 글자가 쓰여지는 부분을 회색으로 채운다.
            Color backGray = Color.FromArgb(240, 240, 240);
            SolidBrush backBrush = new SolidBrush(backGray);
            g.FillRectangle(backBrush, 0, 0, 200, 50);

            String str = string.Format("{0:d5} {1:d5}", e.X, e.Y);
            g.DrawString(str, drawFont, drawBrush, drawRect);
       
            g.Dispose();
*/                         
         
        }

----------------------------------------
4. 자식윈도우생성, 생성이벤트, 종료
----------------------------------------
    private void Form1_Load(object sender, EventArgs e)
        {
            frm2 = new Form2(); // Form2(this) 식으로 구현
            frm2.Owner = this; // Form1의 자식윈도우로 설정
            frm2.Show();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("I'm going to die");
            //frm2.Close(); // 윈도우파괴
            //frm2.Dispose(); // 메모리해제
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            MessageBox.Show("I am dead"); 
        }

----------------------------------------
5. 자식윈도우 스타일: TopLevel=false, Control.Add(frm2)
----------------------------------------
        private void Form1_Load(object sender, EventArgs e)
        {
            frm2 = new Form2();
            frm2.Owner = this;
            frm2.TopLevel = false;
            Controls.Add(frm2);
            frm2.Show();
        }

FormBorderStyle
IsMdiContainer

private Form1 frmMain=null; // 자식윈도우
frmMain = this.Owner as Form1; // 자식윈도우; frm2.Owner = this;
//frmMain = (Form2)this.Owner;
frmMain.TextBox1.Text = "Message From Child Window";
Form2(Form parent)
{
frmMain = parent as Form1;
}

----------------------------------------
6. 컨트롤윈도우
----------------------------------------
button, textbox, label, progressbar, timer, picture box, menu
메뉴-
frmMain.Dispose();
Application.Exit();

----------------------------------------
7.대화상자윈도우
----------------------------------------
Form2 frm2 = new Form2();
if (frm2.ShowDialog(this) == DialogResult.OK)
{
textBox1.Text = frm2.textBox1.Text; // 도구상자 속성에서 public으로 설정
               //MessageBox.Show("오케이");
}
frm2.Dispose(); // 메모리 해제

private void button1_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close(); // 윈도우 파괴
}

----------------------------------------
8. (2016/11/20)OpenCVSharp
- NuGet
----------------------------------------
using OpenCvSharp;
using OpenCvSharp.Extensions;

// https://www.nuget.org/packages/
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Mat src = new Mat("lena.bmp", ImreadModes.Color);
    src = src.Blur(new OpecCvSharp.Size(7,7));


            /*
            using (new Window("dst image", src))
            {
                Cv2.WaitKey();
            }
            */
            // 필터링 적용

            //
            // 아래와 같이 했을때는 올바른 버퍼를 가리키고 있지 않았음.
            //byte[] pImg = src.ToBytes();
            //

            IntPtr pImg = src.Data;

            // for unsafe
            //1. 프로젝트의 속성 페이지를 엽니다.
            //2. 빌드 속성 페이지를 클릭합니다.
            //3. 안전하지 않은 코드 허용 확인란을 선택합니다.
            unsafe
            {
                //byte* buff = (byte*)pImg.ToPointer();
                byte* buff = (byte *)src.DataPointer;

                for (int i = 0, idx = 0; i < src.Cols * src.Rows; i++, idx += 3)
                {
                    int gray=0;
                    gray = (buff[idx + 0] + buff[idx + 1] + buff[idx + 2]);
                    gray = gray / 3;

                    buff[idx + 0] = (byte)gray;
                    buff[idx + 1] = (byte)gray;
                    buff[idx + 2] = (byte)gray;
                }
            }

            //
            // 한번은 비트맵으로 바꾸어야 한다
            //
            Bitmap bmp;
            bmp = src.ToBitmap();

            // 이렇게 하는 것 보다는...
            // pictureBox1.Image = bmp;
            //
            // 아래와 같이 하는 것이 낫지 않을까.
            // (고속처리)
            IntPtr hWnd = pictureBox1.Handle;
            Graphics newGraphics = Graphics.FromHwnd(hWnd);
            // newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);i
            newGraphics.DrawImage(bmp, 0, 0);
            newGraphics.Dispose();
        }
----------------------------------------
Last Updated: 2016/10/05