Timers, Edits, Something is screwed up =\

This is a discussion on Timers, Edits, Something is screwed up =\ within the Windows Programming forums, part of the Platform Specific Boards category; Ok, i cannot figure out why this is not working, i am making a program that Logs off, Shuts down, ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Timers, Edits, Something is screwed up =\

    Ok, i cannot figure out why this is not working, i am making a program that Logs off, Shuts down, or restarts your computer after the time you put, I am having a problem with getting the number out of the text box, please help me get this straightend out

    Oh, and BTW, the things that are in comments, are things ive tried before but didnt work:

    Code:
    #include <windows.h>
    #include <fstream.h>
    #include "controls.h"
    
    #define WIN32_LEAN_AND_MEAN
    
    const char ClassName[] = "WndShutDown";
    
    HINSTANCE hInstance;
    
    ofstream fin("errors.txt");
    
    typedef struct _wh {
    	int Width;
    	int Height;
    }WH;
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	WNDCLASSEX	wc;
    	HWND		hwnd;
    	MSG			msg;
    	WH			Main;
    
    	Main.Height			= 100;
    	Main.Width			= 300;
    
    	wc.cbClsExtra		= 0;
    	wc.cbSize			= sizeof(WNDCLASSEX);
    	wc.cbWndExtra		= 0;
    	wc.hbrBackground	= (HBRUSH)(COLOR_BTNFACE + 1);
    	wc.hCursor			= LoadCursor(hInstance, IDC_ARROW);
    	wc.hIcon			= LoadIcon(hInstance, IDI_APPLICATION);
    	wc.hIconSm			= LoadIcon(hInstance, IDI_APPLICATION);
    	wc.hInstance		= hInstance;
    	wc.lpfnWndProc		= WndProc;
    	wc.lpszClassName	= ClassName;
    	wc.lpszMenuName		= NULL;
    	wc.style			= 0;
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Error Registering Window Class", "Error!", MB_OK);
    		return 0;
    	}
    
    	hwnd = CreateWindowEx(	0,
    							ClassName,
    							"ShutDown Time!",
    							WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU,
    							CW_USEDEFAULT,
    							CW_USEDEFAULT,
    							Main.Width,
    							Main.Height,
    							NULL,
    							NULL,
    							hInstance,
    							NULL);
    
    	if(!hwnd)
    	{
    		MessageBox(NULL, "Error Creating Window", "Error!", MB_OK);
    		return 0;
    	}
    
    	while(GetMessage(&msg, hwnd, 0, 0) > 0)
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	ShowWindow(hwnd, nShowCmd);
    	UpdateWindow(hwnd);
    
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	int time;
    
    	switch(msg)
    	{
    	case WM_CREATE:
    		hTime =		CreateWindowEx(0, TEXT("EDIT"), TEXT("Time (In Seconds)"), WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER | ES_CENTER, 10, 10, 180, 15, hwnd, (HMENU)IDC_TIME, hInstance, NULL);
    		hStart =	CreateWindowEx(0, TEXT("BUTTON"), TEXT("Start"), WS_CHILD | WS_VISIBLE | BS_FLAT, 190, 10, 100, 15, hwnd, (HMENU)IDC_START, hInstance, NULL);
    		hLogoff =	CreateWindowEx(0, TEXT("BUTTON"), TEXT("Logoff"), WS_CHILD | WS_VISIBLE | BS_FLAT | BS_AUTOCHECKBOX, 10, 30, 80, 15, hwnd, (HMENU)IDC_LOGOFF, hInstance, NULL); 
    		hShutdown = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Shutdown"), WS_CHILD | WS_VISIBLE | BS_FLAT | BS_AUTOCHECKBOX, 10, 50, 85, 15, hwnd, (HMENU)IDC_SHUTDOWN, hInstance, NULL);
    		hRestart =	CreateWindowEx(0, TEXT("BUTTON"), TEXT("Restart"), WS_CHILD | WS_VISIBLE | BS_FLAT | BS_AUTOCHECKBOX, 100, 30, 80, 15, hwnd, (HMENU)IDC_RESTART, hInstance, NULL);
    		
    		CheckDlgButton(hwnd, IDC_LOGOFF, BST_CHECKED);
    		bLogoff = true;
    
    		break;
    
    	case WM_COMMAND:
    		{
    			switch(LOWORD(wParam))
    			{
    			case IDC_START:
    				{
    					//time = GetDlgItemInt(hwnd, IDC_TIME, NULL, false);
    					//SendMessage(hTime, WM_GETTEXT, 0, (INT)time);
    					
    					GetWindowText(hTime, (LPTSTR)time, NULL);
    
    					fin << time;
    					fin.close();
    
    					EnableWindow(hTime, false);
    					EnableWindow(hLogoff, false);
    					EnableWindow(hShutdown, false);
    					EnableWindow(hRestart, false);
    					EnableWindow(hStart, false);
    
    					SetTimer(hwnd, IDC_TIMER, 1000, NULL);
    
    				}
    				break;
    
    
    			case IDC_LOGOFF:
    				{
    					cLogoff = IsDlgButtonChecked(hwnd, IDC_LOGOFF);
    
    					if(cLogoff & BST_CHECKED)
    					{
    						bLogoff = true;
    						bShutdown = false;
    						bRestart = false;
    
    						CheckDlgButton(hwnd, IDC_SHUTDOWN, BST_UNCHECKED);
    						CheckDlgButton(hwnd, IDC_RESTART, BST_UNCHECKED);
    					} 
    					else //if(cLogoff & BST_UNCHECKED)
    						CheckDlgButton(hwnd, IDC_LOGOFF, BST_CHECKED);
    				}
    				break;
    
    			case IDC_SHUTDOWN:
    				{
    					cShutdown = IsDlgButtonChecked(hwnd, IDC_SHUTDOWN);
    
    					if(cShutdown & BST_CHECKED)
    					{
    						bLogoff = false;
    						bShutdown = true;
    						bRestart = false;
    
    						CheckDlgButton(hwnd, IDC_LOGOFF, BST_UNCHECKED);
    						CheckDlgButton(hwnd, IDC_RESTART, BST_UNCHECKED);
    					} 
    					else //if(cShutdown & BST_UNCHECKED)
    						CheckDlgButton(hwnd, IDC_SHUTDOWN, BST_CHECKED);
    				}
    				break;
    
    			case IDC_RESTART:
    				{
    					cRestart = IsDlgButtonChecked(hwnd, IDC_RESTART);
    
    					if(cRestart & BST_CHECKED)
    					{
    						bLogoff = false;
    						bShutdown = false;
    						bRestart = true;
    
    						CheckDlgButton(hwnd, IDC_LOGOFF, BST_UNCHECKED);
    						CheckDlgButton(hwnd, IDC_SHUTDOWN, BST_UNCHECKED);
    					} 
    					else //if(cShutdown & BST_UNCHECKED)
    						CheckDlgButton(hwnd, IDC_RESTART, BST_CHECKED);
    				}
    				break;
    			}
    		}
    		break;
    
    	case WM_TIMER:
    		{
    			switch(wParam)
    			{
    			case IDC_TIMER:
    				{
    					if(time > 0)
    					{
    						time--;
    						SetWindowText(hTime, (LPTSTR)time);
    					}
    
    					if(time == 0)
    					{
    						EnableWindow(hTime, true);
    						EnableWindow(hLogoff, true);
    						EnableWindow(hShutdown, true);
    						EnableWindow(hRestart, true);
    						EnableWindow(hStart, true);
    						KillTimer(hwnd, IDC_TIMER);
    					}
    				}
    				break;
    			}
    		}	
    		break;
    
    
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		break;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	
    	default:
    		return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    
    	return 0;
    }
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Hi,

    I didn't copy and paste your code to see if this would work (i'm at work at the mo ), but i did notice one thing.

    Your WndProc funtion is where you store the time variable, so the value that gets stored doesn't stick!! Remember, this function is called a bzillion times by windows, and therefore time is created and destroyed a bzillion times.

    If you get the value of time when it hasnt' been initialised (which is what happens when you receive the WM_TIMER messages), then you'll have an enormous value stored in it! (which explains the behaviour you explained in your previous thread).

    So, 2 solutions:

    1 - make the time variable static, or
    2 - make the time variable part of the global scope

    hope that helps!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    OmG!
    I cant believe i didnt notice that, THANK YOU SO MUCH URALDOR!
    (sorry for the caps) My Timer works sweet now
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    hehe... no problems. glad you're all fixed up!

    Sometimes it's hard to see things coz you're looking too hard! (i have the same problem ).

    good luck!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    50
    Just a quick question that should be simple though I don't know enough about the Windows environment to know this for sure...


    Where do you place a variable to make it part of the global scope? Outside all the functions, or inside WinMain?

    Thanks

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by bluecoder
    Just a quick question that should be simple though I don't know enough about the Windows environment to know this for sure...


    Where do you place a variable to make it part of the global scope? Outside all the functions, or inside WinMain?

    Thanks
    WinMain and main are just like any other function except they are called by your operating system instead of being called by you.....

    Any variable decared in WinMain will only have WinMain scope....if you want global scope then look at declaring it outside of any functions.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I screwed my pendrive
    By abh!shek in forum Tech Board
    Replies: 0
    Last Post: 06-30-2008, 02:32 PM
  2. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 08:56 AM
  3. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 04:45 PM
  4. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21