I am trying to create a thread to continually update the time in the textbox. The issue is how do you reference a textbox in that was created in the main section of the program?
main.cpp
#include <windows.h>
#include <ctime>
#include "resource.h"
static bool keepRunning = true;
static HANDLE hThread = NULL;
static void run();
static void end();
static DWORD WINAPI ThreadTime(LPVOID lpParam);
extern char strCurrentTime[8];
HWND hWnd;
HINSTANCE hInst;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_CONTROLS_DLG), hWnd,
reinterpret_cast<DLGPROC>(DlgProc));
hInst = hInstance;
return 0;
}
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
char strMeetingTime[8], strStartTime[8], strBreak1Start[8], strLunch[8],
strBreak2Start[8], strLeaveTime[8], strLateBreak[8], strTime[10];
HWND hWndCurrentTime, hWndMeetingTime, hWndStartTime, hWndBreak1,
hWndLunch, hWndBreak2, hWndLeaveTime, hWndLateBreak;
hWndCurrentTime = GetDlgItem(hWndDlg, IDC_CURRENT_TIME);
hWndMeetingTime = GetDlgItem(hWndDlg, IDC_MEETING_TIME);
hWndStartTime = GetDlgItem(hWndDlg, IDC_START_TIME);
hWndBreak1 = GetDlgItem(hWndDlg, IDC_BREAK1);
hWndLunch = GetDlgItem(hWndDlg, IDC_LUNCH);
hWndBreak2 = GetDlgItem(hWndDlg, IDC_BREAK2);
hWndLeaveTime = GetDlgItem(hWndDlg, IDC_LEAVE_TIME);
hWndLateBreak = GetDlgItem(hWndDlg, IDC_LATE_BREAK_BTN);
switch(Msg)
{
case WM_INITDIALOG:
run(); // start thread for showing current time
SetWindowText(hWndMeetingTime, "");
SetWindowText(hWndStartTime, "");
SetWindowText(hWndBreak1, "");
SetWindowText(hWndLunch, "");
SetWindowText(hWndBreak2, "");
SetWindowText(hWndLeaveTime, "");
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDC_LATE_BREAK_BTN:
// Start a 15 minute timer
return TRUE;
case IDCANCEL:
end();
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
static void run()
{
DWORD dummy;
hThread = CreateThread(NULL, 0, ThreadTime, NULL, 0, &dummy);
}
static void end()
{
keepRunning = false;
}
static DWORD WINAPI ThreadTime(LPVOID lpParam)
{
while(keepRunning)
{
time_t rawtime;
struct tm * timeinfo;
time( &rawtime);
timeinfo = localtime ( &rawtime);
strftime(strCurrentTime,80,"%I:%M:%S %p",timeinfo);
SetWindowText(hWndCurrentTime, strCurrentTime);
Sleep(1000);
}
return 0;
}