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

📄 main.asm

📁 使用Win32汇编实现屏幕保护程序, 自动关闭显示器
💻 ASM
字号:
.386
.model	flat, stdcall
option	casemap: none

include		gdi32.inc
includelib	gdi32.lib
include		kernel32.inc
includelib	kernel32.lib
include		user32.inc
includelib	user32.lib
include		windows.inc
include		User_Defined.inc

INIT_ETA		equ		10
IDB_BITMAP1             equ        101

							.data?

hInstance			dd	?	
hMainWnd			dd	?	
hMainMenu			dd	?
idTimer				dd	?
nCounter			dd	?
hNotice				dd	?
hQuit				dd	?
hETA				dd	?
							.data
							
							.const

szWndClass			byte	'PowerSave', 0              ;//Class of the main window
szWndCaption		byte	'Power Save', 0				;//Caption of the main window		
szStaticClass		byte	'Static', 0
szNotice			byte	'Going to save mode:', 0
szQuit				byte	'ESC to quit', 0
szETA				byte	'10 Second', 0
szETAFmt			byte	'%d Second', 0
szMsgTimePast		byte	'1 second past', 0
szMsgShutDown		byte	'The monitor is going to shutdown...', 0
szCaption			byte	'Notice', 0			

			
							.code

;//Shut down the monitor with the cpu running.
ShutDownMonitor	proc
				;//TODO: Shut down the monitor here
				invoke	SendMessage, hMainWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1
				invoke	SendMessage, hMainWnd, WM_CLOSE, 0, 0
				ret
ShutDownMonitor	endp

;//Event handler: 1 second past.
Second_Past_Handler	proc
					local szETABuf[256]: byte
					
					dec	nCounter
					.if nCounter == 0
						invoke	KillTimer, NULL, idTimer		
						;//invoke	MessageBox, hMainWnd, offset szMsgShutDown, offset szCaption, MB_OK
						
						invoke	ShutDownMonitor
					.elseif
						;//invoke	MessageBox, hMainWnd, offset szMsgTimePast, offset szCaption, MB_OK
						invoke	wsprintf, addr szETABuf, offset szETAFmt, nCounter 
						invoke	SetWindowText, hETA, addr szETABuf
					.endif
					ret
Second_Past_Handler	endp

;//设置窗口为全屏显示
SetFullScreen	proc	hWnd
				local	stDesktop: RECT
				local	hDesktop: dword
				local	nWidth: dword
				local	nHeight: dword

				mov		nWidth, 0
				mov		nHeight, 0

				invoke	GetDesktopWindow
				mov		hDesktop, eax
				invoke	GetWindowRect, hDesktop, addr stDesktop
				mov		eax, stDesktop.right
				sub		eax, stDesktop.left
				mov		nWidth, eax
				mov		eax, stDesktop.bottom
				sub		eax, stDesktop.top
				mov		nHeight, eax
				invoke	SetWindowPos, hWnd, HWND_TOP, 0, 0, nWidth, nHeight, SWP_SHOWWINDOW
				ret
SetFullScreen	endp

;//Event handler: WM_CREATE
WM_CREATE_Handler	proc	uses ebx esi edi, hWnd
					mov		nCounter, INIT_ETA
					;//Initialize the timer
					invoke	SetTimer, NULL, NULL, 1000, offset Second_Past_Handler	
					mov		idTimer, eax
					invoke	SetFullScreen, hWnd		;//全屏显示

					invoke	CreateWindowEx, NULL, offset szStaticClass, offset szNotice,\
											WS_CHILD or WS_VISIBLE or SS_LEFT or WS_GROUP, 420, 384, 137, 18,\
												hWnd, 1, hInstance, NULL
					mov		hNotice, eax
					invoke	ShowWindow, hNotice, SW_SHOWNORMAL
					invoke	CreateWindowEx, NULL, offset szStaticClass, offset szQuit,\
											WS_CHILD or WS_VISIBLE or SS_CENTER or WS_GROUP, 420, 402, 206, 18,\
												hWnd, 2, hInstance, NULL
					mov		hQuit, eax
					invoke	ShowWindow, hQuit, SW_SHOWNORMAL
					invoke	CreateWindowEx, NULL, offset szStaticClass, offset szETA,\
											WS_CHILD or WS_VISIBLE or SS_RIGHT or WS_GROUP, 558, 384, 68, 18,\
												hWnd, 3, hInstance, NULL
					mov		hETA, eax
					invoke	ShowWindow, hETA, SW_SHOWNORMAL
					ret
WM_CREATE_Handler	endp

