How to get the message time from message structure ?

Windows specific questions.
Post Reply
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

How to get the message time from message structure ?

Post by kcvinu »

Hi all,
Forgive me if this question is already asked. I would like to know how to get the message time from the MSG structure in win32 Gui programming ? I just tried this in the " GetMessage" function. But since, it's a DWORD, i only get big number.

Code: Select all

        TranslateMessage(imsg)
        DispatchMessage(imsg)
        print imsg.time, " - Message time"
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How to get the message time from message structure ?

Post by SARG »

Hi,

Use the GetTimeFormat function, a WDS api. If you need more do not hesitate.

Extract from SDK reference :
The GetTimeFormat function formats a time as a time string for a specified locale. The function formats either a specified time or the local system time.

int GetTimeFormat(

LCID Locale, // locale for which time is to be formatted
DWORD dwFlags, // flags specifying function options
CONST SYSTEMTIME *lpTime, // time to be formatted
LPCTSTR lpFormat, // time format string
LPTSTR lpTimeStr, // buffer for storing formatted string
int cchTime // size, in bytes or characters, of the buffer
);
Parameters
Locale

Specifies the locale for which the time string is to be formatted. If lpFormat is NULL, the function formats the string according to the time format for this locale. If lpFormat is not NULL, the function uses the locale only for information not specified in the format picture string (for example, the locale's time markers).

This parameter can be a locale identifier created by the MAKELCID macro, or one of the following predefined values:

LOCALE_SYSTEM_DEFAULT Default system locale.
LOCALE_USER_DEFAULT Default user locale.

dwFlags

A set of bit flags that specify various function options. You can specify a combination of the following flags:

Flag Meaning
LOCALE_NOUSEROVERRIDE If set, the function formats the string using the system default time format for the specified locale. If not set, the function formats the string using any user overrides to the locale's default time format. This flag cannot be set if lpFormat is non-NULL.
TIME_NOMINUTESORSECONDS Do not use minutes or seconds.
TIME_NOSECONDS Do not use seconds.
TIME_NOTIMEMARKER Do not use a time marker.
TIME_FORCE24HOURFORMAT Always use a 24-hour time format.

lpTime

Pointer to a SYSTEMTIME structure that contains the time information to be formatted. If this pointer is NULL, the function uses the current local system time.

lpFormat

Pointer to a format picture to use to form the time string. If lpFormat is NULL, the function uses the time format of the specified locale.

Use the following elements to construct a format picture string. If you use spaces to separate the elements in the format string, these spaces will appear in the same location in the output string. The letters must be in uppercase or lowercase as shown (for example, "ss", not "SS"). Characters in the format string that are enclosed in single quotation marks will appear in the same location and unchanged in the output string.

Picture Meaning
h Hours with no leading zero for single-digit hours; 12-hour clock
hh Hours with leading zero for single-digit hours; 12-hour clock
H Hours with no leading zero for single-digit hours; 24-hour clock
HH Hours with leading zero for single-digit hours; 24-hour clock
m Minutes with no leading zero for single-digit minutes
mm Minutes with leading zero for single-digit minutes
s Seconds with no leading zero for single-digit seconds
ss Seconds with leading zero for single-digit seconds
t One character time marker string, such as A or P
tt Multicharacter time marker string, such as AM or PM



For example, to get the time string

"11:29:40 PM"

use the following picture string:

"hh':'mm':'ss tt"

lpTimeStr

Pointer to a buffer that receives the formatted time string.

cchTime

Specifies the size, in bytes (ANSI version) or characters (Unicode version), of the lpTimeStr buffer. If cchTime is zero, the function returns the number of bytes or characters required to hold the formatted time string, and the buffer pointed to by lpTimeStr is not used.


Return Values

If the function succeeds, the return value is the number of bytes (ANSI version) or characters (Unicode version) written to the buffer pointed to by lpTimeStr. If the cchTime parameter is zero, the return value is the number of bytes or characters required to hold the formatted time string.

If the function fails, the return value is zero. To get extended error information, call GetLastError. GetLastError may return one of the following error codes:

ERROR_INSUFFICIENT_BUFFER
ERROR_INVALID_FLAGS
ERROR_INVALID_PARAMETER
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to get the message time from message structure ?

Post by kcvinu »

@SARG ,
Thanks for this reply. Let me try. :)
Pierre Bellisle
Posts: 56
Joined: Dec 11, 2016 17:22

Re: How to get the message time from message structure ?

Post by Pierre Bellisle »

(DWORD) .time is the number of milliseconds since the system was started.
A roll over will occur after 49.7 days.
Pierre Bellisle
Posts: 56
Joined: Dec 11, 2016 17:22

Re: How to get the message time from message structure ?

Post by Pierre Bellisle »

For the fun of it...

Code: Select all

