Menu

Painting a child window

Eric Davis
2009-01-11
2012-09-26
  • Eric Davis

    Eric Davis - 2009-01-11

    I'm trying to paint the background ofa child window but haven't been able to manage it. Does anybody know how this is accomplished?

    include <windows.h>

    / Declare Windows procedure /
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

    / Make the class name into a global variable /
    char szClassName[ ] = "WindowsApp";

    HWND hwndButton3; //Window handle to button
    void Horses(HWND);//Horse text function

    int WINAPI WinMain (HINSTANCE hThisInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszArgument,
    int nFunsterStil)

    {
    HWND hwnd; / This is the handle for our window /
    MSG messages; / Here messages to the application are saved /
    WNDCLASSEX wincl; / Data structure for the windowclass /

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);
    
    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&amp;wincl))
        return 0;
    
    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           &quot;Windows App&quot;,       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
    
    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);
    

    ///////////////Create a button//////////////////////////
    hwndButton3 = CreateWindow(
    "BUTTON", // Predefined class; Unicode assumed.
    "Test", // Button text.
    WS_VISIBLE | WS_CHILD, // Styles.
    10, // x position.
    10, // y position.
    95, // Button width.
    85, // Button height.
    hwnd, // Parent window.
    NULL, // No menu.
    (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
    NULL); // Pointer not needed.
    ////////////////////////////////////////////////////////

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&amp;messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&amp;messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&amp;messages);
    }
    
    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
    

    }

    / This function is called by the Windows function DispatchMessage() /
    HWND aChildWindow;

    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message) / handle the messages /
    {
    case WM_CREATE:
    /////////////////////Create a child window//////////////////////
    int test;
    if(test == 0)
    {

         aChildWindow = CreateWindowEx (
           WS_EX_CLIENTEDGE,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           NULL,       /* Title Text */
           WS_VISIBLE | WS_CHILD, /* default window */
           10,       /* Windows decides the position */
           100,       /* where the window ends up on the screen */
           200,                 /* The window width */
           200,                 /* and height in pixels */
          hwnd,        /* The window is a child-window to main window */
           NULL,                /* No menu */
           (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
           test = 1;
    
           }
     break;
    

    ///////////////////////////////////////////////////////////////////

     case WM_COMMAND:   
        if ((HWND)lParam == hwndButton3 &amp;&amp; HIWORD(wParam) == BN_CLICKED)
             {
             Horses(aChildWindow);
             return 0;
             }
             break;
    
        //case WM_ERASEBKGND:
        //break;
    
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    
    return 0;
    

    }

    RECT ClientArea;
    int x, y;
    int count;
    void Horses(HWND htowind)
    {
    HDC Hndltodc;
    char str[] = "horses";
    if( count==0)
    {
    x = 10;
    y = 10;
    }
    Hndltodc = GetDC(htowind);

     TextOut(
     Hndltodc,           // handle to DC
     x,       // x-coordinate of starting position
     y,       // y-coordinate of starting position
     str,  // character string
     6       // number of characters
     );
    
     x=x+50;
     //y=y+50;
     count = count + 1;
    
     GetClientRect(
     htowind,
     &amp;ClientArea
     );
    
     if(x &gt; (int)ClientArea.right - 50)
     {
     y=y+20;
     x=10;
     }
    
     ReleaseDC(
     htowind,  // handle to window
     Hndltodc     // handle to DC
     );
        return;
    

    }

     
    • Eric Davis

      Eric Davis - 2009-01-12

      I have it! I had to move a few things to get it to work.... Having my child window under WM_CREATE was producing some very strange results when I tried to paint to it.

      //////////////////////////////////////////////////////////////////////

      include <windows.h>

      / Declare Windows procedure /
      LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

      / Make the class name into a global variable /
      char szClassName[ ] = "WindowsApp";
      HWND aChildWindow;
      HWND hwndButton3; //Window handle to button
      void Horses(HWND);//Horse text function

      int WINAPI WinMain (HINSTANCE hThisInstance,
      HINSTANCE hPrevInstance,
      LPSTR lpszArgument,
      int nFunsterStil)

      {
      HWND hwnd; / This is the handle for our window /
      MSG messages; / Here messages to the application are saved /
      WNDCLASSEX wincl; / Data structure for the windowclass /

      /* The Window structure */
      wincl.hInstance = hThisInstance;
      wincl.lpszClassName = szClassName;
      wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
      wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
      wincl.cbSize = sizeof (WNDCLASSEX);
      
      /* Use default icon and mouse-pointer */
      wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
      wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
      wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
      wincl.lpszMenuName = NULL;                 /* No menu */
      wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
      wincl.cbWndExtra = 0;                      /* structure or the window instance */
      /* Use Windows's default color as the background of the window */
      wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
      
      /* Register the window class, and if it fails quit the program */
      if (!RegisterClassEx (&amp;wincl))
          return 0;
      
      /* The class is registered, let's create the program*/
      hwnd = CreateWindowEx (
             0,                   /* Extended possibilites for variation */
             szClassName,         /* Classname */
             &quot;Windows App&quot;,       /* Title Text */
             WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, /* default window */
             CW_USEDEFAULT,       /* Windows decides the position */
             CW_USEDEFAULT,       /* where the window ends up on the screen */
             544,                 /* The programs width */
             375,                 /* and height in pixels */
             HWND_DESKTOP,        /* The window is a child-window to desktop */
             NULL,                /* No menu */
             hThisInstance,       /* Program Instance handler */
             NULL                 /* No Window Creation data */
             );
      
      /* Make the window visible on the screen */
      ShowWindow (hwnd, nFunsterStil);
      

      ///////////////Create a button//////////////////////////
      hwndButton3 = CreateWindow(
      "BUTTON", // Predefined class; Unicode assumed.
      "Test", // Button text.
      WS_VISIBLE | WS_CHILD, // Styles.
      10, // x position.
      10, // y position.
      95, // Button width.
      85, // Button height.
      hwnd, // Parent window.
      NULL, // No menu.
      (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
      NULL); // Pointer not needed.
      ////////////////////////////////////////////////////////

      //////////Having my child window under WM_CREATE caused a lot of /////////problems

       aChildWindow = CreateWindowEx (
             WS_EX_CLIENTEDGE,                   /* Extended possibilites for variation */
             szClassName,         /* Classname */
             NULL,       /* Title Text */
             WS_VISIBLE | WS_CHILD, /* default window */
             10,       /* Place window on */
             100,       /* parent*/
             200,                 /* The window width */
             200,                 /* and height in pixels */
            hwnd,        /* The window is a child-window to main window */
             NULL,                /* No menu */
             (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),       /* Program Instance handler */
             NULL                 /* No Window Creation data */
             );
      

      ////////////////////////////////////////////////////////////////////////////////////////

      /* Run the message loop. It will run until GetMessage() returns 0 */
      while (GetMessage (&amp;messages, NULL, 0, 0))
      {
          /* Translate virtual-key messages into character messages */
          TranslateMessage(&amp;messages);
          /* Send message to WindowProcedure */
          DispatchMessage(&amp;messages);
      }
      
      /* The program return-value is 0 - The value that PostQuitMessage() gave */
      return messages.wParam;
      

      }

      / This function is called by the Windows function DispatchMessage() /

      LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
      {
      switch (message) / handle the messages /
      {
      case WM_CREATE:

              break;
      
       case WM_COMMAND:   
          if ((HWND)lParam == hwndButton3 &amp;&amp; HIWORD(wParam) == BN_CLICKED)
               {
               Horses(aChildWindow);
               return 0;
               }
               break;
      
       case WM_PAINT:
      
       HBRUSH      hBrush ;
       HDC         hdc ;
       PAINTSTRUCT ps ;
       RECT        rc ;
      
            hdc = BeginPaint (aChildWindow, &amp;ps) ;
      
            GetClientRect (aChildWindow, &amp;rc) ;
      
            hBrush = CreateSolidBrush (RGB(255,255,255));
      
            FillRect (hdc, &amp;rc, hBrush) ;
      
            EndPaint (aChildWindow, &amp;ps) ;
            DeleteObject (hBrush) ;
      
            UpdateWindow(
            hwndButton3  // handle to window
            );
            break;
      
          case WM_DESTROY:
              PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
              break;
          default:                      /* for messages that we don't deal with */
              return DefWindowProc (hwnd, message, wParam, lParam);
      }
      
      return 0;
      

      }

      RECT ClientArea;
      int x, y;
      int count;
      void Horses(HWND htowind)
      {
      HDC Hndltodc;
      char str[] = "horses";
      if( count==0)
      {
      x = 10;
      y = 10;
      }
      Hndltodc = GetDC(htowind);

       TextOut(
       Hndltodc,           // handle to DC
       x,       // x-coordinate of starting position
       y,       // y-coordinate of starting position
       str,  // character string
       6       // number of characters
       );
      
       x=x+50;
       //y=y+50;
       count = count + 1;
      
       GetClientRect(
       htowind,
       &amp;ClientArea
       );
      
       if(x &gt; (int)ClientArea.right - 50)
       {
       y=y+20;
       x=10;
       }
      
       ReleaseDC(
       htowind,  // handle to window
       Hndltodc     // handle to DC
       );
          return;
      

      }

       
    • Eric Davis

      Eric Davis - 2009-01-12

      Now I can sleep tonight. Cool...

      ////////////////////////////////////////////////////////////////////////

       case WM_PAINT:
      
            PaintWindBckgnd(aChildWindow, 255, 255, 255);
            PaintWindBckgnd(hwnd, 0, 0, 0);
      
            UpdateWindow(
            hwndButton3  // handle to window
            );
            break;
      
       
    • Musa

      Musa - 2009-01-11

      Whats wrong with it, except it doesn't compile? After a simple fix it compiles and works.
      what exactly do you want to achieve.
      see
      http://i150.photobucket.com/albums/s106/toatingbucket/untitled.jpg

       
    • Eric Davis

      Eric Davis - 2009-01-11

      I want to paint the smaller child window in which the text appears. I am trying to make it white. I am learning the API and trying understand how it works.

       
    • Eric Davis

      Eric Davis - 2009-01-12

      BTW: It compiles just fine on my machine. I'm using Dev.

       
    • Musa

      Musa - 2009-01-12

      Where are you trying to paint it white? I don't see it.
      use fillrect http://msdn.microsoft.com/en-us/library/ms533283(VS.85).aspx
      checkout http://msdn.microsoft.com/en-us/library/ms533223(VS.85).aspx for brush info

       
    • Eric Davis

      Eric Davis - 2009-01-12

      Hmm... It gets even better. I've built a function that makes it even easier.

      //////////////////////////////////////////////////////////

      void PaintWindBckgnd(HWND hwnd, int r, int g, int b)
      {
      HBRUSH hBrush ;
      HDC hdc ;
      PAINTSTRUCT ps ;
      RECT rc ;

            hdc = BeginPaint (hwnd, &amp;ps) ;
      
            GetClientRect (hwnd, &amp;rc) ;
      
            hBrush = CreateSolidBrush (RGB(r,g,b));
      
            FillRect (hdc, &amp;rc, hBrush) ;
      
            EndPaint (hwnd, &amp;ps) ;
            DeleteObject (hBrush) ;
      
            return;
            }
      
       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.