⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 offscreen.cpp

📁 一个关屏并锁按键程序(首创),其它人写的都是关屏程序,而没有锁按键程序
💻 CPP
字号:
// OffScreen.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "OffScreen.h"
#include <commctrl.h>
#include <aygshell.h>
#include <sipapi.h>
#include "gx.h"
#define QUERYESCSUPPORT    8

// The following are unique to CE
#define GETVFRAMEPHYSICAL   6144
#define GETVFRAMELEN    6145
#define DBGDRIVERSTAT    6146
#define SETPOWERMANAGEMENT   6147
#define GETPOWERMANAGEMENT   6148


typedef enum _VIDEO_POWER_STATE {
    VideoPowerOn = 1,
    VideoPowerStandBy,
    VideoPowerSuspend,
    VideoPowerOff
} VIDEO_POWER_STATE, *PVIDEO_POWER_STATE;


typedef struct _VIDEO_POWER_MANAGEMENT {
    ULONG Length;
    ULONG DPMSVersion;
    ULONG PowerState;
} VIDEO_POWER_MANAGEMENT, *PVIDEO_POWER_MANAGEMENT;

#define MAX_LOADSTRING 100
#define WM_NOTIFYICON	WM_USER+0x300
// Global Variables:
HINSTANCE			g_hInst;				// The current instance
HWND				g_hwndCB;					// The command bar handle
HWND				g_hbt;
HWND				g_hbh;
GXKeyList			gx_keylist;
TCHAR				keyname[16]={0};
DWORD				key=VK_RETURN;
BOOL				isKey=FALSE;
BOOL				isAuto=FALSE;
TCHAR				bClass[]=L"button";
TCHAR				bName[]=L"设置开屏按键";
NOTIFYICONDATA		nid;
static SHACTIVATEINFO s_sai;

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE, LPTSTR);
BOOL				InitInstance	(HINSTANCE, int);
BOOL				OffScreen(BOOL OnOff);

LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);
HWND				CreateRpCommandBar(HWND);

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	if(*lpCmdLine==L'A')
		isAuto=TRUE;
	MSG msg;
	HACCEL hAccelTable;
	g_hInst=hInstance;
	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_OFFSCREEN);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    It is important to call this function so that the application 
//    will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS	wc;

    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OFFSCREEN));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	return RegisterClass(&wc);
}

