Click here to Skip to main content
13,196,126 members (53,623 online)
Click here to Skip to main content

Stats

31.2K views
1.5K downloads
2 bookmarked
Posted 14 Jun 2012

MinLock, a KeePass 2.x Plugin to keep minimized KeePass Locked

, 14 Jun 2012
MinLock a KeePass 2.x plugin to keep it locked when minimized.
using System;
using System.Windows.Forms;

using KeePass.Plugins;

namespace MinLock
{
  public sealed class MinLockExt : Plugin
  {
    IPluginHost m_Host;
    Timer m_Timer;

    public override bool Initialize(IPluginHost host)
    {
      m_Host = host;

      // Upon unlocking, the database is opened and this event fires;
      // it likely fires other times too (e.g. opening db from menu).
      m_Host.MainWindow.FileOpened += MainWindow_FileOpened;

      return base.Initialize(host);
    }

    public override void Terminate()
    {
      KillTimer();
      base.Terminate();
    }

    void KillTimer()
    {
      if (m_Timer != null)
      {
        m_Timer.Dispose();
        m_Timer = null;
      }
    }

    void MainWindow_FileOpened(object sender, KeePass.Forms.FileOpenedEventArgs e)
    {
      if (m_Timer == null && m_Host.MainWindow.WindowState == FormWindowState.Minimized)
      {
        // Start a Windows.Forms.Timer, because it's based on the event loop it
        // can't interrupt current calls, though calls to Application.DoEvents
        // could wreak havoc, but that doesn't appear to be a problem here.
        m_Timer = new Timer();
        m_Timer.Interval = 1;
        m_Timer.Tick += Timer_Tick;
        m_Timer.Start();
      }
    }

    void Timer_Tick(object sender, EventArgs e)
    {
      KillTimer();

      if (m_Host.MainWindow.WindowState == FormWindowState.Minimized &&
          m_Host.MainWindow.IsAtLeastOneFileOpen())
      {
        m_Host.MainWindow.LockAllDocuments();
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

TLHobbes
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171019.1 | Last Updated 14 Jun 2012
Article Copyright 2012 by TLHobbes
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid