Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Update 参考图菜单增加鼠标滚动
Browse files Browse the repository at this point in the history
  • Loading branch information
LJLCarrien committed Apr 11, 2020
1 parent f6ae7bd commit cb41be6
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 29 deletions.
2 changes: 1 addition & 1 deletion AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[assembly: System.Runtime.InteropServices.ComVisible(false)]
[assembly: System.Runtime.InteropServices.Guid("4483e561-8b3e-427d-98a4-e0e821b7bf2f")]
[assembly: System.Reflection.AssemblyConfiguration("")]
[assembly: System.Reflection.AssemblyFileVersion("2.5.7")]
[assembly: System.Reflection.AssemblyFileVersion("2.5.8")]
[assembly: System.Runtime.CompilerServices.CompilationRelaxations(8)]
[assembly: System.Runtime.CompilerServices.RuntimeCompatibility(WrapNonExceptionThrows = true)]

22 changes: 22 additions & 0 deletions SETUNA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@
<PropertyGroup>
<StartupObject>Program</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Compile Include="Properties\Image.Designer.cs">
Expand Down Expand Up @@ -301,13 +321,15 @@
<Compile Include="SETUNA\Main\ToolStripDustboxList.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SETUNA\Main\ToolStripEx.cs" />
<Compile Include="SETUNA\Main\ToolStripScrapList.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SETUNA\Main\ToolStripStyleButton.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Program.cs" />
<Reference Include="System.Data" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System" />
<Reference Include="System.Drawing" />
Expand Down
10 changes: 8 additions & 2 deletions SETUNA.sln
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.329
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SETUNA", "SETUNA.csproj", "{F50B9DA0-2DEA-464A-B568-888E54701D07}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F50B9DA0-2DEA-464A-B568-888E54701D07}.Debug|x64.ActiveCfg = Debug|x64
{F50B9DA0-2DEA-464A-B568-888E54701D07}.Debug|x64.Build.0 = Debug|x64
{F50B9DA0-2DEA-464A-B568-888E54701D07}.Debug|x86.ActiveCfg = Debug|x86
{F50B9DA0-2DEA-464A-B568-888E54701D07}.Debug|x86.Build.0 = Debug|x86
{F50B9DA0-2DEA-464A-B568-888E54701D07}.Release|x64.ActiveCfg = Release|x64
{F50B9DA0-2DEA-464A-B568-888E54701D07}.Release|x64.Build.0 = Release|x64
{F50B9DA0-2DEA-464A-B568-888E54701D07}.Release|x86.ActiveCfg = Release|x86
{F50B9DA0-2DEA-464A-B568-888E54701D07}.Release|x86.Build.0 = Release|x86
EndGlobalSection
Expand Down
23 changes: 22 additions & 1 deletion SETUNA/Main/ToolStripAbstractList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace SETUNA.Main
{
using Opulos.Core.UI;
using System;
using System.Collections;
using System.Windows.Forms;
Expand All @@ -14,6 +15,8 @@ public ToolStripAbstractList(string text, ScrapBook scrapbook) : base(text)
this._scrapbook = scrapbook;
base.DropDownItems.Clear();
base.DropDownItems.Insert(0, new ToolStripMenuItem(""));

ToolStripEx.BigButtons(this.DropDown);
}

protected abstract ArrayList GetItems();
Expand All @@ -22,8 +25,25 @@ protected virtual void OnAddItem(ToolStripMenuItem addmi)
{
}

protected override void OnDropDownOpened(EventArgs e)
{
Mainform.instance.MouseWheelCallbackEvent += DropDown_MouseWheel;
this.DropDown.MouseWheel += DropDown_MouseWheel;

base.OnDropDownOpened(e);
}

private void DropDown_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0) SendKeys.SendWait("{UP}");
else SendKeys.SendWait("{DOWN}");
}

