프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / MainWindow.xaml.cs @ d04e8ee9

이력 | 보기 | 이력해설 | 다운로드 (23.6 KB)

1
using KCOM.Common;
2
using KCOM.Controls;
3
using KCOM.Views;
4
using KCOMDataModel;
5
using KCOMDataModel.DataModel;
6
using MarkupToPDF.Common;
7
using MarkupToPDF.Controls.Parsing;
8
using MarkupToPDF.Serialize.Core;
9
using MarkupToPDF.Serialize.S_Control;
10
using System;
11
using System.Collections.Generic;
12
using System.ComponentModel;
13
using System.Diagnostics;
14
using System.IO;
15
using System.Linq;
16
using System.Net;
17
using System.Reflection;
18
using System.Runtime.InteropServices;
19
using System.Text;
20
using System.Threading;
21
using System.Threading.Tasks;
22
using System.Windows;
23
using System.Windows.Controls;
24
using System.Windows.Data;
25
using System.Windows.Documents;
26
using System.Windows.Input;
27
using System.Windows.Media;
28
using System.Windows.Media.Imaging;
29
using System.Windows.Navigation;
30
using System.Windows.Shapes;
31
using System.Xml;
32
using Telerik.Windows.Controls;
33
using WinInterop = System.Windows.Interop;
34

    
35
namespace KCOM
36
{
37
    /// <summary>
38
    /// MainWindow.xaml에 대한 상호 작용 논리
39
    /// </summary>
40
    public partial class MainWindow : Window
41
    {
42
        bool isSaveCheck = false;
43
        
44
        ProgressControl progressControl = null;
45
        string destfilepath = string.Empty;
46

    
47
        public MainWindow()
48
        {
49
            App.splashString(ISplashMessage.MAINWINDOW);
50
            this.Loaded += MainWindow_Loaded;
51
            this.Unloaded += MainWindow_Unloaded;
52
            this.PreviewKeyDown += new KeyEventHandler(KeyEventDownAction);
53
            this.SourceInitialized += new EventHandler(win_SourceInitialized);
54
        }
55

    
56
        private void MainWindow_Unloaded(object sender, RoutedEventArgs e)
57
        {
58
            //ViewerDataModel.Instance.ImageViewPath.UriSource = null;
59
            //ViewerDataModel.Instance.ImageViewPath_C.UriSource = null;
60
        }
61

    
62
        public static BitmapImage CursorChange()
63
        {
64
            BitmapImage bmp = new BitmapImage();
65
            bmp.BeginInit();
66
            bmp.StreamSource = System.Windows.Application.GetResourceStream(new Uri("/KCOM;Component/Resources/Cursor/customCursor2.cur", UriKind.Relative)).Stream;
67
            return bmp;
68
        }
69

    
70
        public void DialogMessage_Alert(string content, string header)
71
        {
72
            Telerik.Windows.Controls.DialogParameters parameters = new Telerik.Windows.Controls.DialogParameters()
73
            {
74
                Owner = Application.Current.MainWindow,
75
                Content = new TextBlock()
76
                { 
77
                    MinWidth = 400,
78
                    FontSize = 12,
79
                    Text = content,
80
                    TextWrapping = System.Windows.TextWrapping.Wrap
81
                },
82
                Header = header,
83
                Theme = new Telerik.Windows.Controls.VisualStudio2013Theme(),
84
                ModalBackground = new SolidColorBrush { Color = Colors.Black, Opacity = 0.6 },
85
            };
86
            Telerik.Windows.Controls.RadWindow.Alert(parameters);
87
        }
88

    
89
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
90
        {
91
            InitializeComponent();
92
            
93
            //cursor change  
94
            this.Cursor = new Cursor(CursorChange().StreamSource);
95

    
96
            double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
97
            double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
98
            //this.Width += 40;
99
            double windowWidth = this.Width;
100
            double windowHeight = this.Height;
101
            this.Left = (screenWidth / 2) - (windowWidth / 2);
102
            this.Top = (screenHeight / 2) - (windowHeight / 2);
103

    
104
            ViewerDataModel.Instance.SystemMain = this;
105

    
106
            if (!App.ParameterMode)
107
            {
108
                //App.ViewInfo = new IKCOM.ViewInfo
109
                //{
110
                //    DocumentItemID = "11111112",
111
                //    //DocumentItemID = "10001",
112
                //    bPartner = false,
113
                //    CreateFinalPDFPermission = true,
114
                //    NewCommentPermission = true,
115
                //    ProjectNO = "000000",
116
                //    UserID = "H2011357",
117
                //    //UserID = "H2009115",
118
                //    //Mode = 0 , 1 , 2
119
                //};
120
                //DialogMessage_Alert("데모버전은 단독 실행모드를 지원하지 않습니다", "안내");
121

    
122
                //#if DEBUG
123
                //                App.ViewInfo = new IKCOM.ViewInfo
124
                //                {
125
                //                    DocumentItemID = "11111112",
126
                //                    //DocumentItemID = "10001",
127
                //                    bPartner = false,
128
                //                    CreateFinalPDFPermission = true,
129
                //                    NewCommentPermission = true,
130
                //                    ProjectNO = "000000",
131
                //                    UserID = "H2011357",
132
                //                    //UserID = "H2009115",
133
                //                    //Mode = 0 , 1 , 2
134
                //                };
135
                //                App.ParameterMode = true;
136
                //                this.dzMainMenu.ServiceOn();
137
                //                this.dzMainMenu.SetView(App.ViewInfo);
138
                //#else
139

    
140
                DialogMessage_Alert("데모버전은 단독 실행모드를 지원하지 않습니다", "안내");
141
                this.dzMainMenu.pageNavigator.Visibility = Visibility.Collapsed;
142
                this.dzMainMenu.historyPane.Visibility = Visibility.Collapsed;
143
                this.dzMainMenu.infoListPane.Visibility = Visibility.Collapsed;
144
                this.dzMainMenu.searchPane.Visibility = Visibility.Collapsed;
145
                this.dzMainMenu.talkPane.Visibility = Visibility.Collapsed;
146
                //#endif
147
            }
148
            else
149
            {
150
                this.dzMainMenu.ServiceOn();
151

    
152
                if (!App.ViewInfo.CreateFinalPDFPermission && !App.ViewInfo.NewCommentPermission)
153
                {
154
                    this.dzTopMenu.radRibbonView.HelpButtonVisibility = Visibility.Collapsed;
155
                    var list = this.dzTopMenu.ChildrenOfType<RadRibbonTab>().ToList();
156
                    list.ForEach(item => item.Visibility = Visibility.Collapsed);
157
                    this.dzTopMenu.ribbontab_ReadOnly.Visibility = Visibility.Visible;
158
                    this.dzTopMenu.radRibbonView.SelectedItem = this.dzTopMenu.ribbontab_ReadOnly;
159
                    //this.dzMainMenu.SymbolPane.Visibility = Visibility.Collapsed;
160
                    //this.dzMainMenu.FavoritePane.Visibility = Visibility.Collapsed;
161
                    //this.dzMainMenu.drawingRotateCanvas.IsHitTestVisible = false;
162
                }
163

    
164
                try
165
                {
166
                    ///this.dzMainMenu.HubSet();
167
                    this.dzMainMenu.SetView(App.ViewInfo);
168
                }
169
                catch (Exception ex)
170
                {
171
                    MessageBox.Show(string.Format("웹 서비스 접속시 에러가 발생했습니다.\n{0}", ex.StackTrace));
172

    
173
                    System.Environment.Exit(0);
174
                }
175
            }
176

    
177
            //App.ViewInfo = new IKCOM.ViewInfo
178
            //{
179
            //    DocumentItemID = "11111112",
180
            //    //DocumentItemID = "10001",
181
            //    bPartner = false,
182
            //    CreateFinalPDFPermission = true,
183
            //    NewCommentPermission = true,
184
            //    ProjectNO = "000000",
185
            //    UserID = "H2011357",
186
            //    //UserID = "H2009115",
187
            //    //Mode = 0 , 1 , 2
188
            //};
189

    
190
            //this.dzMainMenu.ServiceOn();
191
            //this.dzMainMenu.SetView(App.ViewInfo);
192
        }
193

    
194
      
195
        bool restoreIfMove = false;
196

    
197
        private void WindowDragEvent(object sender, MouseButtonEventArgs e)
198
        {
199
            if(string.IsNullOrEmpty(destfilepath))
200
            {
201
                if (e.ClickCount == 2)
202
                {
203
                    if ((ResizeMode == ResizeMode.CanResize) ||
204
                        (ResizeMode == ResizeMode.CanResizeWithGrip))
205
                    {
206
                        SwitchState();
207
                    }
208
                }
209
                else
210
                {
211
                    if (WindowState == WindowState.Maximized)
212
                    {
213
                        restoreIfMove = true;
214
                    }
215

    
216
                    this.DragMove();
217
                }
218
            }            
219
        }   
220

    
221
        private void WindowDragEventUp(object sender, MouseButtonEventArgs e)
222
        {
223
            restoreIfMove = false;
224
        }
225

    
226
        private void WindowDragEventMove(object sender, MouseEventArgs e)
227
        {
228
            if (restoreIfMove)
229
            {
230
                if (Mouse.LeftButton == MouseButtonState.Pressed)
231
                {
232
                    //this.WindowState = WindowState.Normal;
233

    
234
                    restoreIfMove = false;
235

    
236
                    double percentHorizontal = e.GetPosition(this).X / ActualWidth;
237
                    double targetHorizontal = RestoreBounds.Width * percentHorizontal;
238

    
239
                    double percentVertical = e.GetPosition(this).Y / ActualHeight;
240
                    double targetVertical = RestoreBounds.Height * percentVertical;
241

    
242
                    POINT lMousePosition;
243
                    GetCursorPos(out lMousePosition);
244

    
245
                    Left = lMousePosition.X - targetHorizontal;
246
                    double top = lMousePosition.Y - targetVertical;
247
                    if(top < 10)
248
                    {
249
                        top = 10;
250
                    }
251
                    Top = lMousePosition.Y;
252

    
253

    
254
                    WindowState = WindowState.Normal;
255

    
256
                    this.DragMove();
257
                }
258
            }
259
        }
260

    
261
        [DllImport("user32.dll")]
262
        [return: MarshalAs(UnmanagedType.Bool)]
263
        static extern bool GetCursorPos(out POINT lpPoint);
264

    
265
        [StructLayout(LayoutKind.Sequential)]
266
        public struct POINT
267
        {
268
            public int X;
269
            public int Y;
270

    
271
            public POINT(int x, int y)
272
            {
273
                this.X = x;
274
                this.Y = y;
275
            }
276
        }
277

    
278
        private void SwitchState()
279
        {
280
            switch (WindowState)
281
            {
282
                case WindowState.Normal:
283
                    {
284
                        WindowState = WindowState.Maximized;
285
                        break;
286
                    }
287
                case WindowState.Maximized:
288
                    {
289
                        WindowState = WindowState.Normal;
290
                        break;
291
                    }
292
            }
293
        }
294

    
295
        private void RadButton_Click(object sender, RoutedEventArgs e)
296
        {
297
            Telerik.Windows.Controls.RadButton button = sender as Telerik.Windows.Controls.RadButton;
298

    
299
            switch (button.CommandParameter.ToString())
300
            {
301
                case ("Min"):
302
                    {
303
                        WindowState = WindowState.Minimized;
304
                    }
305
                    break;
306
                case ("Max"):
307
                    {
308
                        WindowState = WindowState.Maximized;
309
                    }
310
                    break;
311
                case ("Exit"):
312
                    {
313

    
314
                    }
315
                    break;
316
            }
317
        }
318

    
319
        private void WinState(object sender, MouseButtonEventArgs e)
320
        {
321
            switch ((e.Source as Image).Name)
322
            {
323
                case ("Win_min"):
324
                    {
325
                        WindowState = WindowState.Minimized;
326
                    }
327
                    break;
328
                case ("Win_max"):
329
                    {
330
                        if (WindowState == WindowState.Maximized)
331
                        {
332
                            WindowState = WindowState.Normal;
333
                        }
334
                        else
335
                        {
336
                            WindowState = WindowState.Maximized;
337
                        }
338
                    }
339
                    break;
340
                case ("Win_Close"):
341
                    {
342
                        SaveCheck();
343
                        //Update Check 를 통해 update url 을 Get 하고 결과값이 있을 경우에는 SmartUpdater 실행.
344
                        KeyValuePair<bool, string> updatecheck = UpdateCheck();
345
                        if (updatecheck.Key && !string.IsNullOrEmpty(updatecheck.Value))
346
                        {
347
                            CallUpdateProcess(updatecheck.Value);
348
                        }
349
                        this.Close();
350
                    }
351
                    break;
352
            }
353
        }
354
        
355
        private void SaveCheck()
356
        {
357
            if (ViewerDataModel.Instance.UndoDataList.Count > 0)
358
            {
359
                DateTime undoTime = ViewerDataModel.Instance.UndoDataList.OrderByDescending(order => order.EventTime).FirstOrDefault().EventTime;
360
                DateTime updatetime = DateTime.Now.AddDays(-1);
361
                if (ViewerDataModel.Instance._markupInfoList.Count > 0)
362
                {
363
                    updatetime = ViewerDataModel.Instance._markupInfoList.OrderByDescending(order => order.UpdateTime).FirstOrDefault().UpdateTime;
364
                }
365

    
366
                if (undoTime > updatetime)
367
                {
368
                    DialogParameters parameters = new DialogParameters()
369
                    {
370
                        Owner = Application.Current.MainWindow,
371
                        Content = new TextBlock()
372
                        {
373
                            MinWidth = 400,
374
                            FontSize = 11,
375
                            Text = "Found unsaved comments. Save now?",
376
                            TextWrapping = System.Windows.TextWrapping.Wrap
377
                        },
378
                        Header = "Confirm",
379
                        Theme = new VisualStudio2013Theme(),
380
                        ModalBackground = new SolidColorBrush { Color = Colors.Black, Opacity = 0.6 },
381
                        Closed = OnClosingSave,
382
                    };
383
                    RadWindow.Confirm(parameters);
384
                }
385
                else
386
                {
387
                    isSaveCheck = true;
388
                }                    
389
            }else
390
            {
391
                isSaveCheck = true;
392
            }
393
        }
394
        private void OnClosingSave(object sender, WindowClosedEventArgs e)
395
        {
396
            if (e.DialogResult == true)
397
            {
398
                dzTopMenu.SaveEventCallback(null, null);
399
            }
400
            isSaveCheck = true;
401
        }
402
                
403
        private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
404
        {
405
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
406
            {
407
                double bytesIn = double.Parse(e.BytesReceived.ToString());
408
                double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
409
                double percentage = bytesIn / totalBytes * 100;
410
                progressControl.splashText.Text = "Download : " + Math.Truncate(percentage).ToString() + " %";
411
                progressControl.progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());                
412
            }));
