Thread: srand()

  1. #31
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CB_GETLBTEXT is a combobox message but you are using edit controls, as you should be for something like this.

    Generally, messages prefixed CB_ are combobox messages, EM_ are edit control messages, EN_ are edit control notifications etc.

    To retrieve the text from an edit control use, GetWindowText; you can precede it with a call to GetWindowTextLength to get the length of the edit control's contents., should you need to allocate suitable memory for it. You can set the maximum edit control's text length by sending that edit control and EM_SETLIMITTEXT message; if this is a constant then you can use the same constant to allocate TCHAR arrays as required.

    Some other suggestions:
    • it might arguably be better to use static controls for labels rather than relying on TextOut in a WM_PAINT handler as you have done.
    • An edit control with the ES_NUMBER window style accepts only numeric input - you can use GetDlgItemInt
      to retrieve its contents.
    • You seem to be mixing char and TCHAR - consistency is advisable. Accordingly, it would be better if you were to apply the TEXT or _T macro to all string literals. You should also include tchar.h to get access to string mapping macros which resolve to wide or narrow string manipulation functions depending on whether _UNICODE and UNICODE are #defined. Alternatively, use api functions that do that anyway, like wsprintf.
    • You seem to mixing stl and non-stl strings - pick one and stick to it. If you choose to use stl strings, it might be a good idea to typedef a string alias based on std::basic_string<TCHAR>; such an alias will essentially resolve to std::string or std::wstring depending on the #defintions of UNICODE and _UNICODE.
    • You mgiht want to give those edit control handles variable names that reflect their function rather than their current names which imply they are a completely different kind of control.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  2. #32
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    What's the difference between TCHAR and char?

  3. #33
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If UNICODE and _UNICODE are #defined TCHAR is wchar_t, otherwise it's char.

    edit:ie(not exactly but essentially):
    Code:
    #if defined UNICODE
    typedef wchar_t TCHAR;
    #else
    typedef char TCHAR;
    #endif
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #34
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    So your saying it should look more like this?
    Code:
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    
    
    HINSTANCE g_hInst;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    					LPSTR lpCmdLine, int nCmdShow )
    {
    	MSG Msg;
    	HWND hwnd;
    	WNDCLASSEX wincl;
    	g_hInst=hInstance;
    	TCHAR chClassName[] = TEXT( "RaffleWindow" );
    
    	wincl.cbClsExtra=0;
    	wincl.cbWndExtra=0;
    	wincl.style=CS_HREDRAW|CS_VREDRAW;
    	wincl.lpszMenuName=NULL;
    	wincl.hInstance=hInstance;
    	wincl.lpszClassName=chClassName;
    	wincl.cbSize=sizeof(WNDCLASSEX);
    	wincl.lpfnWndProc=(WNDPROC)WndProc;
    	wincl.hCursor=LoadCursor(NULL,IDC_ARROW);
    	wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    	wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
    	wincl.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    
    	if(!RegisterClassEx(&wincl))
    	{
    		MessageBox(0,"Window Registration Failed!","Error!",MB_ICONSTOP|MB_OK);
    		return 0;
    	}
    	hwnd=CreateWindowEx(0,chClassName,"Dark_Ride Raffle",WS_OVERLAPPEDWINDOW,
    	CW_USEDEFAULT,CW_USEDEFAULT,380,205,HWND_DESKTOP,NULL,
    	hInstance,NULL);
    	if(hwnd==NULL)
    	{
    		MessageBox(0,"Window Creation Failed!","Error",MB_ICONSTOP|MB_OK);
    		return 0;
    	}
    	ShowWindow(hwnd,nCmdShow);
    	UpdateWindow(hwnd);
    	while(GetMessage(&Msg,NULL,0,0))
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    }
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	HDC hdc;
    	PAINTSTRUCT ps;
    	char UserName[32];
    	char Message[64], UserData[41];
    
    	int	POST_NumOfTickets=0, PRE_NumOfTickets=0, index, Length, NumOfTickets;
    
    	HWND hSubmit, hAnswer, hPerson, hTicket;
    	LPSTR Greeting, Question, Person, Tickets;
    	Person = "Person:";
    	Tickets = "Tickets:";
    	Greeting = "Well here it is, thanks for putting me through hell!";
    	Question = "Add person and number of tickets";
    
    	std::ofstream a_file("People.ini",std::ios::app);
    
    	switch(msg)
    	{
    		case WM_CREATE:
    			hPerson = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", NULL,
    						WS_CHILD|WS_VISIBLE|CBS_HASSTRINGS, 75, 75, 200, 25,
    						hwnd, (HMENU)1, g_hInst, NULL);
    
    			hTicket = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", NULL,
    						WS_CHILD|WS_VISIBLE|ES_NUMBER, 75, 100, 75, 25,
    						hwnd, (HMENU)2, g_hInst, NULL);
    
    
    			hSubmit = CreateWindowEx(NULL,"Button","Submit",
    						WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 275, 75, 75, 25,
    						hwnd, (HMENU)3, g_hInst, NULL);
    
    			hAnswer = CreateWindowEx (NULL, "Button", "And the lucky winner is",
    						WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 150, 100, 200, 25,
    						hwnd, (HMENU)4, g_hInst, NULL);
    		break;
    
    		case WM_COMMAND:
    			if((LOWORD(wParam) == 1) && (HIWORD(wParam) == CBN_SELCHANGE))
    			{
    				index=SendMessage(hPerson, CB_GETCURSEL, 0, 0);
    				if(index == CB_ERR)
    				{
    					return TRUE;
    				}
    			}
    			if(LOWORD(wParam)==3)
    			{
    				SendMessage(hPerson,EM_SETLIMITTEXT,256,0);
    
    				Length=SendMessage(hPerson,EM_GETLIMITTEXT,0,0);
    
    				GetWindowText(hPerson,UserName,Length);
    
    				GetDlgItemInt(hTicket,NumOfTickets,NULL,FALSE);
    
    
    				Length=sprintf(UserData,"%s %i\n", UserName, NumOfTickets);
    
    				a_file<<UserData;
    				MessageBox(0,"User entered, no error checking was done.","",0);
    			}
    
    			if((LOWORD(wParam) == 4)&& (HIWORD(wParam) == CBN_SELCHANGE))
    			{
    				a_file<<"EOF";
    				a_file.close();
    
    				std::string Winner;
    				std::ifstream b_file("People.ini");
    			}
    		break;
    
    		case WM_PAINT:
    			hdc=BeginPaint(hwnd, &ps);
    			TextOut(hdc, 25, 25, Greeting, strlen(Greeting));
    			TextOut(hdc, 25, 50, Question, strlen(Question));
    			TextOut(hdc, 25, 75, Person, strlen(Person));
    			TextOut(hdc, 25, 100, Tickets, strlen(Tickets));
    			EndPaint(hwnd, &ps);
    		break;
    
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    		break;
    
    		case WM_DESTROY:
    			PostQuitMessage (0);
    		break;
    
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    The only problem is that it still writes to the file wrong and how can I impliment the use of static controls instead of WM_PAINT

  5. #35
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You're still using combobox messages with edit controls, which won't do what you expect it to, if anything.

    Use CreateWindowEx to create static controls; give them the SS_SIMPLE style.

    For what you seem to be trying to do you set the limits to the edit controls just after they are created. The following, modified code should do just about what you seemed to be after; be sure to read up any api functions and messages you are unsure of on msdn.
    Code:
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    
    //control identifiers
    enum 
      {
      IDC_LABEL=400,
      IDC_PERSON,
      IDC_TICKET,
      IDC_SUBMIT,
      IDC_ANSWER
      };
    
    const int MAX_EDIT_TEXT=256;
    const int MAX_DIGITS=5;
    
    BOOL CALLBACK ChangeFonts(HWND,LPARAM);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    void OnCommand(const HWND,const HWND,const int,const int);
    int OnCreate(const HWND,CREATESTRUCT*);
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
              LPSTR lpCmdLine, int nCmdShow )
    {
      
      WNDCLASSEX wincl={0};
    
      char chClassName[] = "RaffleWindow";
    
      wincl.hInstance=hInstance;
      wincl.lpszClassName=chClassName;
      wincl.cbSize=sizeof(WNDCLASSEX);
      wincl.lpfnWndProc=(WNDPROC)WndProc;
      wincl.hCursor=LoadCursor(NULL,IDC_ARROW);
      wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);
      wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
      wincl.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
    
      if(!RegisterClassEx(&wincl))
      {
        MessageBox(0,"Window Registration Failed!","Error!",MB_ICONSTOP|MB_OK);
        return 0;
      }
      HWND hwnd=CreateWindowEx(0,chClassName,"Dark_Ride Raffle",
        WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX, //no point in resizing it
      CW_USEDEFAULT,CW_USEDEFAULT,380,205,HWND_DESKTOP,NULL,
      hInstance,NULL);
      if(hwnd==NULL)
      {
        MessageBox(0,"Window Creation Failed!","Error",MB_ICONSTOP|MB_OK);
        return 0;
      }
      ShowWindow(hwnd,nCmdShow);
      UpdateWindow(hwnd);
      
      MSG Msg;
      while(GetMessage(&Msg,0,0,0)>0)
      {
        if(!IsDialogMessage(hwnd,&Msg))
        {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
        }
      }
      return static_cast<int>(Msg.wParam);
    }
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
      switch(msg)
      {
        case WM_CREATE:
          return OnCreate(hwnd,reinterpret_cast<CREATESTRUCT*>(lParam));
        case WM_COMMAND:
          OnCommand(hwnd,reinterpret_cast<HWND>(lParam),LOWORD(wParam),
                    HIWORD(wParam));
          return 0;
        case WM_DESTROY:
          PostQuitMessage (0);
          return 0;
        default:
          return DefWindowProc(hwnd, msg, wParam, lParam);
      }
    }
    
    void OnCommand(const HWND hwnd,const HWND hCntrl,const int id,const int msg)
    {
    if (id==IDCANCEL)
      {
      //escape key pressed, just quit
      DestroyWindow(hwnd);
      }
    if (hCntrl)
      {
      if (msg==BN_CLICKED && id==IDC_SUBMIT)
        {
        char person[MAX_EDIT_TEXT];
        int len=GetWindowText(GetDlgItem(hwnd,IDC_PERSON),person,MAX_EDIT_TEXT);
        if (!len)
          {
          MessageBox(0,"Please enter a name","Error",MB_OK|MB_ICONEXCLAMATION);
          return;
          }
        int count=GetDlgItemInt(hwnd,IDC_TICKET,0,0);
        if (!count)
          {
          MessageBox(0,"Please enter a number","Error",MB_OK|MB_ICONEXCLAMATION);
          return;
          }
        char buffer[8];
        wsprintf(buffer,"%d",count);
        
        std::ofstream file("people.ini",std::ios::app);
        file<<person<<'\t'<<count<<std::endl;
        }
      }
    }
    
    int OnCreate(const HWND hwnd,CREATESTRUCT *cs)
    {
    //create some labels
    CreateWindowEx(0,"static",
                   "Well here it is, thanks for putting me through hell!",
                   WS_CHILD|WS_VISIBLE|SS_SIMPLE,
                   25,25,340,25,
                   hwnd,
                   reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_LABEL)),
                   cs->hInstance,0);
    CreateWindowEx(0,"static",
                   "Add person and number of tickets",
                   WS_CHILD|WS_VISIBLE|SS_SIMPLE,
                   25,50,340,25,
                   hwnd,
                   reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_LABEL)),
                   cs->hInstance,0);
    CreateWindowEx(0,"static",
                   "Person:",
                   WS_CHILD|WS_VISIBLE|SS_SIMPLE,
                   25,75,340,25,
                   hwnd,
                   reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_LABEL)),
                   cs->hInstance,0);  
    CreateWindowEx(0,"static",
                   "Tickets:",
                   WS_CHILD|WS_VISIBLE|SS_SIMPLE,
                   25,100,340,25,
                   hwnd,
                   reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_LABEL)),
                   cs->hInstance,0);      
                   
    HWND hPerson=CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 0,
      WS_CHILD|WS_VISIBLE|WS_TABSTOP, 
      75, 75, 200, 25,
      hwnd,
      reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_PERSON)), 
      cs->hInstance, 0);
    
    SendMessage(hPerson,EM_SETLIMITTEXT,MAX_EDIT_TEXT,0);
    
    HWND hTicket=CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", 0,
      WS_CHILD|WS_VISIBLE|ES_NUMBER|WS_TABSTOP, 
      75, 100, 75, 25,
      hwnd, 
      reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_TICKET)),
      cs->hInstance, 0);
                
    SendMessage(hTicket,EM_SETLIMITTEXT,MAX_DIGITS,0);
          
    CreateWindowEx(0,"Button","Submit",
                   WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_TABSTOP, 
                   275, 75, 75, 25,
                   hwnd, reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_SUBMIT)),
                   cs->hInstance, 0);
    
    CreateWindowEx (0, "Button", "And the lucky winner is",
                    WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_TABSTOP, 
                    150, 100, 200, 25,
                    hwnd, reinterpret_cast<HMENU>(static_cast<INT_PTR>(IDC_ANSWER)),
                    cs->hInstance, 0);
                
    EnumChildWindows(hwnd,ChangeFonts,0); //change the font to typical control font
    
    SetFocus(hPerson);            
    return 0;            
    }
    
    BOOL CALLBACK ChangeFonts(HWND hwnd,LPARAM lParam)
    {
    //EnumChildProc
    SendMessage(hwnd,WM_SETFONT,
                reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)),
                MAKELPARAM(1,0));
    return TRUE;
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #36
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Woah, that code is alot different from mine.... I need to read up on classes now I guess. How would I go about setting the "And the lucky winner is" button to do something. And where do I put that code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use srand() twice in a function?
    By Jake.c in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 12:51 PM
  2. srand() in .Net
    By Sad Programmer in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2003, 05:01 PM
  3. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM
  4. When to srand()?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 12:20 AM
  5. srand()... possible reasons for failure
    By lightatdawn in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2001, 02:33 AM