protected override void OnDropDownClosed(EventArgs e)
{
this.DropDown.MouseWheel -= DropDown_MouseWheel;
Mainform.instance.MouseWheelCallbackEvent -= DropDown_MouseWheel;

this._createdlist = false;
base.OnDropDownClosed(e);
}
Expand All @@ -43,7 +63,8 @@ protected void RefreshList()
{
foreach (ScrapBase base2 in this.GetItems())
{
ToolStripMenuItem addmi = new ToolStripMenuItem(string.Concat(new object[] { base2.Name, "\n", base2.Width, " x ", base2.Height }), base2.GetThumbnail(), new EventHandler(this._scrapbook.BindForm.OnActiveScrapInList)) {
ToolStripMenuItem addmi = new ToolStripMenuItem(string.Concat(new object[] { base2.Name, "\n", base2.Width, " x ", base2.Height }), base2.GetThumbnail(), new EventHandler(this._scrapbook.BindForm.OnActiveScrapInList))
{
Tag = base2,
ImageScaling = ToolStripItemImageScaling.None
};
Expand Down
145 changes: 145 additions & 0 deletions SETUNA/Main/ToolStripEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Opulos.Core.UI
{
//https://stackoverflow.com/questions/29083903/contextmenustrip-scroll-up-or-down-button
///<summary>Extension class to increase the size of the scroll up-down arrows on a drop down context menu or tool strip menu. The default up-down arrows are only 5 pixels high.</summary>
public static class ToolStripEx
{
public delegate void Action();
private static Hashtable htData = new Hashtable();

private class Data
{
public bool needsUpdate = true;
public bool disposeLastImage = false;
public ToolStrip toolStrip = null;
public List<Image> currentImages = new List<Image>();
}

public static void BigButtons(ToolStrip toolStrip)
{
htData[toolStrip] = new Data() { toolStrip = toolStrip };
toolStrip.VisibleChanged += toolStrip_VisibleChanged;
toolStrip.ForeColorChanged += toolStrip_ForeColorChanged;
toolStrip.Disposed += toolStrip_Disposed;
}

static void toolStrip_Disposed(object sender, EventArgs e)
{
Data d = (Data)htData[sender];
if (d != null && d.currentImages != null)
{
foreach (var img in d.currentImages)
img.Dispose();
d.currentImages = null;
htData.Remove(sender);
}
}

static void toolStrip_ForeColorChanged(object sender, EventArgs e)
{
Data d = (Data)htData[sender];
d.needsUpdate = true;
UpdateImages(d);
}

static void toolStrip_VisibleChanged(object sender, EventArgs e)
{
Data d = (Data)htData[sender];
UpdateImages(d);
}

private static void UpdateImages(Data d)
{
if (!d.needsUpdate)
return;

Action a = () =>
{
try
{
var list = GetChildWindows(d.toolStrip.Handle);
if (list.Count == 0)
return;
List<Image> newImages = new List<Image>();
int k = 0;
foreach (var i in list)
{
var c = Control.FromHandle(i) as Label;
if (c != null && d.needsUpdate)
{
String glyph = (k == 0 ? "t" : "u");
using (Font f = new System.Drawing.Font("Marlett", 20f))
{
Size s = TextRenderer.MeasureText("t", f);
var oldImage = c.Image;
c.Image = new Bitmap(s.Width, s.Height);
newImages.Add(c.Image);
// avoid disposing the default image
// might cause problems, not sure
if (d.disposeLastImage)
oldImage.Dispose();
using (Graphics g = Graphics.FromImage(c.Image))
{
using (Brush b = new SolidBrush(d.toolStrip.ForeColor))
g.DrawString(glyph, f, b, 0, 0);
}
c.AutoSize = true;
}
k++;
}
}
if (newImages.Count > 0)
{
d.needsUpdate = false;
d.disposeLastImage = true;
d.currentImages = newImages;
}
}
catch { } // protect against crash (just in case)
};

d.toolStrip.BeginInvoke(a);
}

private static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumChildWindows(parent, enumProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}

private delegate bool EnumChildProc(IntPtr hWnd, IntPtr lParam);
private static EnumChildProc enumProc = new EnumChildProc(CallChildEnumProc);
private static bool CallChildEnumProc(IntPtr hWnd, IntPtr lParam)
{
GCHandle gch = GCHandle.FromIntPtr(lParam);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");

list.Add(hWnd);
return true;
}

[DllImport("user32.dll")]
private static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam);
}
}
36 changes: 14 additions & 22 deletions SETUNA/Mainform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ static public Mainform instance
private NotifyIcon setunaIcon;
private SETUNA.Main.ContextStyleMenuStrip setunaIconMenu;
private SETUNA.Main.ContextStyleMenuStrip subMenu;
private ToolStripMenuItem testToolStripMenuItem;
private Timer timPool;
private Button button4;
private Button button1;
private Timer timer1;
private ToolTip toolTip1;

public delegate void MouseWheelCallback(object sender, MouseEventArgs e);
public event MouseWheelCallback MouseWheelCallbackEvent;

public Mainform()
{
Expand Down Expand Up @@ -356,12 +357,9 @@ private void InitializeComponent()
this.setunaIcon = new System.Windows.Forms.NotifyIcon(this.components);
this.setunaIconMenu = new SETUNA.Main.ContextStyleMenuStrip(this.components);
this.subMenu = new SETUNA.Main.ContextStyleMenuStrip(this.components);
this.testToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.button4 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.subMenu.SuspendLayout();
this.SuspendLayout();
//
// timPool
Expand All @@ -387,25 +385,9 @@ private void InitializeComponent()
// subMenu
//
this.subMenu.ImageScalingSize = new System.Drawing.Size(36, 36);
this.subMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.testToolStripMenuItem});
this.subMenu.Name = "subMenu";
this.subMenu.Scrap = null;
this.subMenu.Size = new System.Drawing.Size(123, 38);
//
// testToolStripMenuItem
//
this.testToolStripMenuItem.Name = "testToolStripMenuItem";
this.testToolStripMenuItem.Size = new System.Drawing.Size(122, 34);
this.testToolStripMenuItem.Text = "test";
//
// toolTip1
//
this.toolTip1.IsBalloon = true;
this.toolTip1.ShowAlways = true;
this.toolTip1.StripAmpersands = true;
this.toolTip1.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info;
this.toolTip1.ToolTipTitle = "asfdadsf";
this.subMenu.Size = new System.Drawing.Size(61, 4);
//
// button4
//
Expand Down Expand Up @@ -778,6 +760,16 @@ public void ScrapMenuOpening(object sender, ScrapMenuArgs e)
{
this.subMenu.Scrap = e.scrap;
this.subMenu.Show(e.scrap, e.scrap.PointToClient(Cursor.Position));
this.subMenu.MouseWheel -= SubMenu_MouseWheel;
this.subMenu.MouseWheel += SubMenu_MouseWheel;
}

private void SubMenu_MouseWheel(object sender, MouseEventArgs e)
{
if(MouseWheelCallbackEvent != null)
{
MouseWheelCallbackEvent(sender, e);
}
}

public void ScrapRemoved(object sender, ScrapEventArgs e)
Expand Down
3 changes: 0 additions & 3 deletions SETUNA/Mainform.resx
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,6 @@
<metadata name="subMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>698, 17</value>
</metadata>
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>879, 17</value>
</metadata>
<metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>1248, 17</value>
</metadata>
Expand Down

0 comments on commit cb41be6

Please sign in to comment.