413
        }
414

    
415
        /// <summary>
416
        /// KCOM_API 를 통해 업데이트 URL 을 가져옴.
417
        /// false : 업데이트 불필요.
418
        /// true : 업데이트 필요. url 을 같이 Return.
419
        /// </summary>
420
        /// <returns></returns>
421
        private KeyValuePair<bool, string> UpdateCheck()
422
        {
423
            bool isUpdateCheck = false;
424
            string updateurl = string.Empty;
425
            try
426
            {
427
                bool is64bit = Environment.Is64BitProcess;
428
                string clientversion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
429
                updateurl = Common.ViewerDataModel.Instance.SystemMain.dzMainMenu.BaseClient.GetVersionData(is64bit, clientversion);
430
                
431
                if (Check_Uri.isUri(updateurl))
432
                {                          
433
                    DialogParameters parameters = new DialogParameters()
434
                    {
435
                        Owner = Application.Current.MainWindow,
436
                        Content = new TextBlock()
437
                        {
438
                            MinWidth = 400,
439
                            FontSize = 11,
440
                            Text = "Update is available. \n Update now?",
441
                            TextWrapping = System.Windows.TextWrapping.Wrap
442
                        },
443
                        Header = "Confirm",
444
                        Theme = new VisualStudio2013Theme(),
445
                        ModalBackground = new SolidColorBrush { Color = Colors.Black, Opacity = 0.6 },                        
446
                        Closed = delegate (object windowSender, WindowClosedEventArgs e)
447
                        {
448
                            if (e.DialogResult == true)
449
                            {   
450
                                isUpdateCheck = true;                                
451
                            }
452
                            else
453
                            {
454
                                isUpdateCheck = false;
455
                            }                            
456
                        }                    
457
                    };
458
                    RadWindow.Confirm(parameters);       
459
                    
460
                }
461
                else
462
                {
463
                    isUpdateCheck = false;
464
                }
465
            }
466
            catch (Exception ex)
467
            {
468
                throw ex;
469
            }
470
            return new KeyValuePair<bool, string>(isUpdateCheck, updateurl);
471
        }
