Skip to content

Commit

Permalink
auto startup
Browse files Browse the repository at this point in the history
  • Loading branch information
mgkha committed Jan 10, 2021
1 parent f95b99f commit abc4a91
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
29 changes: 20 additions & 9 deletions SmartHomeWin/Home.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 47 additions & 2 deletions SmartHomeWin/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace SmartHomeWin
public partial class Home : Form
{
public static string hotkeyPath = Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile) + @"\tuya\hotkeys.json";
public static string startupPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Startup) + @"\SmartHome.lnk";

[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(Keys vKey);
Expand All @@ -23,7 +24,7 @@ public partial class Home : Form
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

private List<HotKeyBinding> hotkeyBinding;
private readonly List<HotKeyBinding> hotkeyBinding;

private Hotkey hotkey;
private bool listening = false;
Expand All @@ -35,6 +36,7 @@ public Home()
Program.tuya = new TuyaApi.TuyaApi();

menuLogin.Text = Program.tuya.IsAuthenticated() ? "Logout" : "Login";
menuAutoStart.Checked = File.Exists(startupPath);

try
{
Expand Down Expand Up @@ -129,7 +131,7 @@ private async void RefreshDevicesList(bool hardRefresh = false)
}
}

if(hotkeyBinding.Count > 0)
if (hotkeyBinding.Count > 0)
{
btnUnbindKeys.Enabled = true;
}
Expand Down Expand Up @@ -304,6 +306,49 @@ private void BtnUnbindKeys_Click(object sender, EventArgs e)
UnregisterAllHotkeys();
}
}

private bool minimize = true;

private void Home_FormClosing(object sender, FormClosingEventArgs e)
{
if (minimize)
{
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
}

private void MenuExit_Click(object sender, EventArgs e)
{
minimize = false;
Close();
}

private void CreateShortcut(string shortcutPath)
{
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();

IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);
string appExePath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".exe");
shortcut.TargetPath = appExePath;
shortcut.Save();

}

private void MenuAutoStart_Click(object sender, EventArgs e)
{
if (menuAutoStart.Checked == false)
{
CreateShortcut(startupPath);
menuAutoStart.Checked = true;
}
else
{
File.Delete(startupPath);
menuAutoStart.Checked = false;
}
}

}

public class HotKeyBinding
Expand Down
11 changes: 11 additions & 0 deletions SmartHomeWin/SmartHomeWin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
<ApplicationIcon>cloud-storage.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
<COMReference Include="IWshRuntimeLibrary.dll">
<Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<WrapperTool>tlbimp</WrapperTool>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
Expand Down

0 comments on commit abc4a91

Please sign in to comment.