1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-30 05:34:06 +02:00

Move native methods to a NativeMethods class

This commit is contained in:
chylex 2016-05-08 18:33:42 +02:00
parent 3a66fa28ab
commit 03a93ad3f3
7 changed files with 65 additions and 19 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
using TweetDck.Core.Utils;
namespace TweetDck.Core.Controls{ namespace TweetDck.Core.Controls{
public partial class RichTextLabel : RichTextBox{ public partial class RichTextLabel : RichTextBox{
@ -27,7 +28,7 @@ protected override CreateParams CreateParams{
get{ get{
CreateParams createParams = base.CreateParams; CreateParams createParams = base.CreateParams;
if (Program.LoadLibrary("msftedit.dll") != IntPtr.Zero){ if (NativeMethods.LoadLibrary("msftedit.dll") != IntPtr.Zero){
createParams.ClassName = "RICHEDIT50W"; createParams.ClassName = "RICHEDIT50W";
} }

View File

@ -7,6 +7,7 @@
using TweetDck.Configuration; using TweetDck.Configuration;
using TweetDck.Core.Handling; using TweetDck.Core.Handling;
using TweetDck.Resources; using TweetDck.Resources;
using TweetDck.Core.Utils;
namespace TweetDck.Core{ namespace TweetDck.Core{
sealed partial class FormNotification : Form{ sealed partial class FormNotification : Form{
@ -222,7 +223,7 @@ private void MoveToVisibleLocation(){
} }
if (needsReactivating){ if (needsReactivating){
Program.SetWindowPos(Handle.ToInt32(),-1,Left,Top,Width,Height,0x0010); // HWND_TOPMOST, SWP_NOACTIVATE NativeMethods.SetFormPos(this,NativeMethods.HWND_TOPMOST,NativeMethods.SWP_NOACTIVATE);
} }
} }

View File

@ -126,10 +126,9 @@ public void ClickUploadImage(int offsetX, int offsetY){
Point prevPos = Cursor.Position; Point prevPos = Cursor.Position;
Cursor.Position = form.PointToScreen(new Point(offsetX,offsetY)); Cursor.Position = form.PointToScreen(new Point(offsetX,offsetY));
Program.mouse_event(SystemInformation.MouseButtonsSwapped ? 0x08 : 0x02,Cursor.Position.X,Cursor.Position.Y,0,0); // MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_LEFTDOWN NativeMethods.SimulateMouseClick(NativeMethods.MouseButton.Left);
Program.mouse_event(SystemInformation.MouseButtonsSwapped ? 0x10 : 0x04,Cursor.Position.X,Cursor.Position.Y,0,0); // MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_LEFTUP
Cursor.Position = prevPos; Cursor.Position = prevPos;
form.OnImagePastedFinish(); form.OnImagePastedFinish();
}); });
} }

View File

@ -0,0 +1,56 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace TweetDck.Core.Utils{
static class NativeMethods{
public const int HWND_TOPMOST = -1;
public const uint SWP_NOACTIVATE = 0x0010;
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public enum MouseButton{
Left, Right
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr LoadLibrary(string name);
[DllImport("Shell32.dll")]
public static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(int hWnd, int hWndOrder, int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public static void SetFormPos(Form form, int hWndOrder, uint flags){
SetWindowPos(form.Handle.ToInt32(),hWndOrder,form.Left,form.Top,form.Width,form.Height,flags);
}
public static void SimulateMouseClick(MouseButton button){
int flagHold, flagRelease;
switch(button){
case MouseButton.Left:
flagHold = SystemInformation.MouseButtonsSwapped ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_LEFTDOWN;
flagRelease = SystemInformation.MouseButtonsSwapped ? MOUSEEVENTF_RIGHTUP : MOUSEEVENTF_LEFTUP;
break;
case MouseButton.Right:
flagHold = SystemInformation.MouseButtonsSwapped ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN;
flagRelease = SystemInformation.MouseButtonsSwapped ? MOUSEEVENTF_LEFTUP : MOUSEEVENTF_RIGHTUP;
break;
default: return;
}
mouse_event(flagHold,Cursor.Position.X,Cursor.Position.Y,0,0);
mouse_event(flagRelease,Cursor.Position.X,Cursor.Position.Y,0,0);
}
}
}

View File

@ -8,6 +8,7 @@
using Microsoft.Win32; using Microsoft.Win32;
using TweetDck.Core.Other; using TweetDck.Core.Other;
using TweetDck.Migration.Helpers; using TweetDck.Migration.Helpers;
using TweetDck.Core.Utils;
namespace TweetDck.Migration{ namespace TweetDck.Migration{
static class MigrationManager{ static class MigrationManager{
@ -143,7 +144,7 @@ private static bool BeginMigration(MigrationDecision decision, Action<Exception>
} }
} }
Program.SHChangeNotify(0x8000000,0x1000,IntPtr.Zero,IntPtr.Zero); // refreshes desktop NativeMethods.SHChangeNotify(0x8000000,0x1000,IntPtr.Zero,IntPtr.Zero); // refreshes desktop
// uninstall in the background // uninstall in the background
string guid = ProgramRegistrySearch.FindByDisplayName("TweetDeck"); string guid = ProgramRegistrySearch.FindByDisplayName("TweetDeck");

View File

@ -2,7 +2,6 @@
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using CefSharp; using CefSharp;
@ -38,18 +37,6 @@ public static string LogFile{
} }
} }
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr LoadLibrary(string name);
[DllImport("Shell32.dll")]
public static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(int hWnd, int hWndOrder, int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[STAThread] [STAThread]
private static void Main(){ private static void Main(){
Application.EnableVisualStyles(); Application.EnableVisualStyles();

View File

@ -139,6 +139,7 @@
<DependentUpon>TrayIcon.cs</DependentUpon> <DependentUpon>TrayIcon.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Core\Utils\BrowserUtils.cs" /> <Compile Include="Core\Utils\BrowserUtils.cs" />
<Compile Include="Core\Utils\NativeMethods.cs" />
<Compile Include="Core\Utils\UpdateInfo.cs" /> <Compile Include="Core\Utils\UpdateInfo.cs" />
<Compile Include="Migration\FormMigrationQuestion.cs"> <Compile Include="Migration\FormMigrationQuestion.cs">
<SubType>Form</SubType> <SubType>Form</SubType>