Continue to write the timed forced rest program.
The timer/countdown function uses a timer. When there are 30 seconds left for the forced rest, the countdown window is displayed in the lower right corner of the screen.
To display the countdown window in the lower-right corner of the screen, when calculating the window position, we can locate the window directly in the desktop workspace without considering the system taskbar height and other issues.
In practice, the display of the Countdown window from bottom to top is more attractive to computer users than the gradual display. In addition, it is better to add music and sound prompts. This is also the effect of the masm32 programming implementation window gradually getting in and out (Http://blog.csdn.net/Purpleendurer/archive/2008/09/18/2949159.aspx), Because the pre-defined dialog box resources are used in this article, this article creates a window only when the program is running.
Using labels or text boxes in the window to display the countdown information is simple to implement, but I did not do this, but used textout () for output, because, you can use the createfont () function to create a large font, set the font color with settextcolor (), and set the background color with setbkcolor () to make the information more eye-catching and display better.
Of course, for convenience, in the following demo, I used the default font of the system and did not add the beautification code.
(Source program and executable files:Http://download.csdn.net/source/643871)
; <<
; Function: display the countdown window from bottom to top in the lower right corner of the screen
; Author: purpleendurer
; Devenv: Win XP SP2 + masm32 V7
; Log
;----------------------------------------------------
; 2008-09-23 created!
; <<
. 386
. Model flat, stdcall
Option Casemap: None
Include/masm32/include/Windows. inc
Include/masm32/include/kernel32.inc
Includelib/masm32/lib/kernel32.lib
Include/masm32/include/user32.inc
Includelib/masm32/lib/user32.lib
Include/masm32/include/gdi32.inc
Includelib/masm32/lib/gdi32.lib
Winmain proto: DWORD,: DWORD
;. Const
Id_timer equ 1
C_timewinwidth: equ 240; countdown window width
C_timewinheight equ 60; countdown window height
. Data
G_sztimewinclsname DB "timewincls", 0
G_szappname DB "forced rest! ", 0
G_dwremaintime dd 34
G_szremaintime label DWORD
G_bd10 DB?
G_bd1 DB?
G_szrest DB "forced rest in seconds! ";, 0
C_remaintimelen equ $-g_szremaintime
. Data?
G_hhinstance hinstance?
G_stptst paintstruct <?>
G_hdc HDC?
G_hwndmain hwnd?
G_dwtimetexttop DWORD?
G_dwtimetextleft DWORD?
. Code
Start:
Invoke getmodulehandle, null
MoV g_hhinstance, eax
Invoke winmain, g_hhinstance, null, null, sw_showdefault
Invoke exitprocess, eax
Winmain proc hinst: hinstance, hprevinst: hinstance, using line: lpstr, cmdshow: DWORD
Local WC: wndclassex
Local MSG: msg
Local hwnd: hwnd
MoV WC. cbsize, sizeof wndclassex
MoV WC. style, cs_hredraw or cs_vredraw
MoV WC. lpfnwndproc, offset wndproc
MoV WC. cbclsextra, null
MoV WC. cbwndextra, null
Push g_hhinstance
Pop WC. hinstance
MoV WC. hbrbackground, 25; pale yellow window
MoV WC. lpszmenuname, null
MoV WC. lpszclassname, offset g_sztimewinclsname
Invoke loadicon, null, idi_application
MoV WC. hicon, eax
MoV WC. hiconsm, eax
Invoke loadcursor, null, idc_arrow
MoV WC. hcursor, eax
Invoke registerclassex, addr wc; ws_ex_topmost
--- Create a window
Invoke createappswex, null, ADDR g_sztimewinclsname, ADDR g_szappname/
, Ws_popup + ws_visible + ws_border, cw_usedefault, cw_usedefault/
, 0, 0, null, null/
, Hinst, null
MoV hwnd, eax
Invoke showwindow, hwnd, sw_hide; sw_shownormal
; Invoke updatewindow, hwnd
. While true
Invoke getmessage, addr msg, null, 0, 0
. Break. If (! Eax)
Invoke translatemessage, ADDR msg
Invoke dispatchmessage, ADDR msg
. Endw
MoV eax, MSG. wparam
RET
Winmain endp
;///////////////////////////////
Function: calculates and displays the output position of the Countdown information.
; Input: None
; Output: None
;//////////////////////////////
Gettimetextpos proc
Local sttm: textmetric
Invoke getdc, g_hwndmain
--- Retrieve the default font information of the window
MoV EBX, eax
Invoke gettextmetrics, EBX, ADDR sttm
--- Calculate the Y coordinate of the output position
MoV eax, sttm. tmheight
Add eax, c_timewinheight
SHR eax, 2;/4
MoV g_dwtimetexttop, eax
--- Calculate the X coordinate of the output position of the information
MoV eax, sttm. tmavecharwidth; tmmaxcharwidth
MoV BL, c_remaintimelen/2
Mul BL
MoV EBX, c_timewinwidth/2
Sub EBX, eax
MoV g_dwtimetextleft, EBX; eax
RET
Gettimetextpos endp
;///////////////////////////////
Function: Move the countdown window to the bottom right corner of the screen.
; Input: None
; Output: None
;//////////////////////////////
Setwintoscrrightdown proc
Local stdeskrect: rect
--- Take the desktop workspace height
Invoke systemparametersinfo, spi_getworkarea, null, ADDR stdeskrect, null
--- X coordinate in the upper left corner of the Countdown window
MoV eax, stdeskrect. Right
Sub eax, c_timewinwidth
--- Y coordinate in the upper left corner of the Countdown window
MoV EBX, stdeskrect. Bottom
Sub EBX, c_timewinheight
; --- Move the window
Invoke setwindowpos, g_hwndmain, hwnd_topmost, eax, EBX/
, C_timewinwidth, c_timewinheight, swp_noredraw; swp_showwindow
RET
Setwintoscrrightdown endp
Wndproc proc hwnd: hwnd, umsg: uint, wparam: wparam, lparam: lparam
. If umsg = wm_destroy
Invoke postquitmessage, null
. Elseif umsg = wm_create
MoV eax, hwnd
MoV g_hwndmain, eax
Invoke settimer, g_hwndmain, id_timer, 1000, null
Invoke setwintoscrrightdown; move the countdown window to the bottom right corner of the screen
Invoke gettimetextpos; calculates and displays the output position of the Countdown information.
. Elseif umsg = wm_timer
Dec g_dwremaintime
. If g_dwremaintime = 30
--- Display the window from bottom to top in 30 seconds
Invoke animatewindow, g_hwndmain, 500 h, aw_activate + aw_ver_negative; aw_blend
. Endif
. If g_dwremaintime> 0
; --- Convert the number of seconds to characters
MoV eax, g_dwremaintime
MoV DL, 10
Div DL
Add ax, '00'
MoV word PTR g_bd10, ax
; --- Required to redraw the window
Invoke invalidaterect, g_hwndmain, null, true
JMP @ wndproczeroret
. Else
Invoke killtimer, g_hwndmain, id_timer
. Endif
. Elseif umsg = wm_paint
Invoke beginpaint, g_hwndmain, ADDR g_stptst
MoV g_hdc, eax; get handle to device context
--- Output countdown Information
Invoke textout, g_hdc, g_dwtimetextleft, g_dwtimetexttop, ADDR g_szremaintime, c_remaintimelen
Invoke endpaint, hwnd, ADDR g_stptst
. Else
@@:
Invoke defwindowproc, hwnd, umsg, wparam, lparam
RET
. Endif
@ Wndproczeroret:
XOR eax, eax
RET
Wndproc endp
End start