#define JumpCompiler "<D:\Free\32\fbc.exe>" 'Compiler to use "< = left delimiter, >" = right delimiter
#define JumpCompilerCmd "<-s console -w pedantic "D:\Free\bas\~~Default.rc">" 'Command line to send to compiler (gui or console)

#Lang "fb"
#Define Unicode
#Include Once "Windows.bi"
#Include Once "String.bi"
'  ___________________________________________________________________________
' | TickCount = 9,300,1080                                                    |
' |                                                                           |
' | Computer booted since 9,300,1080 miliseconds.                             |
' | Computer booted since 0 days 2 hours, 35 minutes, 0.8742108 seconds       |
' | Computer booted on vendredi 29 novembre 2019 (2019-11-29) at 15:01:38.922 |
' |                                                                           |
' | Press a key or click to end                                               |
'  ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
'_____________________________________________________________________________

Dim zDate       AS WSTRING * 128
Dim zTime       AS WSTRING * 13
Dim System_Time AS SYSTEMTIME
Dim File_Time   AS ULongInt 'FILETIME
Dim TickCount   AS DWORD

Dim milliseconds As DWORD
Dim seconds As DWORD
Dim minutes As DWORD
Dim hours As DWORD
Dim days As DWORD

 TickCount = GetTickCount() 'Mili seconds DWORD
 Print "TickCount = " & FORMAT(TickCount, "0,")
 Print

 milliseconds = TickCount
 seconds      = milliseconds \ 1000
 minutes      = seconds      \ 60
 hours        = minutes      \ 60
 days         = hours        \ 24

 milliseconds = milliseconds - seconds * 60
 seconds      = seconds - minutes      * 60
 minutes      = minutes - hours        * 60
 hours        = hours - days           * 24

 Print "Computer booted since " & FORMAT(TickCount, "0,")  & " miliseconds."

 Print "Computer booted since " & FORMAT(days) & " days " & FORMAT(hours) & " hours, " & _
       FORMAT(minutes) & " minutes, " &  FORMAT(seconds)  & "." & FORMAT(milliseconds) & " seconds"

 GetLocalTime(@System_Time) 'Not UTC time
 SystemTimeToFileTime(@System_Time, Cast(lpFileTime, @File_Time)) 'In 100-nanosecond step,  1,000,000 nanoseconds = one millisecond
 File_Time = File_Time - TickCount * 10000 ' * 1,000,000 / 100  (1 millisecond = 1,000,000 nanoseconds)
 FileTimeToSystemTime(Cast(lpFileTime, @File_Time), @System_Time)
 GetDateFormat(LOCALE_USER_DEFAULT, 0, @System_Time, "dddd d MMMM yyyy (yyyy-MM-dd)", zDate, SIZEOF(zDate))
 GetTimeFormat(LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT, @System_Time, "HH:mm:ss.", @zTime, SIZEOF(zTime))
 PRINT "Computer booted on " & zDate & " at " & zTime & FORMAT(System_Time.wMilliseconds)

 Print : Print "Press a key or click to end" : Dim buttons As Long
 Color 7
 Do : GetMouse(0, 0, 0, Buttons) : IF buttons Or Len(InKey) Then Exit Do : End If : Sleep 20 : Loop
'_____________________________________________________________________________
'
Last edited by Pierre Bellisle on Nov 30, 2019 2:13, edited 1 time in total.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: How to get the message time from message structure ?

Post by paul doe »

Doesn't work with #lang "Fb", Pierre. Either use a different dialect or remove all the suffixes from the format() calls...
Pierre Bellisle
Posts: 56
Joined: Dec 11, 2016 17:22

Re: How to get the message time from message structure ?

Post by Pierre Bellisle »

Yep, I was using the older 1.05.0.
Code updated...
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: How to get the message time from message structure ?

Post by paul doe »

Works flawlessly now. Nice.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to get the message time from message structure ?

Post by dodicat »

Nice one Pierre Bellisle.
Here it is in a while loop (using the millisecond property)

Code: Select all

#include "windows.bi"
#include "fbgfx.bi"

dim as msg  imsg
dim as long firstone
screenres 300,200,,0,fb.GFX_ALWAYS_ON_TOP
width 300\8,200\16
color 4,3
windowtitle "WORK WINDOW"
screencontrol 100,400,200
cls
var MainWindow =  CreateWindowEx(NULL, "#32770", "DUMMY WINDOW",  WS_VISIBLE, 200,200,200, 200, NULL, NULL, NULL, NULL)

GetMessage(@imsg,0,0,0)
firstone= imsg.time

While GetMessage(@imsg,0,0,0)
    var key=inkey
TranslateMessage(@imsg)
'DispatchMessage(@imsg) 'skip, this waits on mainwindow
locate 4,4
if key=chr(27) or key= chr(255)+"k" then end
print str((imsg.time-firstone)\1000)+" seconds elapsed time"
sleep 10
wend
 
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to get the message time from message structure ?

Post by kcvinu »

Thank you everyone for the replys. :) Sorry for the late reply.
Post Reply