Using system;
Using system. runtime. interopservices;
Using system. Threading;
Namespace lockwindows
{
/// <Summary>
/// Summary of nativewin32.
/// </Summary>
Public class nativewin32
{
Public nativewin32 ()
{
//
// Todo: add the constructor logic here
//
}
[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern intptr find1_wex (intptr parent/* hwnd */,
Intptr next/* hwnd */,
Intptr sclassname,
String swindowtitle );
[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern void setforegroundwindow (INT hwnd );
[Dllimport ("user32.dll")]
Public static extern bool showwindow (INT hwnd, ncmdshow );
[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern int sendmessage (intptr hwnd, int MSG, int wparam, int lparam );
[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern int createmutex (intptr hwnd, int MSG, int wparam, int lparam );
[Dllimport ("hk_keyboardctr.dll", charset = charset. Auto)]
Private Static extern void hidehotkey ();
[Dllimport ("hk_keyboardctr.dll", charset = charset. Auto)]
Private Static extern void showhotkey ();
Public static bool hidesyskey (bool Bhide)
{< br> try
{< br> If (Bhide)
{< br> hidehotkey ();
}< br> else
{< br> showhotkey ();
}< br> return true;
}< br> catch
{< br> return false;
}< BR >}< br> // sendmessage (handle2, wm_gettext, 1024, INTEGER (@ BUF);
///
// find a window
///
/// window title
// the returned result indicates that the window is found, false:
Public static bool findthiswindow (string windowtittle)
{< br> intptr hparent = intptr. zero;
intptr hnext = intptr. zero;
hnext = nativewin32.find1_wex (hparent, hnext, intptr. zero, windowtittle);
If (hnext. toint32 ()> 0)
{< br> nativewin32.setforegroundwindow (hnext. toint32 ();
return true;
}< br> else
{< br> return false;
}< BR >}< br> Public static bool isexists (string strmutex)
{
bool creatednew;
system. Threading. mutex = new system. Threading. mutex (true, strmutex, out creatednew);
return! Creatednew; // If createnew is true, the mutex is created successfully without conflict, in this case, the Program is not started
}
Public static bool closethewindows (string strwindowname)
{< br> try
{< br> intptr hparent = intptr. zero;
intptr hnext = intptr. zero;
hnext = nativewin32.find1_wex (hparent, hnext, intptr. zero, strwindowname);
If (hnext. toint32 ()> 0)
{< br> // close the window
nativewin32.sendmessage (hnext, 16,0, 0);
return true;
}< br> else
{< br> return false;
}< BR >}< br> catch (exception ERR)
{< br> return false;
}< BR >}
Public Enum ncmdshow: uint
{
Sw_forceminimize = 0x0,
Sw_hide = 0x1,
Sw_maximize = 0x2,
Sw_minimize = 0x3,
Sw_restore = 0x4,
Sw_show = 0x5,
Sw_showdefault = 0x6,
Sw_showmaximized = 0x7,
Sw_showminimized = 0x8,
Sw_showminnoactive = 0x9,
Sw_showna = 0xa,
Sw_shownoactivate = 0xb,
Sw_shownormal = 0xc,
Wm_close = 0x10,
// Wm_gettext = 0x11,
}
}
}