在程式中对键盘、鼠标、显示器、桌面 操作小结

一 键盘

1.1 Disabling Low-level Keys

#define _WIN32_WINNT 0x0400
#include
LRESULT CALLBACK LowLevelKeyboardProc(int nCode,
           WPARAM wParam, LPARAM lParam)
{
 BOOL fEatKeystroke = FALSE;
 
 if (nCode == HC_ACTION) {
  switch (wParam) {
  case WM_KEYDOWN: 
  case WM_SYSKEYDOWN:
  case WM_KEYUP:   
  case WM_SYSKEYUP:
   PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;
   fEatKeystroke =
    ((p->vkCode == VK_TAB)    &&  ((p->flags & LLKHF_ALTDOWN) != 0)) ||
    ((p->vkCode == VK_ESCAPE) &&  ((p->flags & LLKHF_ALTDOWN) != 0)) ||
    ((p->vkCode == VK_ESCAPE) &&  ((GetKeyState(VK_CONTROL)&0x8000) != 0))||
    ( p->vkCode == VK_LWIN)   ||  ( p->vkCode==VK_RWIN )
    ;
   break;
  }
 }
 return(fEatKeystroke ? 1 : CallNextHookEx(NULL, nCode, wParam,
  lParam));
}


int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int)
{
       
        // Install the low-level keyboard & mouse hooks
        HHOOK hhkLowLevelKybd  = SetWindowsHookEx(WH_KEYBOARD_LL,
        LowLevelKeyboardProc, hinstExe, 0);
 
       // Keep this app running until we're told to stop
         MessageBox(NULL,
             TEXT("Alt+Esc, Ctrl+Esc, Alt+Tab, LWIN and RWIN are now disabled./n") 
          TEXT("Click /"Ok/" to terminate this application and re-enable these keys."),
                TEXT("Disable Low-Level Keys"),
                MB_OK);
  
             UnhookWindowsHookEx(hhkLowLevelKybd);
             return(0);
}


1.2 系统热键
        1.屏蔽:SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,true,0, SPIF_UPDATEINIFILE);
        2.恢复:SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,false,  0, SPIF_UPDATEINIFILE);

1.3 模拟按键
 
让系统模拟一次按下WIN+D键  
 
               keybd_event(VK_LWIN,0,0,0);  
               keybd_event('D',0,0,0);  
               keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0);  
               keybd_event('D',0,KEYEVENTF_KEYUP,0);

1.4 切换到当前桌面

模拟最小化屏幕快捷键Window+D  
 
                             keybd_event(VK_LWIN,0,0,0);      
                             keybd_event('D',0,0,0);      
                             keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0);      
                             keybd_event('D',0,KEYEVENTF_KEYUP,0);  
---------------------------------------------------------------  
 
正统的办法如下:  
 
#include    
#include    
 
int  main  ()  
       {  
               CoInitialize  (0);  
               IShellDispatch4  *pdisp  =  NULL;  
               CoCreateInstance  (CLSID_Shell,  NULL,  CLSCTX_ALL,  __uuidof  (IShellDispatch4),  (void  **)&pdisp);  
               pdisp->ToggleDesktop  ();  //  这句是用来切换桌面的  
               pdisp->Release  ();  
               CoUninitialize  ();  
       };

1.5 调出关机选择画面

////////调出关机选择画面:(重启、关机、休眠、待机)  
typedef  int  (CALLBACK  *SHUTDOWNDLG)(int);  //显示关机对话框函数的指针  
HINSTANCE  hInst  =  LoadLibrary("shell32.dll");  //装入shell32.dll  
SHUTDOWNDLG  ShutDownDialog;  //指向shell32.dll库中显示关机对话框函数的指针  
if(hInst  !=  NULL)  
{  
       //获得函数的地址并调用之  
       ShutDownDialog  =  (SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60);  
       (*ShutDownDialog)(0);  
}  

二 鼠标

2.1   ::ClipCursor(&rect);
        ::ClipCursor(NULL);

2.2 一个未公开的API函数BlockInput,在user32.dll中,用它可以
轻易的锁定键盘和鼠标。

BOOL __stdcall (*BlockInput)(BOOL Flag);
(FARPROC)BlockInput = GetProcAddress(GetModuleHandle(
    "user32.dll"), "BlockInput");
if (BlockInput) BlockInput(TRUE); //这样就可以了

三 显示器

   3.1 关闭显示器
        1. SendMessage( WM_SYSCOMMAND, SC_MONITORPOWER, 2);//关显示器
           SendMessage( WM_SYSCOMMAND, SC_SCREENSAVE, 0);//运行屏保

        2. ::SendMessage(GetSafeHwnd(), WM_SYSCOMMAND, SC_MONITORPOWER, 2);
 
        3. SendMessage(HWND_BROADCAST,WM_SYSCOMMAND,SC_MONITORPOWER,2);
   3.2 启动屏保
         PostMessage(WM_SYSCOMMAND,SC_SCREENSAVE,0);

四 系统桌面

  4.1 隐藏任务栏
       隐藏 ::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_HIDE);
            or ::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_HIDE);
       显示 ::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_SHOW);
            or ::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_SHOW);

   4.2 在任务栏隐藏
       SetWindowLong(Application,Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);

   4.3在任务管理器中隐藏
//定义RegisterServiceProcess函数的原型
typedef DWORD (CALLBACK* LPREGISTERSERVICEPROCESS)(DWORD,DWORD);

//注册应用程序的进程
//其中i = 0,将进程注册成非服务进程,这样,在Windows的关闭程序窗口中就不会出现应用程序
//的名称了。
// i = 1,将进程注册成服务进程,这是一般应用程序运行时所采用的状态。

void RegisterProcess(int i)
{
            HINSTANCE hDLL;
            LPREGISTERSERVICEPROCESS lpRegisterServiceProcess;

            //加载RegisterServiceProcess函数所在的链接库
            hDLL = LoadLibrary("KERNEL32");

            //得到RegisterServiceProcess函数的地址 
            lpRegisterServiceProcess = (LPREGISTERSERVICEPROCESS)GetProcAddress(hDLL, "RegisterServiceProcess");

            //执行RegisterServiceProcess函数
            lpRegisterServiceProcess(GetCurrentProcessId(),i);

           //卸载链接库
           FreeLibrary(hDLL);
}

 

4.4启动屏保

SystemParametersInfo(SPI_SETDESKWALLPAPER,0,"C://*.BMP",WM_WININICHANGE);

你可能感兴趣的:(shell,null,application,callback,keyboard,winapi)