/// <summary> /// Attempts to get the adjacent location to NotifyIcon using specified window width and height. /// </summary> /// <param name="windowWidth">Window width</param> /// <param name="windowHeight">Window height</param> /// <param name="location">Location of window</param> /// <returns>True if successfully gets</returns> protected bool TryGetAdjacentLocationToTaskbar(double windowWidth, double windowHeight, out Rect location) { if (!WindowHelper.TryGetTaskbar(out Rect taskbarRect, out TaskbarAlignment taskbarAlignment)) { location = default; return(false); } var iconPlacement = IconPlacement.Unknown; var overflowAreaRect = default(Rect); if (NotifyIconHelper.TryGetNotifyIconRect(_notifyIcon, out Rect iconRect)) { if (taskbarRect.Contains(iconRect)) { iconPlacement = IconPlacement.InTaskbar; } else if (WindowHelper.TryGetOverflowAreaRect(out overflowAreaRect)) { iconPlacement = IconPlacement.InOverflowArea; } } if (!WindowHelper.TryGetDwmWindowMargin(_window, out Thickness windowMargin)) { windowMargin = new Thickness(0); // Fallback } double x = 0, y = 0; switch (taskbarAlignment) { case TaskbarAlignment.Top: case TaskbarAlignment.Bottom: switch (iconPlacement) { case IconPlacement.InTaskbar: x = iconRect.Right; break; case IconPlacement.InOverflowArea: x = overflowAreaRect.Left; break; default: x = taskbarRect.Right; // Fallback break; } x -= (windowWidth - windowMargin.Right); switch (taskbarAlignment) { case TaskbarAlignment.Top: y = taskbarRect.Bottom - windowMargin.Top; PivotAlignment = PivotAlignment.TopRight; break; case TaskbarAlignment.Bottom: y = taskbarRect.Top - (windowHeight - windowMargin.Bottom); PivotAlignment = PivotAlignment.BottomRight; break; } break; case TaskbarAlignment.Left: case TaskbarAlignment.Right: switch (taskbarAlignment) { case TaskbarAlignment.Left: x = taskbarRect.Right - windowMargin.Left; PivotAlignment = PivotAlignment.BottomLeft; break; case TaskbarAlignment.Right: x = taskbarRect.Left - (windowWidth - windowMargin.Right); PivotAlignment = PivotAlignment.BottomRight; break; } switch (iconPlacement) { case IconPlacement.InTaskbar: y = iconRect.Bottom; break; case IconPlacement.InOverflowArea: y = overflowAreaRect.Top; break; default: y = taskbarRect.Bottom; // Fallback break; } y -= (windowHeight - windowMargin.Bottom); break; } location = new Rect(x, y, windowWidth, windowHeight); return(true); }
private void MainForm_Load(object sender, EventArgs e) { FormBackup = this; Save.FriendsList = JsonConvert.DeserializeObject <FriendsList>(SendRequest($@"{Save.url}v1/LuaApiCaller?qq={Save.curentQQ}&funcname=GetQQUserList&timeout=10", "{\"StartIndex\":0}")); Save.GroupList = JsonConvert.DeserializeObject <GroupList>(SendRequest($@"{Save.url}v1/LuaApiCaller?qq={Save.curentQQ}&funcname=GetGroupList&timeout=10", "{\"NextToken\":\"\"}")); Thread getGroupInfo_Thread = new Thread(() => { foreach (var item in Save.GroupList.TroopList) { item.GroupMemberList = JsonConvert.DeserializeObject <GroupMemberList>(SendRequest($@"{Save.url}v1/LuaApiCaller?qq={Save.curentQQ}&funcname=GetGroupUserList&timeout=10", $"{{\"GroupUin\":{item.GroupId},\"LastUin\":0}}")); } }); getGroupInfo_Thread.Start(); //根据登录时的QQ号 获取QQ昵称 string name = Save.FriendsList.Friendlist.First(x => x.FriendUin == Save.curentQQ).NickName.ToString(); Save.name = name; if (!Directory.Exists("conf")) { Directory.CreateDirectory("conf"); } if (File.Exists(LogHelper.GetLogFilePath()) is false) { LogHelper.CreateDB(); } //读取窗口相关配置 if (File.Exists(@"conf\states.json")) { AppConfig = JObject.Parse(File.ReadAllText(@"conf\states.json")); } //配置文件为空或不包含配置相关键 if (AppConfig.Count == 0 || !AppConfig.ContainsKey("Main")) { JObject config = new JObject { new JProperty("FormX", Left), new JProperty("FormY", Top), new JProperty("ShowWindow", true), new JProperty("TopMost", true), new JProperty("ExpandWindow", false) }; AppConfig.Add(new JProperty("Main", config)); File.WriteAllText(@"conf\states.json", AppConfig.ToString()); } else//存在并读取配置 { Left = Convert.ToInt32(AppConfig["Main"]["FormX"].ToString()); Top = Convert.ToInt32(AppConfig["Main"]["FormY"].ToString()); //不直接给Visable赋值是因为外部调用Show函数会覆盖对Visable的赋值 //所以在调用Show之后需要用标志位恢复对Visable的变化值 ShowFlag = bool.Parse(AppConfig["Main"]["ShowWindow"].ToString()); TopFlag = bool.Parse(AppConfig["Main"]["TopMost"].ToString()); ExpandWindow(bool.Parse(AppConfig["Main"]["ExpandWindow"].ToString())); } //设置窗口透明色, 实现窗口背景透明 this.TransparencyKey = Color.Gray; this.BackColor = Color.Gray; //隐藏日志窗口, 但不关闭 logForm.Text = $"运行日志 - {Save.curentQQ}"; logForm.Show(); logForm.Visible = false; //初始化托盘 NotifyIconHelper._NotifyIcon = notifyIcon; NotifyIconHelper.Init(); NotifyIconHelper.ShowNotifyIcon(); //载入插件 pluginManagment = new PluginManagment(); Thread thread = new Thread(() => { pluginManagment.Init(); }); thread.Start(); //将托盘右键菜单复制一份 pictureBox_Main.ContextMenu = notifyIcon.ContextMenu; //实例化圆形图片框, 实现圆形的头像 HttpWebClient http = new HttpWebClient() { TimeOut = 3000 }; byte[] data = { }; //默认头像,防止网络问题造成空头像出现 Image image = Image.FromFile(@"conf\DefaultPic.jpeg"); try { data = http.DownloadData($"http://q1.qlogo.cn/g?b=qq&nk={Save.curentQQ}&s=640"); MemoryStream ms = new MemoryStream(data); if (ms.Length > 0)//下载成功 { image = Image.FromStream(ms); } else//下载失败 { image = Image.FromFile(@"conf\DefaultPic.jpeg"); } } catch { //网络异常,图片使用默认头像 LogHelper.WriteLog("下载头像超时,重新启动程序可能解决这个问题"); } RoundPictureBox RoundpictureBox = new RoundPictureBox { Size = new Size(43, 43), SizeMode = PictureBoxSizeMode.StretchImage, Left = -1, Top = 0, ContextMenu = notifyIcon.ContextMenu }; if (image != null) { RoundpictureBox.Image = image; } //添加拖动事件 RoundpictureBox.MouseDown += pictureBox_Main_MouseDown; //显示控件, 置顶 this.Controls.Add(RoundpictureBox); RoundpictureBox.BringToFront(); RoundpictureBox.MouseDoubleClick += RoundpictureBox_MouseDoubleClick; Loaded = true; Save.LoginStatus = true; //事件处理 SocketHandler(); //置顶维护时钟,每60秒将窗口重新置顶 Timer.Interval = 60000; Timer.Tick += CheckTopMost; Timer.Start(); }
/// <summary> /// Attempts to get the adjacent location to NotifyIcon using specified window width and height. /// </summary> /// <param name="windowWidth">Window width</param> /// <param name="windowHeight">Window height</param> /// <param name="location">Location of window</param> /// <returns>True if successfully gets</returns> protected bool TryGetAdjacentLocationToTaskbar(double windowWidth, double windowHeight, out Rect location) { if (!WindowHelper.TryGetTaskbar(out Rect taskbarRect, out TaskbarAlignment taskbarAlignment)) { location = default; return(false); } var iconPlacement = IconPlacement.Unknown; var overflowAreaRect = default(Rect); if (NotifyIconHelper.TryGetNotifyIconRect(_notifyIcon, out Rect iconRect)) { if (taskbarRect.Contains( iconRect.X + iconRect.Width / 2D, iconRect.Y + iconRect.Height / 2D)) { iconPlacement = IconPlacement.InTaskbar; } else if (WindowHelper.TryGetOverflowAreaRect(out overflowAreaRect) && overflowAreaRect.Contains(iconRect)) { iconPlacement = IconPlacement.InOverflowArea; } } if (!WindowHelper.TryGetDwmWindowMargin(_window, out Thickness windowMargin)) { windowMargin = new Thickness(0); // Fallback } var isLeftToRight = !CultureInfoAddition.UserDefaultUICulture.TextInfo.IsRightToLeft; double x = 0, y = 0; switch (taskbarAlignment) { case TaskbarAlignment.Top: case TaskbarAlignment.Bottom: x = iconPlacement switch { IconPlacement.InTaskbar => isLeftToRight ? iconRect.Right : iconRect.Left, IconPlacement.InOverflowArea => isLeftToRight ? overflowAreaRect.Left : overflowAreaRect.Right, _ => isLeftToRight ? taskbarRect.Right : taskbarRect.Left, // Fallback }; x -= isLeftToRight ? (windowWidth - windowMargin.Right) : windowMargin.Left; switch (taskbarAlignment) { case TaskbarAlignment.Top: y = taskbarRect.Bottom - windowMargin.Top; PivotAlignment = isLeftToRight ? PivotAlignment.TopRight : PivotAlignment.TopLeft; break; case TaskbarAlignment.Bottom: y = taskbarRect.Top - (windowHeight - windowMargin.Bottom); PivotAlignment = isLeftToRight ? PivotAlignment.BottomRight : PivotAlignment.BottomLeft; break; } break; case TaskbarAlignment.Left: case TaskbarAlignment.Right: switch (taskbarAlignment) { case TaskbarAlignment.Left: x = taskbarRect.Right - windowMargin.Left; PivotAlignment = PivotAlignment.BottomLeft; break; case TaskbarAlignment.Right: x = taskbarRect.Left - (windowWidth - windowMargin.Right); PivotAlignment = PivotAlignment.BottomRight; break; } y = iconPlacement switch { IconPlacement.InTaskbar => iconRect.Bottom, IconPlacement.InOverflowArea => overflowAreaRect.Top, _ => taskbarRect.Bottom, // Fallback }; y -= (windowHeight - windowMargin.Bottom); break; } location = new Rect(x, y, windowWidth, windowHeight); return(true); }
/// <summary> /// Attempts to get the adjacent location to NotifyIcon using specified window width and height. /// </summary> /// <param name="windowWidth">Window width</param> /// <param name="windowHeight">Window height</param> /// <param name="location">Location of window</param> /// <returns>True if successfully gets</returns> protected bool TryGetAdjacentLocationToTaskbar(double windowWidth, double windowHeight, out Rect location) { if (!WindowHelper.TryGetTaskbar(out Rect taskbarRect, out TaskbarAlignment taskbarAlignment, out bool isShown)) { location = default; return(false); } var iconPlacement = IconPlacement.Unknown; var overflowAreaRect = default(Rect); if (NotifyIconHelper.TryGetNotifyIconRect(_notifyIcon, out Rect iconRect)) { if (taskbarRect.Contains( iconRect.X + iconRect.Width / 2D, iconRect.Y + iconRect.Height / 2D)) { iconPlacement = IconPlacement.InTaskbar; } else if (WindowHelper.TryGetOverflowAreaRect(out overflowAreaRect) && overflowAreaRect.Contains(iconRect)) { iconPlacement = IconPlacement.InOverflowArea; } } if (!WindowHelper.TryGetDwmWindowMargin(_window, out Thickness windowMargin)) { windowMargin = new Thickness(0); // Fallback } var isLeftToRight = !CultureInfoAddition.UserDefaultUICulture.TextInfo.IsRightToLeft; double x = 0, y = 0; // To avoid a gap between window and taskbar when taskbar alignment is right or bottom // and monitor DPI is 125%, 150%, 175%, the window width and height (in DIP) must be // a multiple of 4. Otherwise, the window width and height multiplied with those DPI // will have a fraction and it will cause a blurry edge looking as if there is a gap. switch (taskbarAlignment) { case TaskbarAlignment.Top: case TaskbarAlignment.Bottom: x = iconPlacement switch { IconPlacement.InTaskbar => isLeftToRight ? iconRect.Right : iconRect.Left, IconPlacement.InOverflowArea => isLeftToRight ? overflowAreaRect.Left : overflowAreaRect.Right, _ => isLeftToRight ? taskbarRect.Right : taskbarRect.Left, // Fallback }; x -= isLeftToRight ? (windowWidth - windowMargin.Right) : windowMargin.Left; switch (taskbarAlignment) { case TaskbarAlignment.Top: y = (isShown ? taskbarRect.Bottom : taskbarRect.Top) - windowMargin.Top; PivotAlignment = isLeftToRight ? PivotAlignment.TopRight : PivotAlignment.TopLeft; break; case TaskbarAlignment.Bottom: y = (isShown ? taskbarRect.Top : taskbarRect.Bottom) - (windowHeight - windowMargin.Bottom); PivotAlignment = isLeftToRight ? PivotAlignment.BottomRight : PivotAlignment.BottomLeft; break; } break; case TaskbarAlignment.Left: case TaskbarAlignment.Right: switch (taskbarAlignment) { case TaskbarAlignment.Left: x = (isShown ? taskbarRect.Right : taskbarRect.Left) - windowMargin.Left; PivotAlignment = PivotAlignment.BottomLeft; break; case TaskbarAlignment.Right: x = (isShown ? taskbarRect.Left : taskbarRect.Right) - (windowWidth - windowMargin.Right); PivotAlignment = PivotAlignment.BottomRight; break; } y = iconPlacement switch { IconPlacement.InTaskbar => iconRect.Bottom, IconPlacement.InOverflowArea => overflowAreaRect.Top, _ => taskbarRect.Bottom, // Fallback }; y -= (windowHeight - windowMargin.Bottom); break; } location = new Rect(x, y, windowWidth, windowHeight); return(true); }
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { NotifyIconHelper.RemoveNotifycation(); }