Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlwaysOnTop #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions EveRefinery/EveRefinery/EveRefinery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
</Compile>
<Compile Include="ItemsDB.cs" />
<Compile Include="MarketPricesDB.cs" />
<Compile Include="MouseWheelHook.cs" />
<Compile Include="PriceProviderAuto.cs" />
<Compile Include="PriceProviderEveCentralCom.cs" />
<Compile Include="PriceProviderEveMarketdataCom.cs" />
Expand Down
32 changes: 31 additions & 1 deletion EveRefinery/EveRefinery/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,19 @@ public FrmMain()
m_RunningListUpdates = 0;

InitializeComponent();
}

MouseWheelHook.MouseWheelUp += MouseWheelUp;
MouseWheelHook.MouseWheelDown += MouseWheelDown;

Activated += (sender, args) => MouseWheelHook.Stop();
Deactivate += (sender, args) =>
{
if (TopMost)
{
MouseWheelHook.Start();
}
};
}

abstract class CompareItemBase : IComparer
{
Expand Down Expand Up @@ -1061,6 +1073,24 @@ private void TlbChkAlwaysOnTop_CheckedChanged(object sender, EventArgs e)
TopMost = !TopMost;
}

private void MouseWheelUp(object sender, EventArgs e)
{
int curItem = LstRefinery.TopItem.Index;
if (curItem > 0)
{
LstRefinery.TopItem = LstRefinery.Items[curItem - 1];
}
}

private void MouseWheelDown(object sender, EventArgs e)
{
int curItem = LstRefinery.TopItem.Index;
if (curItem < LstRefinery.Items.Count)
{
LstRefinery.TopItem = LstRefinery.Items[curItem + 1];
}
}

private void TlbBtnExport_Click(object sender, EventArgs e)
{
SaveFileDialog fileDialog = new SaveFileDialog();
Expand Down
87 changes: 87 additions & 0 deletions EveRefinery/EveRefinery/MouseWheelHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace EveRefinery
{
public static class MouseWheelHook

{
public static event EventHandler MouseWheelUp = delegate { };
public static event EventHandler MouseWheelDown = delegate { };

public static void Start()
{
_hookId = SetHook(Proc);
}

public static void Stop()
{
UnhookWindowsHookEx(_hookId);
}

private static readonly LowLevelMouseProc Proc = HookCallback;
private static IntPtr _hookId = IntPtr.Zero;

private static IntPtr SetHook(LowLevelMouseProc proc)
{
{
IntPtr hook = SetWindowsHookEx(14, proc, GetModuleHandle("user32"), 0);
if (hook == IntPtr.Zero) throw new Win32Exception();
return hook;
}
}

private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
//intercept only WM_MOUSEWHEEL
if (nCode >= 0 && (IntPtr) 0x020A == wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
if (hookStruct.mouseData > 0)
{
MouseWheelUp(null, new EventArgs());
}
else
{
MouseWheelDown(null, new EventArgs());
}
}

return CallNextHookEx(_hookId, nCode, wParam, lParam);
}

[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public int mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}

private struct POINT
{
public int x;
public int y;
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}