//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HKEY hKey;
	DWORD dwDisp;
	RegCreateKeyEx(HKEY_CURRENT_USER,L"Software\\OffScreen",NULL,NULL,NULL,NULL,NULL,&hKey,&dwDisp);
	if(dwDisp==REG_CREATED_NEW_KEY)
		RegCreateKeyEx(HKEY_CURRENT_USER,L"Software\\OffScreen",NULL,NULL,NULL,NULL,NULL,&hKey,&dwDisp);
	else
	{
		DWORD dwValueLen=sizeof DWORD;
		DWORD nType=REG_DWORD;
		RegQueryValueEx(hKey,L"key",NULL,&nType,(LPBYTE)&key,&dwValueLen);
		RegCloseKey(hKey);
	}
	swprintf(keyname,L"开屏键码:0x%x",key);
	HWND	hWnd = NULL;
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name
	// Initialize global strings
	LoadString(hInstance, IDC_OFFSCREEN, szWindowClass, MAX_LOADSTRING);
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

	//If it is already running, then focus on the window
	hWnd = FindWindow(szWindowClass, szTitle);	
	if (hWnd) 
	{
		// set focus to foremost child window
		// The "| 0x01" is used to bring any owned windows to the foreground and
		// activate them.
		SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
		return 0;
	} 

	MyRegisterClass(hInstance, szWindowClass);
	
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{	
		return FALSE;
	}
	g_hbt=CreateWindow(bClass,bName,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,0,176,90,20,hWnd,(HMENU)IDC_KEY,hInstance,NULL);
	g_hbh=CreateWindow(bClass,L"开机启动",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,0,220,90,20,hWnd,(HMENU)IDC_AUTO_START,hInstance,NULL);
	nid.cbSize=sizeof nid;
	nid.hWnd=hWnd;
	nid.uID=372274;
	nid.uFlags=NIF_ICON;
	nid.uCallbackMessage=WM_NOTIFYICON;
	nid.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_OFFSCREEN));
	Shell_NotifyIcon(NIM_ADD,&nid);
	//When the main window is created using CW_USEDEFAULT the height of the menubar (if one
	// is created is not taken into account). So we resize the window after creating it
	// if a menubar is present
	if (g_hwndCB)
    {
		RECT rc;
        RECT rcMenuBar;
		GetWindowRect(hWnd, &rc);
        GetWindowRect(g_hwndCB, &rcMenuBar);
		rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);	
		MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
	}
	HRSRC hc=FindResource(hInstance,MAKEINTRESOURCE(IDR_LOCK),L"LOCK");
	HGLOBAL hLock =LoadResource(hInstance,hc);
	HANDLE hF;
	TCHAR lock[]=L"\\Windows\\Lock.exe";
	hF=CreateFile(lock,
					GENERIC_WRITE,           // Open for reading
					FILE_SHARE_WRITE,        // Share for reading
					NULL,                   // No security
					CREATE_NEW,          // Existing file only
					FILE_ATTRIBUTE_NORMAL,  // Normal file
					NULL);
	if((DWORD)hF!=-1)
	{
		DWORD dwBytesWritten;
		WriteFile(hF,hLock,3584,&dwBytesWritten,NULL);
		CloseHandle(hF);
	}
	CreateProcess(lock,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
//	GXOpenDisplay( hWnd, GX_FULLSCREEN);
//	GXOpenInput();
	if(isAuto==FALSE)
	{
		OffScreen(VideoPowerOff);
//		gx_keylist = GXGetDefaultKeys(GX_NORMALKEYS);
		SetTimer(hWnd,3,10000,NULL);
	}
	else
	{
		ShowWindow(hWnd,SW_HIDE);
		GXSuspend();
	}
//	DWORD out;
//	BOOL INNN=SystemParametersInfo(SPI_GETBATTERYIDLETIMEOUT,1,&out,NULL);
	UpdateWindow(hWnd);
	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	TCHAR szHello[MAX_LOADSTRING];

	switch (message) 
	{
		case WM_KILLFOCUS:
			if(IsWindowVisible(hWnd))
			{
				KillTimer(hWnd,3);
				SetTimer(hWnd,1,10000,NULL);
			}
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			HANDLE as;
			switch (wmId)
			{	
				case IDOK:
					SendMessage (hWnd, WM_CLOSE, 0, 0);
					break;
				case IDCLOSE:
					SendMessage (hWnd, WM_CLOSE, 0, 0);
					break;
				case IDC_AUTO_START:
					as=CreateFile(_T("\\Windows\\启动\\OffScreen.lnk"),
						GENERIC_WRITE,           // Open for reading
						FILE_SHARE_WRITE,        // Share for reading
						NULL,                   // No security
						CREATE_ALWAYS,          // Existing file only
						FILE_ATTRIBUTE_NORMAL,  // Normal file
						NULL);
					if((DWORD)as==-1)
					{
						as=CreateFile(_T("\\Windows\\Startup\\OffScreen.lnk"),
							GENERIC_WRITE,           // Open for reading
							FILE_SHARE_WRITE,        // Share for reading
							NULL,                   // No security
							CREATE_ALWAYS,          // Existing file only
							FILE_ATTRIBUTE_NORMAL,  // Normal file
							NULL);
					}
					TCHAR mfn[MAX_PATH],mfn1[MAX_PATH];
					::GetModuleFileName(g_hInst,mfn,256);
					char lnk[MAX_PATH];
					swprintf(mfn1,_T("%d#\"%s\" A"),wcslen(mfn)+7,mfn);
					::WideCharToMultiByte(CP_ACP, 0,mfn1, -1,lnk,MAX_PATH, NULL, NULL);
					DWORD nW;
					WriteFile(as,lnk,strlen(lnk),&nW,NULL);
					CloseHandle(as);
					SetFocus(hWnd);
					break;
				case IDC_KEY:
					if(isKey==FALSE)
					{
						isKey=TRUE;
						KillTimer(hWnd,3);
						SetWindowText(g_hbt,L"确认设置按键");
						SetFocus(hWnd);
//						GXSuspend();
					}
					else
					{
						isKey=FALSE;
						SetTimer(hWnd,3,10000,NULL);
						SetWindowText(g_hbt,bName);
						SetFocus(hWnd);
//						GXResume();
					}
					break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
			g_hwndCB = CreateRpCommandBar(hWnd);
            // Initialize the shell activate info structure
            memset (&s_sai, 0, sizeof (s_sai));
            s_sai.cbSize = sizeof (s_sai);
			
			break;
		case WM_PAINT:
			RECT rt;
			hdc = BeginPaint(hWnd, &ps);
			GetClientRect(hWnd, &rt);
			LoadString(g_hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
			DrawText(hdc, szHello, _tcslen(szHello), &rt, 
				  DT_CENTER);
			rt.top=180;
			rt.left=100;
			DrawText(hdc, keyname, _tcslen(keyname), &rt, 
				  DT_LEFT|DT_SINGLELINE);
			EndPaint(hWnd, &ps);
			break; 
		case WM_DESTROY:
			GXCloseInput();
			GXCloseDisplay();		
			CommandBar_Destroy(g_hwndCB);
			PostQuitMessage(0);
			Shell_NotifyIcon(NIM_DELETE,&nid);
			break;
		case WM_ACTIVATE:
            // Notify shell of our activate message
			SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
     		break;
		case WM_SETTINGCHANGE:
//			SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
     		break;
		case WM_TIMER:			
			if(wParam==3)
			{
				SystemIdleTimerReset();
				OffScreen(VideoPowerOff);
			}
			else if(wParam==1)
			{
				SetForegroundWindow(hWnd);
				SetTimer(hWnd,3,10000,NULL);
			}
			break;
		case WM_KEYDOWN:
			if(wParam == key&&isKey==FALSE)
			{
				Sleep(1000);
				if((::GetAsyncKeyState(wParam)!=0&&::GetAsyncKeyState(wParam)!=1))
				{
					if(isAuto==TRUE)
					{
						GXSuspend();
						ShowWindow(hWnd,SW_HIDE);
						KillTimer(hWnd,3);
					}
					else
						DestroyWindow(hWnd);
					OffScreen(VideoPowerOn);
				}
			}
			else if(isKey==TRUE&&wParam>30)
			{
				key=wParam;
				RECT rt;
				swprintf(keyname,L"开屏键码:0x%x",wParam);
				HDC hdc=GetWindowDC(hWnd);
				GetClientRect(hWnd, &rt);
				rt.top=180;
				rt.left=100;
				DrawText(hdc, keyname, _tcslen(keyname), &rt, 
					  DT_LEFT|DT_SINGLELINE);
				ReleaseDC(hWnd,hdc);
				HKEY hKey;
				RegOpenKeyEx(HKEY_CURRENT_USER,L"Software\\OffScreen",NULL,NULL,&hKey);
				DWORD dwValueLen=sizeof DWORD;
				RegSetValueEx(hKey,L"key",NULL,REG_DWORD,(LPBYTE)&key,dwValueLen);
				RegCloseKey(hKey);
			}
			break;
		case WM_NOTIFYICON:
			if(wParam==372274&&(lParam==WM_LBUTTONUP||lParam==WM_RBUTTONUP))
			{
				GXResume();
				ShowWindow(hWnd,SW_SHOW);
				SetForegroundWindow(hWnd);
				isAuto=TRUE;
				KillTimer(hWnd,3);
				SetTimer(hWnd,3,10000,NULL);
				OffScreen(VideoPowerOff);
			}
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

HWND CreateRpCommandBar(HWND hwnd)
{
	SHMENUBARINFO mbi;

	memset(&mbi, 0, sizeof(SHMENUBARINFO));
	mbi.cbSize     = sizeof(SHMENUBARINFO);
	mbi.hwndParent = hwnd;
	mbi.nToolBarId = IDM_MENU;
	mbi.hInstRes   = g_hInst;
	mbi.nBmpId     = 0;
	mbi.cBmpImages = 0;

	if (!SHCreateMenuBar(&mbi)) 
		return NULL;

	return mbi.hwndMB;
}
BOOL OffScreen(BOOL OnOff)
{
	int iESC=SETPOWERMANAGEMENT;
	HDC gdc;
	VIDEO_POWER_MANAGEMENT vpm;
	gdc = ::GetDC(NULL);
	if(ExtEscape(gdc, QUERYESCSUPPORT, sizeof(int), (LPCSTR)&iESC, 0, NULL)==0)
		return FALSE;
	else
	{		
		vpm.Length = sizeof(VIDEO_POWER_MANAGEMENT);
		vpm.DPMSVersion = 0x0001;
		vpm.PowerState = OnOff;
		ExtEscape(gdc, SETPOWERMANAGEMENT, vpm.Length, (LPCSTR)&vpm, 0, NULL);
	}
	::ReleaseDC(NULL, gdc);
	return TRUE;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -