4 Replies - 12175 Views - Last Post: 10 March 2010 - 06:25 AM Rate Topic: -----

#1 Immortal_King  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 10

CreateWindow input box & timer functioning help

Posted 09 March 2010 - 02:56 PM

Alright, I'm new to C++ programming (started yesterday) programmed a bit with Java and C# until my friend said he could only run C++ >.<

**NOTE** I'm using Dev-C++ Compiler Converted to VC++, just so you're not trying to guess what I'm using :P and source-code is @ bottom of post.

First of all, I need to figure out how to retrieve/change/clear a input box's text when using
 CreateWindow(TEXT("Edit"), "0", 
WS_CHILD | WS_VISIBLE | WS_BORDER,
170, 30, 30, 20, hwnd, (HMENU) 6,
NULL, NULL); 


I know in VC# it's textBox1.Clear(); textBox1.Text = ""; string example = textBox1.Text; but I have no clue what it is in Dev-C++...
I wouldn't ask on something this simple, but I searched like mad and either am searching the wrong query (empty/clear/change/retrieve input box text in C++) or there is no other results.... LOL

Second, It's not really neccesary ( I can probably find this on my own, but it's be nice to see help with this as well, as for I haven't started with this yet so I don't expect it) is I need help forming the code behind my GUI, I'm making a timer application and i needed help with the timer (function?), any help would be appreciated.

And Last but not least, I'm not asking for it, but if you find any code corrections/suggestions I would appreciate a post with a better code snippet :)

*drumroll* How's this look for a one-day-old C++ programmer? (just the GUI, no code behind it yet)
Posted Image

Source code here:
#include <windows.h>

static void chfont(int size, HWND where) {
        HFONT hfffont;
        hfffont = CreateFont(size,
                   0,
                   0,
                   0,
                   FW_BOLD,
                   0,
                   0,
                   0,
                   ANSI_CHARSET,
                   OUT_DEFAULT_PRECIS,
                   CLIP_DEFAULT_PRECIS,
                   DEFAULT_QUALITY,
                   DEFAULT_PITCH | FF_DONTCARE,
                   "Arial");
                   SendMessage(where, WM_SETFONT, (DWORD)hfffont, 0);
                   }

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

  static HWND hwndEdit1;
  static HWND cc;
  static HWND cb;
  static HFONT hfffont;
  static HWND hwndEdit2;
  static HWND hwndEdit3;
  static HWND hwndEdit4;
  static HWND startButton;
  static HWND pauseButton;
  static HWND stopButton;
  static HWND hrlbl;
  static HWND minlbl;
  static HWND seclbl;
  static HWND DisplayFont;
  HINSTANCE hInst;
  static int len;
  static TCHAR text[30];
  


  switch(msg)
  {
    case WM_CREATE:
	hwndEdit1 = CreateWindow(TEXT("Edit"), "0", WS_CHILD | WS_VISIBLE | WS_BORDER,
				90, 30, 30, 20, hwnd, (HMENU) 4,
				NULL, NULL);
				
    hwndEdit2 = CreateWindow(TEXT("Edit"), "0", WS_CHILD | WS_VISIBLE | WS_BORDER,
				130, 30, 30, 20, hwnd, (HMENU) 5,
				NULL, NULL);
				
	hwndEdit3 = CreateWindow(TEXT("Edit"), "0", WS_CHILD | WS_VISIBLE | WS_BORDER,
				170, 30, 30, 20, hwnd, (HMENU) 6,
				NULL, NULL);
				
	hwndEdit4 = CreateWindow(TEXT("Edit"), "Alarm msg here", WS_CHILD | WS_VISIBLE | WS_BORDER,
				90, 80, 110, 20, hwnd, (HMENU) 10,
				NULL, NULL);

	
	CreateWindow(TEXT("STATIC"), "hrs", WS_CHILD | WS_VISIBLE | SS_LEFT, 90, 50, 30, 20, hwnd, (HMENU) 1, hInst, NULL);
	CreateWindow(TEXT("STATIC"), "min", WS_CHILD | WS_VISIBLE | SS_LEFT, 130, 50, 30, 20, hwnd, (HMENU) 2, hInst, NULL);
	CreateWindow(TEXT("STATIC"), "sec", WS_CHILD | WS_VISIBLE | SS_LEFT, 170, 50, 30, 20, hwnd, (HMENU) 3, hInst, NULL);
	
	CreateWindow(TEXT("STATIC"), "Set Time:", WS_CHILD | WS_VISIBLE | SS_LEFT, 17, 30, 70, 20, hwnd, (HMENU) 11, hInst, NULL);
	CreateWindow(TEXT("STATIC"), "Message:", WS_CHILD | WS_VISIBLE | SS_LEFT, 17, 80, 70, 20, hwnd, (HMENU) 12, hInst, NULL);
	
	 hrlbl = CreateWindow(TEXT("STATIC"), "00", WS_CHILD | WS_VISIBLE | SS_LEFT, 35, 180, 50, 50, hwnd, (HMENU) 13, hInst, NULL);
   	    cc = CreateWindow(TEXT("STATIC"), ":", WS_CHILD | WS_VISIBLE | SS_LEFT, 90, 180, 15, 50, hwnd, (HMENU) 14, hInst, NULL);
	minlbl = CreateWindow(TEXT("STATIC"), "00", WS_CHILD | WS_VISIBLE | SS_LEFT, 115, 180, 50, 50, hwnd, (HMENU) 15, hInst, NULL);
  	    cb = CreateWindow(TEXT("STATIC"), ":", WS_CHILD | WS_VISIBLE | SS_LEFT, 170, 180, 115, 50, hwnd, (HMENU) 16, hInst, NULL);
	seclbl = CreateWindow(TEXT("STATIC"), "00", WS_CHILD | WS_VISIBLE | SS_LEFT, 195, 180, 50, 50, hwnd, (HMENU) 17, hInst, NULL);

    CreateWindow(TEXT("STATIC"), "Hours:", WS_CHILD | WS_VISIBLE | SS_LEFT, 15, 160, 70, 20, hwnd, (HMENU) 18, hInst, NULL);
    CreateWindow(TEXT("STATIC"), "Minutes:", WS_CHILD | WS_VISIBLE | SS_LEFT, 80, 160, 70, 20, hwnd, (HMENU) 19, hInst, NULL);
    CreateWindow(TEXT("STATIC"), "Seconds:", WS_CHILD | WS_VISIBLE | SS_LEFT, 155, 160, 70, 20, hwnd, (HMENU) 20, hInst, NULL);

    chfont(50, hrlbl);
    chfont(50, cc);
    chfont(50, minlbl);
    chfont(50, cb);
    chfont(50, seclbl);


	startButton = CreateWindow(
		TEXT("button"), TEXT("Start"),       
		WS_VISIBLE | WS_CHILD,  
		215, 25, 50, 25,        
		hwnd, (HMENU) 7, NULL, NULL); 
        
    pauseButton = CreateWindow(
		TEXT("button"), TEXT("Pause"),       
		WS_VISIBLE | WS_CHILD,  
		215, 60, 50, 25,        
		hwnd, (HMENU) 8, NULL, NULL);
       
    stopButton = CreateWindow(
		TEXT("button"), TEXT("Stop"),       
		WS_VISIBLE | WS_CHILD,  
		215, 95, 50, 25,        
		hwnd, (HMENU) 9, NULL, NULL);     

	break;
	
	case WM_COMMAND:	
           if (LOWORD(wParam) == 7) {
               MessageBox(NULL, "START", "START", 0);
           }
           if (LOWORD(wParam) == 8) {
               MessageBox(NULL, "PAUSE", "PAUSE", 0);
           }
           if (LOWORD(wParam) == 9) {
               MessageBox(NULL, "STOP", "STOP", 0);
           }
	break;

	case WM_DESTROY:
		PostQuitMessage(0);
	break;
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
			LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Edit Control" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0,IDC_ARROW);

  
  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Alarm by RJ"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                220, 220, 285, 290, 0, 0, hInstance, 0);  

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}



Thanks in advance!


P.S.
Please Don't flame/scold me for posting errors, it's my first post, there's bound to be a problem or ten :


EDIT - Forgot to say, it's a countdown alarm - input hour/min/sec and it counts down that input and alarms when time >= 0, NOT a clock timer as in input the day/time to alarm.

EDIT #2 - Code i placed probably won't be updated for a bit, working on the timer engine or whatever you want to call it, GUI will stay pretty much the same.

