"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 ;
}