diff --git a/src/KpxConfig.h b/src/KpxConfig.h
index 45ff3ec..2de2c15 100644
--- a/src/KpxConfig.h
+++ b/src/KpxConfig.h
@@ -70,6 +70,7 @@ public:
 	QString lastKeyLocation(){return settings.value("Options/LastKeyLocation").toString();}
 	tKeyType lastKeyType(){return stringToKeyType(settings.value("Options/LastKeyType").toString());}
 	QByteArray mainWindowGeometry();
+	bool minimizeToTray(){return settings.value("Options/MinimizeToTray",false).toBool();} 	
 	bool minimizeTray(){return settings.value("Options/MinimizeTray",false).toBool();}
 	bool startMinimized(){return settings.value("Options/StartMinimized",false).toBool();}
 	bool startLocked(){return settings.value("Options/StartLocked",false).toBool();}
@@ -142,6 +143,7 @@ public:
 	void setLastKeyLocation(const QString& value){settings.setValue("Options/LastKeyLocation",value);}
 	void setLastKeyType(tKeyType value){settings.setValue("Options/LastKeyType",keyTypeToString(value));}
 	void setMainWindowGeometry(const QByteArray& value){settings.setValue("UI/MainWindowGeometry",value);}
+	void setMinimizeToTray(bool value){settings.setValue("Options/MinimizeToTray",value);}
 	void setMinimizeTray(bool value){settings.setValue("Options/MinimizeTray",value);}
 	void setStartMinimized(bool value){settings.setValue("Options/StartMinimized",value);}
 	void setStartLocked(bool value){settings.setValue("Options/StartLocked",value);}
diff --git a/src/dialogs/SettingsDlg.cpp b/src/dialogs/SettingsDlg.cpp
index f63284f..55334d7 100644
--- a/src/dialogs/SettingsDlg.cpp
+++ b/src/dialogs/SettingsDlg.cpp
@@ -34,6 +34,7 @@ CSettingsDlg::CSettingsDlg(QWidget* parent):QDialog(parent,Qt::Dialog)
 	connect(DialogButtons, SIGNAL( rejected() ), this, SLOT( OnCancel() ) );
 	connect(DialogButtons, SIGNAL( clicked(QAbstractButton*)), this, SLOT(OnOtherButton(QAbstractButton*)));
 	
+	connect(CheckBox_ShowSysTrayIcon, SIGNAL( toggled(bool) ), CheckBox_CloseToTray, SLOT( setEnabled(bool) ) );
 	connect(CheckBox_ShowSysTrayIcon, SIGNAL( toggled(bool) ), CheckBox_MinimizeTray, SLOT( setEnabled(bool) ) );
 	connect(CheckBox_OpenLast, SIGNAL( toggled(bool) ), CheckBox_RememberLastKey, SLOT( setEnabled(bool) ) );
 	connect(CheckBox_OpenLast, SIGNAL( toggled(bool) ), CheckBox_StartMinimized, SLOT( setEnabled(bool) ) );
@@ -84,6 +85,7 @@ CSettingsDlg::CSettingsDlg(QWidget* parent):QDialog(parent,Qt::Dialog)
 	CheckBox_OpenLast->setChecked(config->openLastFile());
 	CheckBox_RememberLastKey->setChecked(config->rememberLastKey());
 	CheckBox_ShowSysTrayIcon->setChecked(config->showSysTrayIcon());
+	CheckBox_CloseToTray->setChecked(config->minimizeToTray());
 	CheckBox_MinimizeTray->setChecked(config->minimizeTray());
 	CheckBox_StartMinimized->setChecked(config->startMinimized());
 	CheckBox_StartLocked->setChecked(config->startLocked());
@@ -237,6 +239,7 @@ void CSettingsDlg::apply(){
 	
 	//General (1)
 	config->setShowSysTrayIcon(CheckBox_ShowSysTrayIcon->isChecked());
+	config->setMinimizeToTray(CheckBox_CloseToTray->isChecked());
 	config->setMinimizeTray(CheckBox_MinimizeTray->isChecked());
 	config->setStartMinimized(CheckBox_StartMinimized->isChecked());
 	config->setStartLocked(CheckBox_StartLocked->isChecked());
diff --git a/src/forms/SettingsDlg.ui b/src/forms/SettingsDlg.ui
index 210b252..81820a9 100644
--- a/src/forms/SettingsDlg.ui
+++ b/src/forms/SettingsDlg.ui
@@ -146,6 +146,36 @@ QListView::item {
            
           
          
+         - 
+          
+           
- 
+            
+             
+              Qt::Horizontal
+             
+             
+              QSizePolicy::Fixed
+             
+             
+              
+               25
+               10
+              
+             
+            
+           
 
+           - 
+            
+             
+              false
+             
+             
+              Minimize to tray when clicking the main window's close button
+             
+            
+           
 
+          
+          
          - 
           
            
- 
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 7ae85e3..556ee3a 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -50,6 +50,7 @@ Export_Txt export_Txt;
 Export_KeePassX_Xml export_KeePassX_Xml;
 
 KeepassMainWindow::KeepassMainWindow(const QString& ArgFile,bool ArgMin,bool ArgLock,QWidget *parent, Qt::WFlags flags) :QMainWindow(parent,flags){
+	ShutingDown=false;
 	IsLocked=false;
 	EventOccurred=true;
 	inactivityCounter=0;
@@ -978,6 +979,7 @@ void KeepassMainWindow::OnFileChangeKey(){
 }
 
 void KeepassMainWindow::OnFileExit(){
+	ShutingDown = true;
 	close();
 }
 
@@ -1070,7 +1072,16 @@ void KeepassMainWindow::OnFileModified(){
 }
 
 void KeepassMainWindow::closeEvent(QCloseEvent* e){
+	if (!ShutingDown && config->showSysTrayIcon() && config->minimizeToTray()){
+		e->ignore();
+		if (config->lockOnMinimize() && !IsLocked && FileOpen)
+			OnUnLockWorkspace();
+		hide();
+		return;
+	}
+	
 	if(FileOpen && !closeDatabase()){
+		ShutingDown = false;
 		e->ignore();
 		if (!isVisible())
 			show();
@@ -1406,6 +1417,8 @@ void KeepassMainWindow::OnInactivityTimer(){
 }
 
 void KeepassMainWindow::OnShutdown(QSessionManager& manager) {
+	ShutingDown = true;
+	
 	/* QApplication::commitData() only closes visible windows,
 	   so we need to manually close mainwindow if it's hidden */
 	if (manager.allowsInteraction() && !isVisible()) {
diff --git a/src/mainwindow.h b/src/mainwindow.h
index 1b41dd5..a9b8d67 100644
--- a/src/mainwindow.h
+++ b/src/mainwindow.h
@@ -136,6 +136,7 @@ class KeepassMainWindow : public QMainWindow, private Ui_MainWindow{
 		QWidget* NormalCentralWidget;
 		QWidget* LockedCentralWidget;
 		Ui_WorkspaceLockedWidget WorkspaceLockedWidget;
+		bool ShutingDown;
 		bool InUnLock;
 		QList lockGroup;
 		QDialog* unlockDlg;