CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Calling WM_PAINT or its equivalent with a Button Control

    In this pseudo code I am trying to display a selected bitmap by clicking on a button...

    Code:
    case WM_COMMAND:
            {
                switch(LOWORD(wParam))
                {
    
                    case IDC_BTN_ROLL:
                    {
                        case WM_PAINT:
                        {
    
                               if(condition...)
                               hDC = BeginPaint(hwndDlg, &Ps);
    
                               bmpFull = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
    
                               MemDCFull = CreateCompatibleDC(hDC);
    
                               SelectObject(MemDCFull, bmpFull);
    
                               BitBlt(hDC, 50, 50, 112, 112, MemDCFull, 0, 0, SRCCOPY);
    
                               DeleteDC(MemDCFull);
                               DeleteObject(bmpFull);
                               EndPaint(hwndDlg, &Ps);
    
                               return TRUE;
                    }
    
                }
    
            }
    I am pretty sure this cannot function that way. Would WM_PRINT or WM_PRINTCLIENT be of more use? They are mentioned as alternatives to WM_PAINT in the following quote from MSDN:
    "The WM_PAINT message is generated by the system and should not be sent by an application. To force a window to draw into a specific device context, use the WM_PRINT or WM_PRINTCLIENT message. Note that this requires the target window to support the WM_PRINTCLIENT message. Most common controls support the WM_PRINTCLIENT message."
    ANy tips or more realistic code examples are welcome...

  2. #2
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: Calling WM_PAINT or its equivalent with a Button Control

    Maybe I am asking the wrong question...

    How do I get a Button Control to trigger a bitmap to be displayed in a dialog?

    I can display a bitmap but only in a default type way, which means I cannot figure out how to make a bitmap appear following a mouse click on a Button in a specified location - or be dynamic so to speak.

    So any tips on what Message to send or the configuration of the case structure would be really excellent.

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Calling WM_PAINT or its equivalent with a Button Control

    what is the "case WM_PAINT:" doing in your button handler? It should be a separate case on the same level as WM_COMMAND. What you need to do is ONLY paint in the WM_PAINT handler (the correct one). Use a flag that can be set in your button handler that tells your WM_PAINT routine what bitmap to paint, then call Invalidate() to force a repaint.

  4. #4
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: Calling WM_PAINT or its equivalent with a Button Control

    hoxsiew...

    I know my example is incorrect - I posted it like that to show the problem I was trying to convey, not how I expected it to actually work...

    BUT - I think I have a better idea of how to work through it from your suggestion of using a flag in the button handler.

    If you have the time, could you please elaborate on how to implement the flag? Possibly with some example code?

    Thanks for your reply BTW

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Calling WM_PAINT or its equivalent with a Button Control

    Well, using your code above, try something like this:

    Code:
    
    static int flag=0;
    
    //...
    
    switch(msg){
      case WM_COMMAND:  
        switch(LOWORD(wParam)){
          case IDC_BTN_ROLL:
            flag=1;
            InvalidateRect(hwnd, NULL, TRUE); 
            return TRUE;
        }
      case WM_PAINT:
        hDC = BeginPaint(hwndDlg, &Ps);
        if(flag){
          bmpFull = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
        }else{
          bmpFull = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP2));
        }
        MemDCFull = CreateCompatibleDC(hDC);
        SelectObject(MemDCFull, bmpFull);
        BitBlt(hDC, 50, 50, 112, 112, MemDCFull, 0, 0, SRCCOPY);
        DeleteDC(MemDCFull);
        DeleteObject(bmpFull);
        EndPaint(hwndDlg, &Ps);
        return TRUE;
    }

  6. #6
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: Calling WM_PAINT or its equivalent with a Button Control

    Thanks hoxsiew,

    Now that I have a really good idea on how to load different bitmaps, the next matter is how can I load bitmaps to multiple locations following the code you supplied?

    edit:
    I tried this and it seems to be a start:
    Code:
                if(flag)
                BitBlt(hDC, 50, 50, 112, 112, MemDCFull, 0, 0, SRCCOPY);
    
                else
                BitBlt(hDC, 80, 50, 112, 112, MemDCFull, 0, 0, SRCCOPY);
    what do you think?
    Last edited by Morbane; November 19th, 2010 at 11:48 PM.

  7. #7
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: Calling WM_PAINT or its equivalent with a Button Control

    IS it possible, using these examples, to have multiple bitmaps in multiple locations, all able to be changed?

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Calling WM_PAINT or its equivalent with a Button Control

    Sorry, but the answer is pretty apparent as for me: everything's possible in case you follow design principles and programming instructions. Though not sure about "using these examples" as long as there's absolutely nothing special in the last hoxsiew's snippet, therefore there's no any ground for doubts.

    PS. It's very important to understand what lines make an elementary template for a single bitmap to apply this approach to multiple region updates.
    PPS. Let's remember that any engineering solution is just a combination of elementary tasks put together proper way, at proper time, in proper order.
    Last edited by Igor Vartanov; November 20th, 2010 at 03:27 PM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured