Thread: owner draw...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    owner draw...

    I'm trying to make an owner drawed button, but no luck yet...

    I've been following the msdn example, but it's weird and doesn't work:

    http://msdn.microsoft.com/library/de...ingbuttons.asp


    Can someone give me a working example without using classes?




    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Code:
    CreateWindow( "Button", "My Button", WS_CHILD|WS_VISIBLE, 10, 10, 60, 30, hwndParent, (HMENU)MY_BUTTON_ID, hInstance, NULL );
    
    ...
    case WM_DRAWITEM: {
      
      DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT*)lParam;
      RECT rc;
      GetWindowRect( hwnd, &rc );
      rc.right-=rc.left
      rc.bottom-=rc.top;
      rc.top = rc.left = 0;
      
      HBRUSH red = CreateSolidBrush( RGB( 255,0,0 ) );
      HBRUSH green = CreateSolidBrush( RGB( 0,255,0 ) );
      
      if ( dis->itemAction&ODA_FOCUS && dis->itemStat&ODS_FOCUS ) {
          
          FillRect( dis->hDC, &rc, green );
          
      }else if ( dis->itemAction&ODA_FOCUS ) {
          
          FillRect( dis->hDC, &rc, red );
          
      }
      
      
      SetBkMode( dis->hDC, TRANSPARENT );
      int txtLen = GetWindowTextLength( hwnd );
      char buff[txtLen];
      GetWindowText( hwnd, buff, txtLen);
      DrawText( dis->hDC, buff, txtLen, &rc,  DT_CENTER|DT_VCENTER|DT_SINGLELINE );
      DeleteObject( red );
      DeleteObject( green );
      
      return TRUE;
    }
    ...
    That's a pretty lame example, if it even works... There are a few things that you would not do there, like use GetWindowRect() normally because it supplies a rect but I wasn't sure if it was client or screen so I didn't use it.. That is essentially all you need though.

    I'm not sure if the dc is released for you either, the documentation didn't say. Probably best to do that as well.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    can you please give me an example with bitmap buttons?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    can you please give me an example with bitmap buttons?

    uhmm.... if you know anything about bitmaps, you can take that example and apply bitmaps to it, all you have to do is get a device context,etc. like a bitmap.

  5. #5
    Well, to add a bitmap use BitBlt() ...

    Say you've loaded your bitmap and it's called hBmp.

    Code:
    ...
    HDC tmpDC = CreateCompatibleDC( dc );
    SelectObject( tmpDC, hBmp );
    BitBlt( dc, x1, y1, cx, cy, tmpDC, 0,0, SRCCOPY );
    ...
    x1 and y1 would be where you want the top left point of the bitmap to be, cx and cy are the width and height of the bitmap respectively ...

    http://msdn.microsoft.com/library/de...tmaps_0fzo.asp

  6. #6
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Here's an example of owner-drawn buttons using bitmaps.

    Note: The bitmaps should be in the same directory as the program.

    Edit: You may also notice that the buttons don't respond well to double-clicks. The solution is simple, just subclass the buttons and handle the WM_LBUTTONDBLCLK message by sending a WM_LBUTTONDOWN message.
    Last edited by Dante Shamest; 03-12-2004 at 12:10 PM.

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    got it, thanks
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which is the better way to draw?
    By g4j31a5 in forum Game Programming
    Replies: 16
    Last Post: 01-22-2007, 11:56 PM
  2. draw function HELP!!!
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:28 PM
  3. owner draw?
    By Devil Panther in forum Windows Programming
    Replies: 4
    Last Post: 02-29-2004, 11:08 AM
  4. Owner draw button with bitmap
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 11-30-2003, 03:14 PM
  5. ray casting
    By lambs4 in forum Game Programming
    Replies: 62
    Last Post: 01-09-2003, 06:57 PM