Thread: need some help understanding this code win32

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    38

    need some help understanding this code win32

    Basicly I want to get the user to input a directory and file name in another program, then the program saves it.

    I plan to modify this code to suit my needs, however i need someone to help me understand this code better.

    I can't seem to find where string is when the user enter something in and clicks the button. Would be nice if someone could tell me from line 4 to 33.

    Code:
    #include <windows.h>
    #define ID_EDIT 1
    #define ID_BUTTON 2
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
      static HWND hwndEdit;
      static HWND hwndButton;
      static int len;
      static TCHAR text[30];
    
      switch(msg)
      {
        case WM_CREATE:
     hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
        50, 50, 150, 20, hwnd, (HMENU) ID_EDIT,
        NULL, NULL);
     hwndButton = CreateWindow(
      TEXT("button"), TEXT("Set Title"),       
      WS_VISIBLE | WS_CHILD,  
      50, 100, 80, 25,        
      hwnd, (HMENU) ID_BUTTON, NULL, NULL);      
     break;
     case WM_COMMAND: 
               if (HIWORD(wParam) == BN_CLICKED) {
                   len = GetWindowTextLength(hwndEdit) + 1;
                   GetWindowText(hwndEdit, text, len);
                   SetWindowText(hwnd, text);
               }
     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,
                    220, 220, 280, 200, 0, 0, hInstance, 0);  
      while( GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      return (int) msg.wParam;
    }
    Last edited by airesore; 10-17-2011 at 12:22 PM.

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by airesore View Post
    Would be nice if someone could tell me from line 4 to 33.
    Somebody did. Look at the text both above and below this code on the tutorial you got it from.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    ok i read it and was able to save the char array as a string. Now I want this function in my other program. How do i combine this

    Code:
    case WM_CREATE:
     hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
        50, 50, 150, 20, hwnd, (HMENU) ID_EDIT,
        NULL, NULL);
     hwndButton = CreateWindow(
      TEXT("button"), TEXT("ok"),       
      WS_VISIBLE | WS_CHILD,  
      50, 100, 80, 25,        
      hwnd, (HMENU) ID_BUTTON, NULL, NULL);      
     break;
     case WM_COMMAND: 
               if (HIWORD(wParam) == BN_CLICKED) {
                   len = GetWindowTextLength(hwndEdit) + 1;
                   GetWindowText(hwndEdit, text, len);
                   string myString =text;
                  
                   
                //  char *myCharArray = (char*)myString.c_str();
                //  SetWindowText(hwnd, myCharArray);
               }
     break;
    with this

    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <string>
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
         LPSTR lpCmdLine, int nCmdShow )
    {
      MSG  msg ;    
      WNDCLASS wc = {0};
      wc.lpszClassName = TEXT( "Buttons" );
      wc.hInstance     = hInstance ;
      wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
      wc.lpfnWndProc   = WndProc ;
      wc.hCursor       = LoadCursor(0, IDC_ARROW);
      
      RegisterClass(&wc);
      CreateWindow( wc.lpszClassName, TEXT("XMLtoCSV"),
                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                    150, 150, 230, 150, 0, 0, hInstance, 0);  
      while( GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      return (int) msg.wParam;
    }
    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
        
      switch(msg)  
      {
     case WM_CREATE:
     {
            CreateWindow(TEXT("button"), TEXT("XML to CSV"),    
                   WS_VISIBLE | WS_CHILD ,
                   20, 30, 80, 25,        
                   hwnd, (HMENU) 1, NULL, NULL);    
           
            CreateWindow(TEXT("button"), TEXT("Split CSV"),    
                   WS_VISIBLE | WS_CHILD ,
                   20, 70, 80, 25,        
                   hwnd, (HMENU) 2, NULL, NULL); 
    
         CreateWindow(TEXT("button"), TEXT("Quit"),    
                   WS_VISIBLE | WS_CHILD ,
                   120, 50, 80, 25,        
                   hwnd, (HMENU) 3, NULL, NULL);    
         break;
     }
          case WM_COMMAND:
          {
        if (LOWORD(wParam) == 2) {
           system("CSVS.bat");
        }
            if (LOWORD(wParam) == 1) {        
            //codes goes here--------------------------
        }
        if (LOWORD(wParam) == 3) {
                  PostQuitMessage(0);
        }
        break;
           }
          case WM_DESTROY:
          {
             PostQuitMessage(0);
             break;
          }
      }
      return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    I want it to make it so that it has a text box when the person click button one.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Win32 programming goes to the Windows programming board, not the C++ board.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    Win32 programming goes to the Windows programming board, not the C++ board.
    Yeah. Moved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... if I were you, I'd stop trying to "borrow" code... scoop and poop programming almost never gives a satisfactory result...

    Instead of wasting time harvesting bad code from the net... spend the same time and effort to Educate Yourself. Here's a good place to get started...

    theForger's Win32 API Tutorial
    Last edited by CommonTater; 10-18-2011 at 04:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help understanding a Code
    By shiroaisu in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2010, 10:05 AM
  2. I need help understanding this code
    By Sshakey6791 in forum C++ Programming
    Replies: 5
    Last Post: 12-23-2008, 02:49 PM
  3. help me understanding this code
    By apfelsaft in forum C Programming
    Replies: 2
    Last Post: 01-14-2004, 04:46 PM
  4. help understanding code
    By volk in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 10:50 AM
  5. Code Understanding!
    By Jill n Jall in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 04:16 PM