;//Event handler: WM_CLOSE
WM_CLOSE_Handler	proc	uses ebx edi edi
					invoke	DestroyWindow, hMainWnd
					invoke	PostQuitMessage, NULL
					ret
WM_CLOSE_Handler	endp

WM_CHAR_Handler	proc dwCharCode
				xor	eax, eax
				mov	eax, dwCharCode
				.if	eax == VK_ESCAPE
					invoke	KillTimer, NULL, idTimer
					mov		nCounter, INIT_ETA
					invoke	SendMessage, hMainWnd, WM_CLOSE, 0, 0
				.endif
				ret
WM_CHAR_Handler	endp

;//Window procedure: windows messages handling
ProcWinMain	proc	uses ebx esi edi, hWnd, uMsg, wParam, lParam
					mov	eax, uMsg
					.if	eax == WM_CREATE
						invoke	WM_CREATE_Handler, hWnd
					.elseif	eax == WM_CHAR
						invoke	WM_CHAR_Handler, wParam
					.elseif	eax == WM_CLOSE
						invoke	WM_CLOSE_Handler
					.else
						invoke	DefWindowProc, hWnd, uMsg, wParam, lParam
						ret                                              
					.endif                                                   
					xor	eax, eax                                         
					ret                                                      
ProcWinMain	endp

;//****************
;//* Main program *
;//****************
WinMain		proc	
			local	stWndClassEx: WNDCLASSEX	;//Window class structrue
			local	stMsg: MSG					;//Windows message structure
			local	hAccelerator				;//Handler to the loaded accelerators table
			
			;//**************************
			;//* Get application handle *
			;//**************************
			invoke	GetModuleHandle, NULL                                               
			mov		hInstance, eax
			
			;//******************
			;//* Load Main Menu *
			;//******************
			;//invoke	LoadMenu, hInstance, IDM_MAIN
			;//mov		hMainMenu, eax                               
																 
			;//**************************
			;//* Load accelerators table*
			;//**************************
			;//invoke	LoadAccelerators, hInstance, IDA_MAIN
			;//mov		hAccelerator, eax                           
			
			;//*************************
			;//* Register window class *
			;//*************************
			invoke	RtlZeroMemory, addr stWndClassEx, sizeof stWndClassEx
			
			;//*************
			;//* Load icon *	
			;//*************
			invoke	LoadIcon, hInstance, IDB_BITMAP1
			mov		stWndClassEx.hIcon, eax
			mov		stWndClassEx.hIconSm, eax

			;//***************
			;//* Load cursor *	
			;//***************
			invoke	LoadCursor, NULL, IDC_ARROW
			mov		stWndClassEx.hCursor, eax

			;//***************************
			;//* Load application handle *
			;//***************************
			push	hInstance
			pop		stWndClassEx.hInstance
			
			;//****************************
			;//* Initialize other members *
			;//****************************
			mov	stWndClassEx.cbSize, sizeof WNDCLASSEX	;//Specifies the size, in bytes, of this structure
			mov	stWndClassEx.style, CS_HREDRAW or CS_VREDRAW	;//Specifies the class style(s). 	==> CS_***
			mov	stWndClassEx.lpfnWndProc, offset ProcWinMain	;//Pointer to the window procedure	
			mov	stWndClassEx.hbrBackground, COLOR_BACKGROUND + 1
			mov	stWndClassEx.lpszClassName, offset szWndClass
			
			invoke	RegisterClassEx, addr stWndClassEx

			;//**************************
			;//* Create and Show window *
			;//**************************
			invoke	 CreateWindowEx, WS_EX_CLIENTEDGE,\
									offset szWndClass,\
									offset szWndCaption,\
									WS_POPUP,\
									CW_USEDEFAULT, CW_USEDEFAULT,\
									CW_USEDEFAULT, CW_USEDEFAULT,\
									NULL,\
									hMainMenu,\
									hInstance,\
									NULL
			mov		hMainWnd, eax
			
			invoke	ShowWindow, hMainWnd, SW_SHOWNORMAL
			invoke	UpdateWindow, hMainWnd

			;//****************
			;//* Message loop *
			;//****************
			.while	TRUE
					invoke	GetMessage, addr stMsg, NULL, 0, 0
					.break	.if eax == 0
					invoke	TranslateAccelerator, hMainWnd, hAccelerator, addr stMsg
					.if	eax == 0
						invoke	TranslateMessage, addr stMsg
						invoke	DispatchMessage, addr stMsg
					.endif
			.endw
			ret
WinMain		endp

;//***********************
;//* Program entry point *
;//***********************
start:	
		call	WinMain
		invoke	ExitProcess, NULL
end		start

⌨️ 快捷键说明

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