Thread: Show dynamic text in windows form

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    30

    Question Show dynamic text in windows form

    Hello.

    I have this c++ console program that works fine and couts some text, however I would like this text to be shown in a windows form.
    The text changes after a while.
    Is this possible to achieve using text labels? and how?
    Im using VC++ 2010 express edition.

    If anyone knows any good tutorial or could explain this it would be appreciated.

    /Daniel

  2. #2
    Registered User blackcoder41's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    14
    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        LPSTR lpCmdLine, int nCmdShow)
    {
        MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
        return 0;
    }

    Tutorial: Getting Started

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    I dont think you got what I meant.

    I need to text to be dynamicly displayed in a windows form not in a messagebox, and I dont see how your example would allow me to even change that messagebox text dynamicly.

  4. #4
    Registered User blackcoder41's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    14
    Well there's so many way to make a form in C++, I don't know how much you knowlege you know and what you are using so.. Maybe MFC, WINAPI, wxWidgets, C++/CLI, etc, etc. Sorry my post didn't help.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    I am using winAPI, but I still need help with this.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well the usual way of displaying window text at the API level is with SetWindowText();

    SetWindowText Function (Windows)

    In a dialog or top level window using the window's handle will change the title bar text.
    In a static or edit control using the control's handle it will change the text content of the control.

  7. #7
    Registered User blackcoder41's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    14
    This is kinda long story, CommonTater already answered it shortly.

    You might want to look here
    Win32 Examples: Net Price Calculation
    Controls
    Standard Controls: Button, Edit, List Box, Static

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    So I read the tutorials you linked to and I think I got it now.
    Thanks.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    30

    Question

    Hi again.
    I finished the turorials but I have one problem.
    I have this code: (with resource file that im not including now)
    Code:
    #include <windows.h>
    #include "resource.h" 
    
    
    
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{
    		case WM_INITDIALOG:
    						
    		break;
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case IDC_Start:
    				{
    				
    					//problem here
                                            int i
    					for(i= 0; i<11; i++)
                                    {
    				SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)"Hello");
    				}	
    					
    				}
    				break;
    				
    			}
    		break;
    		case WM_CLOSE:
    			EndDialog(hwnd, 0);
    		break;
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
    }
    I want my program to do a loop and list some text when I press the start button. simple enough. but I get alot of errors when I write this loop. so I figured I was doing something wrong, but I dont know what. Help would be appreciated.
    Last edited by Danne; 01-11-2011 at 01:59 AM.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Missing syntax (int i (semi-colon)). The IDE usually shows you which line may be causing the problems when compilation fails.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First... as a matter of practice, I find it a bad idea to put code in switch statements. Create a function and call it from the switch. You'll find out why the first time you have to debug a failed winproc... Also you should not leave inactive messages in your winprocs, this can have surprising side effects.

    You missed the ; after declaring the int i for your loop...

    Try the loop in this form...
    Code:
    for ( int i = 0 ; i < 10 ; i++ )

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    30

    Question

    I placed the semicolon in the right place but i still got like 5 "missing ; before type" error by some reason.

    If I use a function how would I include the
    "SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)"Hello");" in it?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Danne View Post
    If I use a function how would I include the
    "SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)"Hello");" in it?
    Like this...
    Code:
    void LoadListBox(HWND Dlg)
      { int i;
        for (i = 0 ; i < 10 ; i++)
          SendDlgItemMessage(Dlg,IDC_LIST,LB_ADDSTRING,0,(LPARAM)"Hello"); }
    
    
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
     { switch(Message)
         { case WM_COMMAND:
             switch(LOWORD(wParam))
               { case IDC_Start:
                   LoadListBox(hwnd);	
                   return 0;
    
                     // other cases here 
    
                 default :
                   return 0; }	
          case WM_CLOSE:
               EndDialog(hwnd, 0);
               return 0;
          default:
              return 0; } }
    
    
    int WINAPI WinMain(HINSTANCE hI, HINSTANCE hP, LPSTR CLine, int nCS)
    { return DialogBox(hI, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc); }
    Tutorials are great for a first start, but hard experience is a better teacher. The reason behind putting as little as possible into switch statements is that if you get the braces tangled up, or end up with a program that opens then immediately closes, you can debug it a lot more easily... Similarly if you have something misbehaving, you can look at your switch, determine which function it's in and work only on the proken function without crashing the rest of your program.
    Last edited by CommonTater; 01-11-2011 at 02:32 AM.

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    30

    Smile

    That works, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows Form App as parent, Directx as child??
    By yetti82 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2006, 03:04 AM
  2. openGL text in multiple windows
    By hannibar in forum Windows Programming
    Replies: 1
    Last Post: 03-02-2006, 02:17 PM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. show a form using code, easy question
    By Rune Hunter in forum C# Programming
    Replies: 1
    Last Post: 04-04-2005, 09:03 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM