Tech Support > Microsoft Windows > Development Resources > Problem with Timer in Dialog - isn't the Timer!!
Problem with Timer in Dialog - isn't the Timer!!
Posted by Daniel Miller on July 9th, 2003


I previously posted a message here about getting two WM_TIMER events in
my Dialog window. Since then, I made a couple of discoveries:

1. I disabled the Timer control completely, and

2. I typed one keyboard character into an Edit control that I've created
on the Dialog box...

.... and I got FOUR characters in response to typing only one!!!

I looked at the activity with Spy+, and I'm actually getting four
EN_UPDATE and four EN_CHANGE events for each character that I type!!!

Okay, so the Timer has nothing to do with the problem, and I'm clearly
doing something very wrong in my program. I wonder what??

I don't even *have* a keyboard handler for the Edit controls; the
original Dialog was created specifying:

wc.lpfnWndProc = DefDlgProc;

and passing my window procedure to CreateDialog(); so I assumed there's a
default keyboard handler for the Edit controls, and I don't need to
handle the keyboard... is this a naive assumption?? Do I actually have
to create a keyboard handler for each separate edit control?? The
reference code that I got this from just let the user type whatever they
want into the control, then used GetWindowText() to get whatever was
there once the Update button was clicked... However, his sample program
was a Window, not a Dialog.

.... and I *still* have no idea why I'm getting multiple events for one
action... ???

Posted by Daniel Miller on July 9th, 2003


bunny <xbunny@eidosnet.co.uk> wrote in
news:3F0C4535.3060600@eidosnet.co.uk:

RegisterClass().

Sadly, tho, I'm still getting multiple events for each input...
???



Posted by Matti Vuori on July 9th, 2003


Daniel Miller <dmiller@home.net> wrote in news:3f0c442c$1_3@corp-
news.newsgroups.com:

Perhaps, if you post your full code, someone can see what's wrong.

--
Matti Vuori, <http://sivut.koti.soon.fi/mvuori/index-e.htm>


Posted by Daniel Miller on July 10th, 2003


"Tim Robinson" <tim.at.gaat.freeserve.co.uk@invalid.com> wrote in
news:bei2ot$5itjf$1@ID-103400.news.dfncis.de:

I'll trim out the unrelated stuff and hope it's okay...

#include <windows.h>
#include <stdio.h>
#include <windowsx.h>
#include <time.h>
#include <shellapi.h>

#define IDD_MAINDIALOG 100
#define IDAPPLICON 710
#define ID_TRAYMENU 2000
#define ID_TRAYOPEN 2010
#define ID_TRAYEXIT 2020

#define IDT_TIMER (201)

// Define control identifiers
#define IDC_EDIT1 1001
#define IDC_EDIT2 1002
#define IDC_EDIT3 1003
#define IDC_STATIC 1020
#define IDC_BUTTON 1030
#define IDC_CHECK 1040

// definitions for dialog controls
static HWND hwndButton1;
static HWND hwndButton2;
static HWND hwndButton3;
static HWND hwndStatic1;
static HWND hwndStatic2;
static HWND hwndStatic3;
static HWND hwndStatic4;
static HWND hwndStatic5;
static HWND hwndTagline;
static HWND hwndStatus;
static HWND hwndEdit1;
static HWND hwndEdit2;
static HWND hwndEdit3;

// positions of dialog controls
#define REF_ROW 30
#define SIG_ROW 70
#define PERIOD_ROW 110
#define TAG_ROW 150
#define STATUS_ROW 210

#define LABEL_COL 10
#define LABEL_LEN 160

#define DATA_COL (LABEL_LEN + 10)
#define BUTTON_COL (DATA_COL + 430)

#define FIELD_LEN 400
#define TAGOUT_LEN 600
#define NUMBER_LEN 50
#define BUTTON_WIDTH 160

#define FIELD_HEIGHT 20
#define BUTTON_HEIGHT 30

HINSTANCE hInstance;
// HWND hwnd;
HWND hwndMain;

UINT timerID = 0 ;

char tempstr[260] ;

// how many seconds between updates??
unsigned cycle_time = 10 ;

char tag_ref_name[_MAX_PATH] = "d:\\taglines.txt" ;
char tag_out_name[_MAX_PATH] = "d:\\my_sig.txt" ;

#define MAX_TAG_LEN (1024)
char inpstr[MAX_TAG_LEN] ;

//************************************************** *******************
static int generate_new_tagline(HWND hwnd);
static void update_timer_count(HWND hwnd);
static BOOL CALLBACK DialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam,
LPARAM lParam);

//************************************************** ********************
int APIENTRY WinMain (HINSTANCE hInst, HINSTANCE hinstPrev,
LPSTR lpCmdLine, int nCmdShow)
{
// WNDCLASS wc ;
// WNDCLASS wc1;
MSG msg;

my_srand() ;
hInstance = hInst;

// create modeless dialog window
if (!(hwndMain = CreateDialog (hInst,
// MAKEINTRESOURCE (IDD_MAINDIALOG), NULL, NULL)))
MAKEINTRESOURCE (IDD_MAINDIALOG), NULL, (DLGPROC) DialogFunc)))
return 0;

// this was originally done on WM_CREATE...
timerID = SetTimer(hwndMain, IDT_TIMER, 1000, (TIMERPROC) NULL) ;

// remove this line to start out minimized to toolbar
ShowWindow(hwndMain, nCmdShow) ;

// process Windows messages
while (GetMessage (&msg, NULL, 0, 0)) {
if (IsDialogMessage (hwndMain, &msg)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}

return msg.wParam;
}

//************************************************** ********************
// A function to set a control's text to the default font
int SetDefaultFont (int identifier, HWND hwnd)
{
SendDlgItemMessage (hwnd,
identifier,
WM_SETFONT,
(WPARAM) GetStockObject (DEFAULT_GUI_FONT), MAKELPARAM (TRUE, 0));
return 0;
}

// A function to create static text
HWND CreateStatic (char *tempText, int x, int y, int width, int height,
int identifier, HWND hwnd)
{
HWND hStaticTemp;

hStaticTemp = CreateWindowEx (0,
"STATIC",
tempText,
WS_CHILD | WS_VISIBLE,
x, y, width, height, hwnd, (HMENU) identifier, hInstance, NULL);
return hStaticTemp;
}

// A function to create a textarea
HWND CreateEdit (char *tempText, int x, int y, int width, int height,
int identifier, HWND hwnd)
{
HWND hEditTemp;

hEditTemp = CreateWindowEx (WS_EX_CLIENTEDGE,
"EDIT",
tempText,
WS_CHILD | WS_VISIBLE,
x, y, width, height, hwnd, (HMENU) identifier, hInstance, NULL);
return hEditTemp;
}

// A function to create a button
HWND CreateButton (char *tempText, int x, int y, int width, int height,
int identifier, HWND hwnd)
{
HWND hButtonTemp;

hButtonTemp = CreateWindowEx (0,
"BUTTON",
tempText,
WS_CHILD | WS_VISIBLE,
x, y, width, height, hwnd, (HMENU) identifier, hInstance, NULL);
return hButtonTemp;
}

// A function to create a checkbox
HWND CreateCheck (char *tempText, int x, int y, int width, int height,
int identifier, HWND hwnd)
{
HWND hCheckTemp;

hCheckTemp = CreateWindowEx (0,
"BUTTON",
tempText,
WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
x, y, width, height, hwnd, (HMENU) identifier, hInstance, NULL);
return hCheckTemp;
}

//************************************************** ********************
static BOOL CALLBACK DialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam,
LPARAM lParam)
{
static NOTIFYICONDATA NotifyIconData;
static HMENU hMenu;
static int chg_count = 0 ;
static int upd_count = 0 ;
POINT MouseCoordinates;

switch (msg) {
case WM_INITDIALOG:
// Create other controls

// static labels
SetDefaultFont (IDC_STATIC, hwndDlg);
hwndStatic1 = CreateStatic ("Reference file: ", LABEL_COL,
REF_ROW, LABEL_LEN, FIELD_HEIGHT, IDC_STATIC, hwndDlg);
hwndStatic2 = CreateStatic ("Signature file: ", LABEL_COL,
SIG_ROW, LABEL_LEN, FIELD_HEIGHT, IDC_STATIC, hwndDlg);
hwndStatic3 = CreateStatic ("Update period (secs): ", LABEL_COL,
PERIOD_ROW, LABEL_LEN, FIELD_HEIGHT, IDC_STATIC, hwndDlg);
hwndStatic4 = CreateStatic ("Current tagline: ", LABEL_COL,
TAG_ROW, LABEL_LEN, FIELD_HEIGHT, IDC_STATIC, hwndDlg);
hwndTagline = CreateStatic ("unknown", DATA_COL,
TAG_ROW, TAGOUT_LEN, 2*FIELD_HEIGHT, IDC_STATIC, hwndDlg);
hwndStatic5 = CreateStatic ("Status: ", LABEL_COL,
STATUS_ROW, LABEL_LEN, FIELD_HEIGHT, IDC_STATIC, hwndDlg);
hwndStatus = CreateStatic ("none", DATA_COL,
STATUS_ROW, FIELD_LEN, FIELD_HEIGHT, IDC_STATIC, hwndDlg);

// edit fields
SetDefaultFont (IDC_EDIT1, hwndDlg);
hwndEdit1 = CreateEdit (tag_ref_name, DATA_COL, REF_ROW,
FIELD_LEN, FIELD_HEIGHT, IDC_EDIT1, hwndDlg);
SetDefaultFont (IDC_EDIT2, hwndDlg);
hwndEdit2 = CreateEdit (tag_out_name, DATA_COL, SIG_ROW,
FIELD_LEN, FIELD_HEIGHT, IDC_EDIT2, hwndDlg);
SetDefaultFont (IDC_EDIT3, hwndDlg);
sprintf(tempstr, "%u", cycle_time) ;
hwndEdit3 = CreateEdit (tempstr, DATA_COL, PERIOD_ROW,
NUMBER_LEN, FIELD_HEIGHT, IDC_EDIT3, hwndDlg);

// buttons
SetDefaultFont (IDOK, hwndDlg);
hwndButton1 = CreateButton ("Minimize to tray", BUTTON_COL,
REF_ROW-5, BUTTON_WIDTH, BUTTON_HEIGHT, IDOK, hwndDlg);
SetDefaultFont (IDCANCEL, hwndDlg);
hwndButton2 = CreateButton ("Exit", BUTTON_COL,
SIG_ROW-5, BUTTON_WIDTH, BUTTON_HEIGHT, IDCANCEL, hwndDlg);
SetDefaultFont (IDC_BUTTON, hwndDlg);
hwndButton3 = CreateButton ("Update variables", BUTTON_COL,
PERIOD_ROW-5, BUTTON_WIDTH, BUTTON_HEIGHT, IDC_BUTTON, hwndDlg);

// create tray menu
hMenu = LoadMenu (hInstance, MAKEINTRESOURCE (ID_TRAYMENU));

// put the icon into a system tray
NotifyIconData.cbSize = sizeof (NOTIFYICONDATA);
NotifyIconData.hWnd = hwndDlg;
NotifyIconData.uID = 0;
NotifyIconData.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
NotifyIconData.uCallbackMessage = WM_USER; // tray events will
generate WM_USER event
// NotifyIconData.uCallbackMessage = WM_TRAYNOTIFY;
NotifyIconData.hIcon = (HICON) LoadImage (hInstance,
MAKEINTRESOURCE (IDAPPLICON), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); //
load 16 x 16 pixels icon
lstrcpy (NotifyIconData.szTip, "This is tray tip: both left and
right clicks work here!"); // max 64 characters

Shell_NotifyIcon (NIM_ADD, &NotifyIconData);

return TRUE;
// break;

case WM_USER:
// case WM_TRAYNOTIFY:
// event genereted by a system tray - the type of tray event that
// generated the message can be found in lParam
switch (lParam) {
case WM_LBUTTONUP:
// display a tray menu
GetCursorPos (&MouseCoordinates);
TrackPopupMenu (GetSubMenu (hMenu, 0), TPM_RIGHTALIGN |
TPM_LEFTBUTTON, MouseCoordinates.x, MouseCoordinates.y, 0, hwndDlg,
NULL);
break;

case WM_RBUTTONUP:
// show window as response to right-clicking the tray icon
ShowWindow (hwndDlg, SW_SHOWNORMAL);
SetForegroundWindow (hwndDlg);
break;
}
break;

case WM_TIMER:
switch (wParam) {
case IDT_TIMER:
update_timer_count(hwndMain) ;
break;
}
break;

case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK: // button
// 'minimize' to tray
ShowWindow (hwndDlg, SW_HIDE);
return 1;

case IDCANCEL: // button
case ID_TRAYEXIT: // menu option
// exit
DestroyWindow (hwndDlg);
return 1;

case ID_TRAYOPEN: // menu option
// open dialog
ShowWindow (hwndDlg, SW_SHOW);
break;
}
break;

case WM_CLOSE:
DestroyWindow (hwndDlg);
return TRUE;

case WM_DESTROY:
if (timerID != 0) {
KillTimer(hwndMain, timerID) ;
timerID = 0 ;
}
// a window is beeing destroyed

case WM_ENDSESSION:
// log-off or shutdown

// remove the icon from a system tray and free .dll handle
Shell_NotifyIcon (NIM_DELETE, &NotifyIconData);

// free menu
DestroyMenu (hMenu);

PostQuitMessage (0);
break;
}
return FALSE;
}

//************************************************** *******************
void update_timer_count(HWND hwnd)
{
static unsigned tcount = cycle_time ;
unsigned tmins ;
unsigned tsecs ;
// skip this function call if time is same as previous call
static clock_t prev_time = 0 ;
clock_t curr_time ;
// FILE *fd ;

curr_time = clock() ;
if (curr_time == prev_time)
return ;
if (tcount == 0) {
tcount = cycle_time ;
// skip this for posting to newsgroup
// generate_new_tagline(hwnd) ;
} else {
tcount-- ;
tmins = tcount / 60 ;
tsecs = tcount % 60 ;

wsprintf(tempstr, "%2u mins, %2u secs ", tmins, tsecs) ;
SetWindowText(hwnd, tempstr) ;
}
prev_time = curr_time ;
}


Posted by Daniel Miller on July 10th, 2003


Daniel Miller <dmiller@home.net> wrote in news:3f0d8f0e_3@corp-
news.newsgroups.com:


#include <windows.h>

710 ICON ctray.ico

100 DIALOG 7, 20, 395, 86
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "System tray example for C applications"
FONT 8, "Comic Sans MS"
BEGIN
PUSHBUTTON "Minimize to tray", IDOK, 326, 6, 63, 14
PUSHBUTTON "Exit", IDCANCEL, 326, 27, 63, 14
END
2000 MENU
BEGIN
POPUP "&TrayMenu"
BEGIN
MENUITEM "&Open", 2010
MENUITEM SEPARATOR
MENUITEM "&Exit", 2020
END
END

Posted by ef on July 10th, 2003


Daniel Miller wrote:
You forgot an '!' in the last line
It should be like this:

if (!IsDialogMessage (hwndMain, &msg)) {

This should fix most of your problems.


Elias


Posted by Daniel Miller on July 10th, 2003


"ef" <efotinisNO@SPAMyahoo.com> wrote in news:1057859876.175396@athprx02:


That's all that was wrong?? Yep, everything seems to work correctly now...

Okay, now I understand the IsDialogMessage() test; it didn't make sense to
me before... it's saying "anything the Dialog doesn't handle automatically,
I'll take care of" ...

Thank you very much!!!

1relievedprogrammer

Posted by Tim Robinson on July 10th, 2003


"Daniel Miller" <dmiller@home.net> wrote in message
news:3f0dd907$1_3@corp-news.newsgroups.com...

// If it's not a dialog message...
if (!IsDialogMessage(hwnd, &msg))
{
// ...perform the normal translations
TranslateMessage(&msg);
// ... and call the WndProc
DispatchMessage(&msg);
}
// Otherwise, the IsDialogMessage function has already handled it


--
Tim Robinson (MVP, Windows SDK)
http://www.themobius.co.uk/




Similar Posts