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.Runtime.InteropServices;

namespace LessonA48
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport(@"CChartu.dll", EntryPoint = "?CreatePopupChartWndGlobal@NsClassless@@YAPAUHWND__@@PAU2@HPA_WHHHH@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        protected extern static int CreatePopupChartWndGlobal(int hwndParent, int charttype, string title, int x, int y, int width, int height);
        [DllImport(@"CChartu.dll", EntryPoint = "?Attach@NsClassless@@YAHPAUHWND__@@H@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        protected extern static int Attach(int hWnd, int nType);
        [DllImport(@"CChartu.dll", EntryPoint = "?AddPoint2D@NsClassless@@YAHNNHH@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        protected extern static int AddPoint2D(double x, double y, int nCurveIndex, int nPlotIndex);
        [DllImport(@"CChartu.dll", EntryPoint = "?SetTitle@NsClassless@@YAXPB_W@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        protected extern static void SetTitle(string title);
        [DllImport(@"CChartu.dll", EntryPoint = "?ReDraw@NsClassless@@YAXXZ", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        protected extern static void ReDraw();
        [DllImport(@"CChartu.dll", EntryPoint = "?ChooseCurChart@NsClassless@@YAXPAUHWND__@@@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        protected extern static void ChooseCurChart(int hWnd);

        public MainWindow()
        {
            InitializeComponent();
        }

        private void MyPopupWindow(object sender, RoutedEventArgs e)
        {
            string title = "弹出窗口";
            int hWnd = CreatePopupChartWndGlobal(0, 0, title, 0, 0, 800, 600);
            ChooseCurChart(hWnd);
            double x, y;
            for (int i = 0; i < 361; ++i)
            {
                x = 1.6 * Math.Cos(i * 2.0 * Math.PI / 360);
                y = 1.6 * Math.Sin(i * 2.0 * Math.PI / 360);
                AddPoint2D(x, y, 0, 0);
            }
            
            SetTitle("弹出窗口模式");
            ReDraw();
        }

        private void MyDrawWindow(object sender, RoutedEventArgs e)
        {
            int hWnd;
            hWnd = ChartPanel.Handle.ToInt32();
            Attach(hWnd, 0);
            ChooseCurChart(hWnd);
            
            double x, y;
            for (int i = 0; i < 361; ++i)
            {
                x = i*2.0;
                y = 1.6 * Math.Sin(i * 2.0 * Math.PI / 360);
                AddPoint2D(x, y, 0, 0);
            }
            string title = "子窗口模式";
            SetTitle(title);
            ReDraw();
        }
    }
}
