Windows API Programming-----Disable task switching in Windows NT environments

Source: Internet
Author: User

Function prototypes:BOOL WINAPI systemparametersinfo (  _in_    uint  uiaction,  _in_    uint  Uiparam,  _ Inout_ PVOID pvparam,  _in_    UINT  fwinini); Function MSDN Link: https://msdn.microsoft.com/en-us/library/ms724947 (v=vs.85). aspx
usage: UINT npreviousstate; // Disable task switching SystemParametersInfo (Spi_setscreensaverrunning,true,&npreviousstate,0)// Allow task switching SystemParametersInfo (Spi_setscreensaverrunning,false,&npreviousstate,0);

code example:

//DisableChangeTask.cpp: Defines the entry point for the application. //#include"stdafx.h"#include"DisableChangeTask.h"#defineMax_loadstring 100typedefstruct_dlong{LONG WParam; LONG LParam;} Dlong;//Global Variables:HINSTANCE HInst;//Current InstanceWCHAR sztitle[max_loadstring];//title bar TextWCHAR szwindowclass[max_loadstring];//main window class name//The forward declaration of the function contained in this code module:ATOM MyRegisterClass (hinstance hinstance); BOOL InitInstance (HINSTANCE,int); LRESULT CALLBACK WndProc (hwnd, UINT, WPARAM, LPARAM); Int_ptr CALLBACK About (hwnd, uint, WPARAM, LPARAM);//MyFunction, that is, using this function to process messagesBOOL CALLBACK Enumwindowsproc (HWND hwnd, LPARAM LPARAM);intapientry wWinMain (_in_ hinstance hinstance, _in_opt_ hinstance hprevinstance, _in_ lpwstr lpCmdLine, _in_intncmdshow)    {Unreferenced_parameter (hprevinstance);    Unreferenced_parameter (lpCmdLine); if(lpCmdLine! =NULL) {        if(Lstrlen (lpCmdLine) >=1) {MessageBox (NULL, lpCmdLine, TEXT ("The cmdline"), MB_OK); }    }    //TODO: Place the code here. //Initializing global StringsLoadstringw (hinstance, Ids_app_title, SzTitle, max_loadstring);    Loadstringw (HInstance, Idc_disablechangetask, Szwindowclass, max_loadstring);    MyRegisterClass (HINSTANCE); //To perform an application initialization:    if(!InitInstance (HINSTANCE, nCmdShow)) {        returnFALSE; } haccel hacceltable=loadaccelerators (hinstance, Makeintresource (Idc_disablechangetask));    MSG msg; //main message loop:     while(GetMessage (&msg, nullptr,0,0))    {        if(! TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {TranslateMessage (&msg); DispatchMessage (&msg); }    }    return(int) Msg.wparam;}////function: MyRegisterClass ()////Purpose: Registers the window class. //ATOM MyRegisterClass (hinstance hinstance) {WNDCLASSEXW Wcex; Wcex.cbsize=sizeof(Wndclassex); Wcex.style= Cs_hredraw |Cs_vredraw; Wcex.lpfnwndproc=WndProc; Wcex.cbclsextra=0; Wcex.cbwndextra=0; Wcex.hinstance=hinstance; Wcex.hicon=LoadIcon (hinstance, Makeintresource (Idi_disablechangetask)); Wcex.hcursor=loadcursor (nullptr, Idc_arrow); Wcex.hbrbackground= (Hbrush) (color_window+1); Wcex.lpszmenuname=Makeintresourcew (Idc_disablechangetask); Wcex.lpszclassname=Szwindowclass; WCEX.HICONSM=LoadIcon (Wcex.hinstance, Makeintresource (Idi_small)); returnREGISTERCLASSEXW (&Wcex);}////function: InitInstance (hinstance, int)////Purpose: Save the instance handle and create the main window////notes:////in this function, we save the instance handle in the global variable and//Create and display the main program window. //BOOL InitInstance (HInstance hinstance,intncmdshow) {HInst= HInstance;//storing an instance handle in a global variableHWND hwnd=Createwindoww (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault,0, Cw_usedefault,0, nullptr, nullptr, hinstance, nullptr); if(!hWnd) {      returnFALSE; }/********************************my code goes here***************************/BOOL old;   Dlong Dlong; Dlong.wparam=(WPARAM) hWnd; Dlong.lparam=(LPARAM) FALSE; EnumWindows (Enumwindowsproc, (LONG)&Dlong); //using the screen saver to trick the operating system against Alt+tab/escSystemParametersInfo (spi_screensaverrunning, TRUE, &old,0);/********************************my code goes up there***********************/ShowWindow (hWnd, ncmdshow);   UpdateWindow (HWND); returnTRUE;}////functions: WndProc (HWND, UINT, WPARAM, LPARAM)////Purpose: Processes the message of the main window. ////wm_command-Processing Application Menu//WM_PAINT-Draw the main window//Wm_destroy-Sends an exit message and returns////LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {Switch(message) {/*****************my code goes below***************/     CaseWm_syscommand:if(WParam = = sc_minimize)return 0; returnDefWindowProc (hWnd, message, WParam, LParam);  Break; /*****************my code goes above***************/     Casewm_command: {intWmid =LoWord (WParam); //Analysis Menu Selection:            Switch(wmid) { CaseIdm_about:dialogbox (HInst, Makeintresource (Idd_aboutbox), hWnd, about);  Break;  CaseIdm_exit:destroywindow (HWND);  Break; default:                returnDefWindowProc (hWnd, message, WParam, LParam); }        }         Break;  Casewm_paint: {paintstruct ps; HDC HDC= BeginPaint (HWnd, &PS); //TODO: Add any drawing code here that uses HDC ...EndPaint (HWnd, &PS); }         Break;  CaseWm_destroy:/******* allows the task to switch ******** when exiting the current window*/Dlong Dlong;        BOOL old; Dlong.lparam=(LPARAM) TRUE; Dlong.wparam=(WPARAM) hWnd; EnumWindows (Enumwindowsproc, (LONG)&Old ); SystemParametersInfo (spi_screensaverrunning, FALSE,&old,0); /******* allows the task to switch ******** when exiting the current window*/PostQuitMessage (0);  Break; default:        returnDefWindowProc (hWnd, message, WParam, LParam); }    return 0;}//The message handler for the About box. int_ptr CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM); Switch(message) { CaseWm_initdialog:return(INT_PTR) TRUE;  CaseWm_command:if(LoWord (wParam) = = IDOK | | LoWord (wParam) = =IDCANCEL)            {EndDialog (hdlg, LoWord (WParam)); return(INT_PTR) TRUE; }         Break; }    return(INT_PTR) FALSE;} BOOL CALLBACK Enumwindowsproc (HWND hwnd, LPARAM LPARAM) {Dlong*pdlong = (dlong*) LParam; BOOL benable= (BOOL) pdlong->LParam; if(hwnd! = (HWND) pdlong->WParam)    EnableWindow (hwnd, benable); returnTRUE;}

Run Result: If you do not close the running interface, you cannot switch to another task:

Windows API Programming-----Disable task switching in Windows NT environments

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.