472

    
473
        /// <summary>
474
        /// SmartUpdate 를 호출.
475
        /// </summary>
476
        /// <param name="updateurl">Download 할 설치파일 경로</param>
477
        private void CallUpdateProcess(string updateurl)
478
        {
479
            ProcessStartInfo proInfo = new ProcessStartInfo();
480
            string smartupdaterpath = string.Empty;
481
            smartupdaterpath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SmartUpdate.exe");
482
            Process.Start(smartupdaterpath, updateurl);
483
        }
484
        private async void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
485
        {
486
            try
487
            {
488
                await Dispatcher.InvokeAsync(()=> progressControl.splashText.Text = "Download Completed");
489

    
490
                if(progressControl != null)
491
                {
492
                    progressControl.Close();
493
                    progressControl = null;
494
                }
495
                if(File.Exists(destfilepath))
496
                {
497
                    ProcessStartInfo update_msi = new ProcessStartInfo();
498
                    update_msi.FileName = destfilepath;
499
                    Process.Start(update_msi);
500
                }                
501
                this.Close();
502
            }
503
            catch (Exception)
504
            {
505
                throw;
506
            }
507
            
508
        }
509

    
510
        void win_SourceInitialized(object sender, EventArgs e)
511
        {
512
            //System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
513
            //WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
514
        }
515

    
516
        private static System.IntPtr WindowProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled)
517
        {
518
            switch (msg)
519
            {
520
                case 0x0024:
521
                    WmGetMinMaxInfo(hwnd, lParam);
522
                    handled = true;
523
                    break;
524
            }
525
            return (System.IntPtr)0;
526
        }
527

    
528
        private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
529
        {
530
            MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
531
            int MONITOR_DEFAULTTONEAREST = 0x00000002;
532
            System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
533

    
534
            if (monitor != System.IntPtr.Zero)
535
            {
536
                MONITORINFO monitorInfo = new MONITORINFO();
537
                GetMonitorInfo(monitor, monitorInfo);
538
                RECT rcWorkArea = monitorInfo.rcWork;
539
                RECT rcMonitorArea = monitorInfo.rcMonitor;
540
                mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
541
                mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
542
                mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
543
                mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
544
            }
545
            Marshal.StructureToPtr(mmi, lParam, true);
546
        }
547

    
548
        [StructLayout(LayoutKind.Sequential)]
549
        public struct POINT2
550
        {
551
            public int x;
552
            public int y;
553
            public POINT2(int x, int y)
554
            {
555
                this.x = x;
556
                this.y = y;
557
            }
558
        }
559

    
560
        [StructLayout(LayoutKind.Sequential)]
