본문 바로가기

   
Programming/Winform

윈폼(winform)으로 구현한 화면 캡쳐, 성적그래프 WPF 예제

반응형

윈폼(winform)으로 구현한 화면 캡쳐, 성적그래프 WPF 예제

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Imaging;

 

namespace WinformTest

{

       public partial class Ex01_Capture : Form

       {

             public Ex01_Capture()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //현재 스크림의 크기

                    Size size = new Size();

                    size.Width = SystemInformation.PrimaryMonitorSize.Width;

                    size.Height = SystemInformation.PrimaryMonitorSize.Height;

                    //Screen.PrimaryScreen.Bouds.Width

 

 

                    //2. 현재 스크림의 크기와 똑같은 크기의 이미지 생성

                    Bitmap img = new Bitmap(size.Width, size.Height);

 

                    //3. img 로부터 Graphics 객체를 생성 -> img에 그림 그리기

                    Graphics g = Graphics.FromImage(img);

 

                    //4. 원하는 그림 그리기

                    //g.DrawEllipse(Pens.Red, 10, 10, 200, 200);

                    //g.CopyFromScreen(0, 0,: 현재 이미지 크기 0, 0, : img 루트 경로 size);

                    g.CopyFromScreen(0, 0, 0, 0, size);

 

                    //5. 이미지를 픽쳐박스에 속성으로 대입

                    pictureBox1.Image = img;

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    saveFileDialog1.Filter = "png 이미지|*.png";

 

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)

                    {

                           //픽쳐박스의 이미지를 원하는 경로에 직접 저장

                           pictureBox1.Image.Save(saveFileDialog1.FileName, ImageFormat.Png);

                    }

             }

       }

}


 
 USER CONTROLL 내가원하는 것을 모아놓은 상태에서 기능 까지 복사가 된다.
사용자 정의 컨트롤을 부모에서 접근할때는 접근 지정자를 private 에서 public 으로 바꾸어준다.
프로젝트 오른쪽 버튼  추가 -> 사용자 정의 컨트롤
프로젝트 실행 파일에 폴더를 만들어 이미지를 넣어 놓으며 txt(db)파일도 실행 폴더안에 넣어놓는다.


윈폼프로젝트 + 부분 WPF = 0
WPF프로젝트 + 부분윈폼 = X

 

 


student cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ProjectExam

{

       class Student

       {

             public string Name { get; set; }

             public double Kor { get; set; }

             public double Eng { get; set; }

             public double Math { get; set; }

       }

}

 

 


Start.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Windows.Annotations;

 

namespace ProjectExam

{

       public partial class Start : Form

       {

             private List<Student> list;

             public Start()

             {

                    InitializeComponent();

                    list = new List<Student>();

             }

 

             private void Start_Load(object sender, EventArgs e)

             {

                    StreamReader reader = new StreamReader("student.txt");

                    string s = "";

                    while ((s = reader.ReadLine()) != null)

                    {

                           string[] temp = s.Split('|');

 

                           //Student student = new Student { Name = temp[0], Kor = double.Parse(temp[1]), Eng = double.Parse(temp[2]), Math = double.Parse(temp[3]) };

 

                           Student student = new Student();

                           student.Name = temp[0];

                           student.Kor = double.Parse(temp[1]);

                           student.Eng = double.Parse(temp[2]);

                           student.Math = double.Parse(temp[3]);

 

                           list.Add(student);

                    }

 

                    reader.Close();

 

 

 

                    //List<Student> list안의 데이터를 ListView컨트롤 추가 -> 표 출력

 

                    foreach (Student st in list)

                    {

                           //ListViewItem 1개 생성 -> 한행

                           ListViewItem item = new ListViewItem(st.Name);

                           item.SubItems.Add(st.Kor.ToString());

                           item.SubItems.Add(st.Eng.ToString());

                           item.SubItems.Add(st.Math.ToString());

 

                           listView1.Items.Add(item);

                    }

             }

 

             private void listView1_SelectedIndexChanged(object sender, EventArgs e)

             {

                    //항목 선택(학생 선택)

                    if (listView1.SelectedItems.Count > 0)

                    {

                           string name = listView1.SelectedItems[0].Text;

                           string kor = listView1.SelectedItems[0].SubItems[1].Text;

                           string eng = listView1.SelectedItems[0].SubItems[2].Text;

                           string math = listView1.SelectedItems[0].SubItems[3].Text;

 

                           //MessageBox.Show(name + " : " + kor + " : " + eng + " : " + math);

 

                           //WPF의 메서드를 호출

                           ucStudent uc = elementHost1.Child as ucStudent;

                           uc.ShowScore(double.Parse(kor), double.Parse(eng), double.Parse(math));

                    }

             }

       }

}

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Windows.Media.Animation;//**

 

 ucStudent.XAML.CS

namespace ProjectExam

{

       /// <summary>

       /// ucStudent.xaml에 대한 상호 작용 논리

       /// </summary>

       public partial class ucStudent : UserControl

       {

             public ucStudent()

             {

                    InitializeComponent();

             }

 

             //윈폼에서 호출할 메서드

             public void ShowScore(double kor, double eng, double math)

             {

                    List<Button> btnList = new List<Button>();

                    btnList.Add(btnKor);

                    btnList.Add(btnEng);

                    btnList.Add(btnMath);

 

                    List<double> scoreList = new List<double>();

                    scoreList.Add(kor);

                    scoreList.Add(eng);

                    scoreList.Add(math);

 

                    for (int i = 0; i < 3; i++)

                    {

                           DoubleAnimation da = new DoubleAnimation();

                           da.From = 0;

                           da.To = scoreList[i] * 2.5;

                           da.Duration = new Duration(TimeSpan.FromSeconds(1));

 

                           btnList[i].BeginAnimation(Button.HeightProperty, da);

                    }

             }

       }

}

 
ucStudent.xaml

 <UserControl x:Class="ProjectExam.ucStudent"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             mc:Ignorable="d"

             d:DesignHeight="447" d:DesignWidth="317">

       <UserControl.Resources>

             <Style TargetType="{x:Type Button}">

                    <Setter Property="Width" Value="80" />

                    <Setter Property="Height" Value="0" />

                    <Setter Property="Margin" Value="3" />

                    <Setter Property="VerticalAlignment" Value="Bottom" />

                    <Setter Property="VerticalContentAlignment" Value="Top" />

             </Style>

       </UserControl.Resources>

 

       <StackPanel Name="stack" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom">

             <Button Content="국어" Name="btnKor" />

             <Button Content="영어" Name="btnEng" />

             <Button Content="수학" Name="btnMath" />

       </StackPanel>

 

</UserControl>

 

반응형