Visual Studio 2019 C# Winforms 相关记录
改变窗体
this.SizeChanged += Form1_SizeChanged;
...
private void Form1_SizeChanged(object sender, EventArgs e){
//操作代码
}
NotifyIcon1鼠标左右键
private void NotifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//右键
}
if (e.Button == MouseButtons.Left)
{
//左键
}
}
禁止CefSharp ChromiumWebBrowser新窗口打开
public class CefLifeSpanHandler : ILifeSpanHandler
{
public CefLifeSpanHandler()
{
}
public bool DoClose(IWebBrowser browserControl, IBrowser browser)
{
if (browser.IsDisposed || browser.IsPopup)
{
return false;
}
return true;
}
public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
{
}
public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
{
}
// 阻止打开新的窗体
public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
newBrowser = null;
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
chromiumWebBrowser.Load(targetUrl);
return true;
}
}
控件设置
public void InitializeChromium()
{
CefSettings settings = new CefSettings() { IgnoreCertificateErrors = true };
Cef.Initialize(settings);
web.LifeSpanHandler = new CefLifeSpanHandler();
web.BrowserSettings = new BrowserSettings()
{
WebGl = CefState.Enabled,
ImageLoading = CefState.Enabled,
RemoteFonts = CefState.Enabled,
AcceptLanguageList = "zh-CN"
};
}
浏览器缓存配置
var settings = new CefSettings()
{
Locale = "zh-CN",
CachePath = Directory.GetCurrentDirectory() + @"\Cache",
IgnoreCertificateErrors = true
};
settings.CefCommandLineArgs.Add("proxy-auto-detect", "0");
settings.CefCommandLineArgs.Add("no-proxy-server", "1");
/*初始化配置*/
Cef.Initialize(settings);
禁止程序多开,Program.cs
static class Program
{
static readonly Mutex mutex = new Mutex(true, "MyMutex");
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
mutex.ReleaseMutex();
}
else
{
MessageBox.Show("程序已经在运行中!","提示");
}
}
}
圆角:控件
class DesignForm
{
public void Type(Control sender, int p_1, double p_2)
{
GraphicsPath oPath = new GraphicsPath();
oPath.AddClosedCurve(new Point[] { new Point(0, sender.Height / p_1), new Point(sender.Width / p_1, 0), new Point(sender.Width - sender.Width / p_1, 0), new Point(sender.Width, sender.Height / p_1), new Point(sender.Width, sender.Height - sender.Height / p_1), new Point(sender.Width - sender.Width / p_1, sender.Height), new Point(sender.Width / p_1, sender.Height), new Point(0, sender.Height - sender.Height / p_1) }, (float)p_2);
sender.Region = new Region(oPath);
}
}
调用方式:
public partial class Form1 : Form
{
readonly DesignForm DDesignF = new DesignForm();
//...
DDesignF.Type(控件名, 15, 0.2);
控件圆形
public void region(Control sender)
{
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(sender.ClientRectangle);
Region region = new Region(gp);
sender.Region = region;
gp.Dispose();
region.Dispose();
}
调用:
DDesignF.region(QRClose);
交互,点击浏览器上元素
Pweb.EvaluateScriptAsync("document.querySelector('.play-pause').click()");