561
        public struct MINMAXINFO
562
        {
563
            public POINT2 ptReserved;
564
            public POINT2 ptMaxSize;
565
            public POINT2 ptMaxPosition;
566
            public POINT2 ptMinTrackSize;
567
            public POINT2 ptMaxTrackSize;
568
        };
569

    
570
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
571
        public class MONITORINFO
572
        {
573
            public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
574
            public RECT rcMonitor = new RECT();
575
            public RECT rcWork = new RECT();
576
            public int dwFlags = 0;
577
        }
578

    
579
        [StructLayout(LayoutKind.Sequential, Pack = 0)]
580
        public struct RECT
581
        {
582
            public int left;
583
            public int top;
584
            public int right;
585
            public int bottom;
586

    
587
            public static readonly RECT Empty = new RECT();
588

    
589
            public int Width
590
            {
591
                get { return Math.Abs(right - left); }  // Abs needed for BIDI OS
592
            }
593

    
594
            public int Height
595
            {
596
                get { return bottom - top; }
597
            }
598

    
599
            public RECT(int left, int top, int right, int bottom)
600
            {
601
                this.left = left;
602
                this.top = top;
603
                this.right = right;
604
                this.bottom = bottom;
605
            }
606

    
607
            public RECT(RECT rcSrc)
608
            {
609
                this.left = rcSrc.left;
610
                this.top = rcSrc.top;
611
                this.right = rcSrc.right;
612
                this.bottom = rcSrc.bottom;
613
            }
614

    
615
            public bool IsEmpty
616
            {
617
                get
618
                {
619
                    // BUGBUG : On Bidi OS (hebrew arabic) left > right
620
                    return left >= right || top >= bottom;
621
                }
622
            }
623

    
624
            public override bool Equals(object obj)
625
            {
626
                if (!(obj is Rect)) { return false; }
627
                return (this == (RECT)obj);
628
            }
629

    
630
            public override int GetHashCode()
631
            {
632
                return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
633
            }
634

    
635
            public static bool operator ==(RECT rect1, RECT rect2)
636
            {
637
                return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
638
            }
639

    
640
            public static bool operator !=(RECT rect1, RECT rect2)
641
            {
642
                return !(rect1 == rect2);
643
            }
644
        }
645

    
646
        [DllImport("user32")]
647
        internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
648
        [DllImport("User32")]
649
        internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
650
    }
651
}
클립보드 이미지 추가 (최대 크기: 500 MB)