This post has been edited by Immortal_King: 10 March 2010 - 02:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: CreateWindow input box & timer functioning help

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: CreateWindow input box & timer functioning help

Posted 09 March 2010 - 04:48 PM

Retrieiving the text from an edit control

GetWindowText(HWND hWnd, LPTSTR lpString, int nMaxCount);


For example. This created an edit box then gets the text from it and places it in a buffer.

	// Create input text box 1 
HWND inputEditBox1  =  CreateWindow("Edit", NULL, WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL|ES_AUTOVSCROLL,	82,30,200,20, hwnd, NULL, hInstance_2, 0);

// Allocate space for text buffer
char *text = (char*)malloc(256);	
GetWindowText(inputEditBox1, text, 256);



To set the text, you use the SetWindowText() function instead. Its basically the same, infact, even easier.

This post has been edited by Aphex19: 09 March 2010 - 04:55 PM

Was This Post Helpful? 2
  • +
  • -

#3 besten  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 40
  • Joined: 03-March 10

Re: CreateWindow input box & timer functioning help

Posted 09 March 2010 - 06:39 PM

So cool for a first timer. I bet it was torture to manually write your resources since DEV C++ doesn't come with a resource editor :).
Try http://www.winprog.org
I think this will help you because you will need to go through the
case WM_TIMER 

You will also find more controls over there. Good luck!

This post has been edited by besten: 09 March 2010 - 07:38 PM

Was This Post Helpful? 1
  • +
  • -

#4 Immortal_King  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 10

Re: CreateWindow input box & timer functioning help

Posted 10 March 2010 - 04:49 AM

Thank you both! solved my problem with textboxes, and helped a lot with my timer function with that link, rep++ to both of you as soon as i figure out how :P
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: CreateWindow input box & timer functioning help

Posted 10 March 2010 - 06:25 AM

On a quick note immortal_king, devC++ is depreciated and was discontinued about 4 years ago. Why not move to Visual C++, their is a free version called Visual C++ express. Its the best IDE out their without a doubt.

Anyway
For timing, I either use the timeGetTime() function, like this.
// Link the library we need for the timeGetTime() function
#pragma comment (lib, "WINMM.LIB")

DWORD start = timeGetTime();

for (int i = 0; i < 1000000; i++)
{
}

DWORD end = timeGetTime();
DWORD timeTaken = (end-start);

// timeTaken now equals the time that the loop took in millseconds.
// this is good for measuring things like frame time etc.


or i use this
Here is a good class that I have for timing that basically does the same thing but it uses the windows high performence counter.

Here is the class with a main as an example. ( I usually have the timer class in a seperate file though)

#include <iostream>
#include <windows.h>

using namespace std;

class CTimer
{
public:
	CTimer() 
	{ 
		QueryPerformanceFrequency(&mqFreq); 
	}
	void Start()
	{ 
		QueryPerformanceCounter(&mqStart);
	}
	void End() 
	{ 
		QueryPerformanceCounter(&mqEnd); 
	}
	double GetTimeInSeconds() 
	{ 
		return (mqEnd.QuadPart - mqStart.QuadPart)/(double)mqFreq.QuadPart;
	}
	double GetTimeInMilliseconds() 
	{
		return (1.0e3*(mqEnd.QuadPart - mqStart.QuadPart))/mqFreq.QuadPart;
	}
	double GetTimeInMicroseconds()
	{ 
		return (1.0e6*(mqEnd.QuadPart - mqStart.QuadPart))/mqFreq.QuadPart;
	}
	double GetTimeInNanoseconds()
	{ 
		return (1.0e9*(mqEnd.QuadPart - mqStart.QuadPart))/mqFreq.QuadPart; 
	}

private:
	LARGE_INTEGER mqStart;
	LARGE_INTEGER mqEnd;
	LARGE_INTEGER mqFreq;
};

int main()
{
	CTimer *timer = new CTimer;

	timer->Start();
	
	for (int i = 0; i < 100000000; i++)
	{
	}

	timer->End();

	cout << timer->GetTimeInSeconds() << " secs" << endl;
	cin.get();
	return 0;
}


hope these help you.

This post has been edited by Aphex19: 10 March 2010 - 06:31 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1