Thread: new functions in a win32 application

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    new functions in a win32 application

    Hey everybody, I made a new function in a win32 app but it doesn't send stuff to the message loop. If someone has some advice on this please help.
    (sorry if it sounds like I have homework... I don't I just want to know how to sent messages to the message loop).

    example

    Code:
    original stuff up here
    case WM_OPEN: // case value
         open();
    
    message loop down here
    
    open();
    {
    ...  // cannot send to message loop.
    
    }
    I hope this isn't to vague...
    Thanks in advance.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You can send messages to your application through the SendMessage and the PostMessage functions, i don't understand what you're trying to do though...
    Devoted my life to programming...

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I had a ton of loops in my case values so I just started making functions to put them in

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    And the problem is.......
    Devoted my life to programming...

  5. #5
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I was getting wierd errors so I just put everything in a new function. And I need to send messages to the message loop, because the function creates new controls.

  6. #6
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    actually the listbox isn't being created even if my code isn't in another function... here is the code


    Code:
     case ID_OPEN :{
               
                static HWND hwndlistbox;
        HWND hwnd;
        char buff1[50];
        char *current;
        int num1 = 1;
        FILE *pfile;
        
        DestroyWindow(hwndEdit2);
                        DestroyWindow(hwndEdit3);
                        DestroyWindow(hwndEdit4);
                        DestroyWindow(hwndStatic);
                        DestroyWindow(hwndStatic2);
                        DestroyWindow(hwndStatic3);
                        DestroyWindow(hwndStatic4);
                        DestroyWindow(hwndStatic5);
                        DestroyWindow(hwndButton);	
    	
        hwndlistbox = CreateWindow(TEXT("Listbox"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL,
    				0, 0, 265, 305, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);
    				
    				 pfile = fopen("C:\\reminder\\data\\backup2.xls","r");
        
        while (fgets(buff1, 50, pfile))
    {
        char *current = buff1;
        while (*current) 
        {
            if (num1 != 3 && *current == ',')
            {
                SendMessage(hwndlistbox, LB_ADDSTRING, 0, (LPARAM)"");
                ++num1;
            }
            else
            {
                SendMessage(hwndlistbox, LB_ADDSTRING, 0, (LPARAM)current);
            }
            ++current;
    }
    }
          
          }
          
          break;

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    First add LBS_STANDARD to your listbox styles. Second, you don't have a ShowWindow after your listbox creation. There might be more problems, but thats 2 I see.
    Last edited by DaveH; 07-13-2010 at 07:10 AM.

  8. #8
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    Halp

    how would I use the ShowWindow, what should I put in the parameters.
    and here is the code. I have tried just about everything.
    (The listbox is in the case ID_open: section. and thanks for your help.)

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <commctrl.h>
    
    #define ID_EDIT 1
    #define ID_BUTTON 2
    #define ID_STATIC 3
    #define     ID_NEW       1000
    #define     ID_OPEN      1004
    #define     ID_CLOSE     1001
    #define     ID_EXIT      1002
    
    
    
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
       
        
        
        char *current;
        
      
      
      int num1 = 1;
     char buff1;
      FILE *pfile;
      FILE *pfile2;
      static HWND hwndlistbox;
      static HWND hwndEdit;
      static HWND hwndEdit2;
      static HWND hwndEdit3;
      static HWND hwndEdit4;
      static HWND hwndButton;
      static HWND hwndStatic;
      static HWND hwndStatic2;
      static HWND hwndStatic3;
      static HWND hwndStatic4;
      static HWND hwndStatic5;
    
      static int len;
      static CHAR text[1000];
      static CHAR text2[1000];
      static CHAR text3[1000];
      static CHAR text4[1000];
      CREATESTRUCT            *cs;
       HMENU                   hMenu, hSubMenu, hSubMenu2;
       char                    szText[64];
       // Menu-item identifiers 
       static   char           szMenuItem[6][10] = {"Open", "Close", "Exit", "New"};
    
      switch(msg)
      {
        case WM_CREATE:
    	
    	cs = (LPCREATESTRUCT)lParam;
          hMenu = CreateMenu();
          SetMenu(hwnd, hMenu);
    
          hSubMenu = CreatePopupMenu();
          AppendMenu(hSubMenu, MF_STRING, ID_OPEN, "&Open");
          AppendMenu(hSubMenu, MF_STRING, ID_NEW, "&New");
          AppendMenu(hSubMenu, MF_STRING, ID_CLOSE, "&Close");
          AppendMenu(hSubMenu, MF_STRING, ID_EXIT, "&Exit");
          
          InsertMenu(hMenu, 0, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu, "File");
          
          
    
          DrawMenuBar(hwnd);
        
        break;
    
    	case WM_COMMAND:	     
             
              
                  switch(LOWORD(wParam))
          {
          
          
          
          case ID_OPEN :
               
                static HWND hwndlistbox;
        HWND hwnd;
        char buff1[50];
        char *current;
        int num1;
        num1 = 1;
        FILE *pfile;
        
                        printf("open");
                        DestroyWindow(hwndEdit2);
                        DestroyWindow(hwndEdit);
                        DestroyWindow(hwndEdit3);
                        DestroyWindow(hwndEdit4);
                        DestroyWindow(hwndStatic);
                        DestroyWindow(hwndStatic2);
                        DestroyWindow(hwndStatic3);
                        DestroyWindow(hwndStatic4);
                        DestroyWindow(hwndStatic5);
                        DestroyWindow(hwndButton);	
         printf("creating");	
       hwndlistbox = CreateWindow(TEXT("LISTBOX"),"",WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_STANDARD,
             50,50,50,50,hwnd,(HMENU) ID_EDIT,
             NULL,NULL);
    				printf("created");
    				 pfile = fopen("C:\\reminder\\data\\backup2.xls","r");
        
        while (fgets(buff1, 50, pfile))
    {
        char *current = buff1;
        while (*current) 
        {
            if (num1 != 3 && *current == ',')
            {
                SendMessage(hwndlistbox, LB_ADDSTRING, 0, (LPARAM)"");
                ++num1;
            }
            else
            {
                SendMessage(hwndlistbox, LB_ADDSTRING, 0, (LPARAM)current);
            }
            ++current;
    }
    }
          
          
          
          break;
          
          
          case ID_NEW:{
               hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
    				50, 70, 170, 140, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);
    
    	hwndEdit2 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
    				50, 30, 20, 20, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);
    				
    	hwndEdit3 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
    				120, 30, 20, 20, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);
                    
        hwndEdit4 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
    				190, 30, 40, 20, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);  
                    
        hwndStatic = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
    				1, 1, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL);  
        
        hwndStatic2 = CreateWindow(TEXT("static"), "/", WS_CHILD | WS_VISIBLE,
    				1, 5, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL); 
                                                      			
        hwndStatic3 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
    				1, 3, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL); 
    				
    	hwndStatic4 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
    				1, 4, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL); 			
    				
    				
    	hwndStatic5 = CreateWindow(TEXT("static"), "", WS_CHILD | WS_VISIBLE,
    				20, 220, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL);		
    				
        hwndButton = CreateWindow(
    		TEXT("button"), TEXT("set title and write to file"),       
    		WS_VISIBLE | WS_CHILD,  
    		30, 240, 200, 25,        
    		hwnd, (HMENU) ID_BUTTON, NULL, NULL);                
    				
    	DestroyWindow(hwndlistbox);				
          
          
          }
          break;
          
          
          case ID_CLOSE :
          case ID_EXIT :
             
             sprintf(szText, "You chose %s", szMenuItem[LOWORD(wParam) - 1000]);
             MessageBox(hwnd, szText, "MenuApp", MB_OK);
             break;
          }
          break;
                
               if (HIWORD(wParam) == BN_CLICKED) {
                   pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "r");
                   if(pfile2 == NULL){
                              system("mkdir C:\\reminder\\data");
                             pfile = fopen("C:reminder\\data\\backup1.xls", "a");
                             }
                   pfile = fopen("database1234.xls", "r");
                   printf("break1");
                   if(pfile == NULL){
                   
                   MessageBox(hwnd,"the database for the program was deleted or moved, we will now use the backup,\neverything you enter now will be put into the original file\n and the backup\n the program will try to copy the backup.\n(the program will take care of everything)","database not found",MB_OK);
                   
                  /* pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "r");
                    pfile = fopen("database1234.xls",)
                    while( (buff1 = fgetc(pfile))!= EOF)
                    {
                           fprintf()
                           
                           }
                   */
                   if(pfile2 == NULL){
                   printf("breakloop5");
                   system("mkdir C:\\reminder\\data");
                   printf("breakloop6");
                   pfile = fopen("C:reminder\\data\\backup1.xls", "a");}
                            printf("breakloop");
                            }
                          else{
                          printf("breakloop7");
                          fclose(pfile);
                          printf("breakloop8");
                          pfile = fopen("database1234.xls", "a");
                          printf("breakloop9");
                        }
                   fclose(pfile);
                  printf("break10");
                  fclose(pfile2);
                  printf("break11");
                  pfile = fopen("database1234.xls", "a");
                  printf("break12");
                  pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "a"); 
                   printf("break13");
                   SetWindowText(hwndStatic5,("Getting text..."));
                   len = GetWindowTextLength(hwndEdit) + 1;
                   GetWindowText(hwndEdit, text, len);
                   GetWindowText(hwndEdit2, text2, len);
                   GetWindowText(hwndEdit3, text3, len);
                   GetWindowText(hwndEdit4, text4, len);
                   //SetWindowText(hwnd, text);
                   printf("break14");
                   SetWindowText(hwndStatic5,("writing to file..."));
                   fprintf(pfile,"%s,%s,%s,¥\"¥%s¥\"¥\n",text2,text3,text4,text); 
                   printf("break15");
                   fprintf(pfile2,"%s,%s,%s,¥\"¥%s¥\"¥\n",text2,text3,text4,text);
                   printf("break16");
                   //MessageBox(hwnd,text,"text",MB_OK);
                  printf("break17");
                  fclose(pfile);
                  printf("break18");
                  fclose(pfile2);
               SetWindowText(hwndStatic5,("done")); 	
               SetWindowText(hwndEdit,(""));
               SetWindowText(hwndEdit2,(""));
               SetWindowText(hwndEdit3,(""));
               SetWindowText(hwndEdit4,("")); 
               
               printf("break19");
               }
    	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("Edit control"),
                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                    1000, 420, 280, 350, 0, 0, hInstance, 0);  
    
      while( GetMessage(&msg, NULL, 0, 0)) {
        
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      return (int) msg.wParam;
    }
    Thanks again!

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    In your WinMain, you should be checking the return of CreateWindow. After this CreateWindow there should be a ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd);

    In your ID_OPEN case, enclose it in parentheses. Set the hwndlistbox = NULL. Again check the CreateWindow return value. You are just assuming it is created. It might not be. If the returned handle is valid, do ShowWindow(hwndlistbox, SW_SHOWNORMAL); In ID_OPEN there is an unused HWND hwnd variable.

    You need to check returns for all your other CreateWindow calls and add ShowWindows for each of them.
    Last edited by DaveH; 07-13-2010 at 11:29 AM.

  10. #10
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    It was the HWND hwnd variable, thanks for all of your help.

  11. #11
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    ok, now that button click at the bottom isn't working
    the:

    Code:
    if (HIWORD(wParam) == BN_CLICKED) {}
    sorry i am dragging this on

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Code:
    if (HIWORD(wParam) == BN_CLICKED) {}
    This can not be reached due to the placement of 'break'. It has no 'case' statement (ie case ID_SOME_CONTROL: is missing)


    Also, you have 2 declarations of the listboxes HWND, which may be causing issues (as the one that gets a valid HWND has limited scope.


    You also appear to be doing this the hard way, creating and destroying the controls (instead of just show/hide the controls as required ie ShowWindow(hWndEdit, SW_HIDE) ).

    Normally to do this kind of thing you create another dialog to hold all the edits and Hide/Show this dialog as required (ie draw the dialog in the resource editor, create the dlg once in the apps Create msg, show/hide the whole dlg in response to a command msg, destroy the whole dlg on apps close msg)

    EDIT: Have a look at GetLastError() Is very handy to debug these type of things.
    Last edited by novacain; 07-13-2010 at 08:13 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  13. #13
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I read your code again....

    I think you should break your code down into smaller blocks (that do one thing well and can be reused). It would help you see the flow of your code (as it is repeating operations and failing to do other ones) and avoid what appears to be some cut/paste errors.

    Some issues I see...

    You can not have 4 edits with the same ID (they need to be different if on the same dlg).

    Quote Originally Posted by MSDN CreateWindow() hmenu param
    For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.

    This is what your code does...

    Code:
    		//open pfile2 as C:\\reminder\\data\\backup1.xls
    		//if pfile2 failed to open 
    			//open/create pfile as C:\\reminder\\data\\backup1.xls 
    			//pfile2 == null
    
    		//open pfile as database1234.xls (on a possibly already used pfile)
    		//if database1234.xls failed to open
    			//if C:\\reminder\\data\\backup1.xls failed to open
    				//open pFile as C:reminder\\data\\backup1.xls (which may fail again leaving pfile null)
    				//pfile2 is null
    			//else pfile is null
    		//if database1234.xls opened
    			//close it and open in append mode
    
    		//close both files (up to 5 fopens for no reason....)
    
    		//open both files in append mode (what if you have read only permission or the file is already open or fails again etc? [will data be lost if this happens?])
    		
    		//get entered text from edits
    
    		//write to file
    		//close files
    		//clear edits
    This code could fail; getting no text or crash as text len exceeds buffer size.

    Code:
    len = GetWindowTextLength(hwndEdit) + 1;
    //what happens if len is now bigger than 1000 (what text can hold) or zero (no text entered or error)?
    
    //get the text from edit(1)
    GetWindowText(hwndEdit, text, len);//the 3rd parameter should be the size of the char array (ie 1000)
    
    GetWindowText(hwndEdit2, text2, len);//you are using the same len, from edit(1) for all edits, if it is zero....

    What you want is...
    Code:
    Validate
                Check user has entered text in all req edits
                       validate text if required
                               prompt user for more input if req
                Check files can be opened in append mode
                       warn user and abort if fail
    
    Get input
    
    Write input to file
    
    Clean up
             Close files
             Clear edits
    Last edited by novacain; 07-13-2010 at 11:59 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  14. #14
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I tried fixing the button but it still doesnt work. This is what i did.

    Code:
    hwndButton = CreateWindow(
    		TEXT("button"), TEXT("set title and write to file"),       
    		WS_VISIBLE | WS_CHILD,  
    		30, 240, 200, 25,        
    		hwnd, (HMENU) ID_BUTTON1, NULL, NULL); 
    
    a little father down...
    
              
              if(HIWORD(wParam) == BN_CLICKED)
    {
      switch(LOWORD(wParam))
      {
        case ID_BUTTON1://I #defined this above.
          MessageBox(hwnd,"Message box","Title",MB_OK);
          break;
      }
    }
    And thanks for helping me fine tune everything... I am polishing everything right now... then I will probably get another idea ... but hey thats what programming is for.

  15. #15
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Please post the whole callback, as I do not think you have the main switch statement correct (you appear to be switching on the ID twice).

    Does hwndButton get a value or does CreateWindow() fail (you do not test)?

    Again, you are doing this the hard way, which may not work due to the timing of msgs (also you do not init your variables, re-init them when free'd or test the values are valid).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. First time Creating Win32 application with UI
    By chiefmonkey in forum Windows Programming
    Replies: 9
    Last Post: 09-23-2009, 11:44 AM
  3. Replies: 6
    Last Post: 05-15-2007, 10:47 PM
  4. Painting with Tex on a Win32 Application
    By webzest in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2004, 03:04 PM
  5. Can I Load A Sound or Music Into My Win32 Application ?
    By SonicWave in forum Windows Programming
    Replies: 8
    Last Post: 09-21-2001, 07:54 AM