-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
164 lines (133 loc) · 5.52 KB
/
Form1.cs
File metadata and controls
164 lines (133 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using CefSharp;
using CefSharp.Enums;
using CefSharp.WinForms;
namespace Optimade
{
public partial class Optimade : Form
{
//Wndproc Messages
const int WM_MOUSEMOVE = 0x0200;
const int WM_MOUSELEAVE = 0x02A3;
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
//The initialized browser instance.
public CefSharp Browser;
public Optimade()
{
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Browser = new CefSharp();
Browser.chromeBrowser.DragHandler = new DragDropHandler();
// Add it to the form and fill it to the form window.
this.Controls.Add(Browser.chromeBrowser);
Browser.chromeBrowser.Dock = DockStyle.Fill;
Browser.chromeBrowser.IsBrowserInitializedChanged += ChromeBrowser_IsBrowserInitializedChanged;
Browser.JavascriptMessageChanged += OnJavascriptMessage;
}
//Intercepts Javascript messages and acts accordingly.
private void OnJavascriptMessage(object sender, JavascriptMessageChangedArgs args)
{
Console.WriteLine(args.msg);
switch (args.msg)
{
case CefSharp.Messages.Maximized:
{
this.Invoke(new MethodInvoker(delegate ()
{
this.WindowState = FormWindowState.Maximized;
}));
break;
}
case CefSharp.Messages.Minimized:
{
this.Invoke(new MethodInvoker(delegate ()
{
this.WindowState = FormWindowState.Normal;
}));
break;
}
case CefSharp.Messages.Hidden:
{
this.Invoke(new MethodInvoker(delegate ()
{
this.WindowState = FormWindowState.Minimized;
}));
break;
}
case CefSharp.Messages.Close:
{
Application.Exit();
break;
}
}
}
private void ChromeBrowser_IsBrowserInitializedChanged(object sender, EventArgs args)
{
ChromeWidgetMessageInterceptor.SetupLoop(Browser.chromeBrowser, (message) =>
{
if (message.Msg == WM_LBUTTONDOWN)
{
Point point = new Point(message.LParam.ToInt32());
if (((DragDropHandler)Browser.chromeBrowser.DragHandler).draggableRegion.IsVisible(point))
{
ReleaseCapture();
SendHandleMessage();
}
}
});
}
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
static extern IntPtr CreateRoundRectRgn
(int nLeftRect, // x-coordinate of upper-left corner.
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // width of ellipse
int nHeightEllipse // height of ellipse
);
//Makes the window have it's elliptical shape.
protected override void OnActivated(EventArgs e)
{
this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
//Updates the window and browser's shape and size.
protected override void OnSizeChanged(EventArgs e)
{
this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
//If statement is present here because OnSizeChanged triggers before CefSharp is initialized.
if (Browser != null)
{
Browser.chromeBrowser.Size = this.Size;
//Compensates for automatic resizing upon dragging the window when in fullscreen.
if (this.WindowState == FormWindowState.Normal)
{
Browser.chromeBrowser.ExecuteScriptAsyncWhenPageLoaded(@"
document.getElementById('size_img').src = 'MaximizeButton.png';
maximized = false;");
}
}
}
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public void SendHandleMessage()
{
if (InvokeRequired) { Invoke(new SendHandleMessageDelegate(SendHandleMessage), new object[] { }); return; }
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
public delegate void SendHandleMessageDelegate();
}
}