Solved

button image problem

Posted on 2001-06-09
12
217 Views
Last Modified: 2008-02-01
Guys, I have a serious problem.
I have VC5 and Iam trying to use buttons using CreateWindow() to make them without MFC. I set a new window procedure using SetWindowLong() function. in that window procedure i draw an image onto the button  using a resource file. But for some reason it also draws the same bitmap onto my other button. Can you help me please???
0
Comment
Question by:Professor
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6173458
As I understand, you make button and after this
use ownerdraw mode for Display something.
Next is small example, that makes ~ same (without using rc file).May be it helps you:

#include <windows.h>

#define IDC_SMALLER      1
#define IDC_LARGER       2

#define BTN_WIDTH        (8 * cxChar)
#define BTN_HEIGHT       (4 * cyChar)

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

HINSTANCE hInst ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                PSTR szCmdLine, int iCmdShow)
     {
     static char szAppName[] = "OwnerDrw" ;
     MSG         msg ;
     HWND        hwnd ;
     WNDCLASSEX  wndclass ;

     hInst = hInstance ;

       wndclass.cbSize        = sizeof (wndclass) ;
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = szAppName ;
     wndclass.lpszClassName = szAppName ;
       wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

     RegisterClassEx (&wndclass) ;

     hwnd = CreateWindow (szAppName, "Owner-Draw Button Demo",
                          WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
          }
     return msg.wParam ;
     }

void Triangle (HDC hdc, POINT pt[])
     {
     SelectObject (hdc, GetStockObject (BLACK_BRUSH)) ;
     Polygon (hdc, pt, 3) ;
     SelectObject (hdc, GetStockObject (WHITE_BRUSH)) ;
     }

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     {
     static HWND      hwndSmaller, hwndLarger ;
     static int       cxClient, cyClient, cxChar, cyChar ;
     int              cx, cy ;
     LPDRAWITEMSTRUCT pdis ;
     POINT            pt[3] ;
     RECT             rc ;

     switch (iMsg)
          {
          case WM_CREATE :
               cxChar = LOWORD (GetDialogBaseUnits ()) ;
               cyChar = HIWORD (GetDialogBaseUnits ()) ;

                         // Create the owner-draw pushbuttons

               hwndSmaller = CreateWindow ("button", "",
                                  WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
                                  0, 0, BTN_WIDTH, BTN_HEIGHT,
                                  hwnd, (HMENU) IDC_SMALLER, hInst, NULL) ;

               hwndLarger = CreateWindow ("button", "",
                                  WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
                                  0, 0, BTN_WIDTH, BTN_HEIGHT,
                                  hwnd, (HMENU) IDC_LARGER, hInst, NULL) ;
               return 0 ;

          case WM_SIZE :
               cxClient = LOWORD (lParam) ;
               cyClient = HIWORD (lParam) ;

                         // Move the buttons to the new center

               MoveWindow (hwndSmaller, cxClient / 2 - 3 * BTN_WIDTH  / 2,
                                        cyClient / 2 -     BTN_HEIGHT / 2,
                                        BTN_WIDTH, BTN_HEIGHT, TRUE) ;

               MoveWindow (hwndLarger,  cxClient / 2 +     BTN_WIDTH  / 2,
                                        cyClient / 2 -     BTN_HEIGHT / 2,
                                        BTN_WIDTH, BTN_HEIGHT, TRUE) ;
               return 0 ;

          case WM_COMMAND :
               GetWindowRect (hwnd, &rc) ;

                         // Make the window 10% smaller or larger

               switch (wParam)
                    {
                    case IDC_SMALLER :
                         rc.left   += cxClient / 20 ;
                         rc.right  -= cxClient / 20 ;
                         rc.top    += cyClient / 20 ;
                         rc.bottom -= cyClient / 20 ;

                         break ;

                    case IDC_LARGER :
                         rc.left   -= cxClient / 20 ;
                         rc.right  += cxClient / 20 ;
                         rc.top    -= cyClient / 20 ;
                         rc.bottom += cyClient / 20 ;

                         break ;
                    }

               MoveWindow (hwnd, rc.left, rc.top, rc.right  - rc.left,
                                                  rc.bottom - rc.top, TRUE) ;
               return 0 ;

          case WM_DRAWITEM :
               pdis = (LPDRAWITEMSTRUCT) lParam ;

                         // Fill area with white and frame it black

               FillRect (pdis->hDC, &pdis->rcItem,
                         (HBRUSH) GetStockObject (WHITE_BRUSH)) ;

               FrameRect (pdis->hDC, &pdis->rcItem,
                          (HBRUSH) GetStockObject (BLACK_BRUSH)) ;

                         // Draw inward and outward black triangles

               cx = pdis->rcItem.right  - pdis->rcItem.left ;
               cy = pdis->rcItem.bottom - pdis->rcItem.top  ;

               switch (pdis->CtlID)
                    {
                    case IDC_SMALLER :
                         pt[0].x = 3 * cx / 8 ;  pt[0].y = 1 * cy / 8 ;
                         pt[1].x = 5 * cx / 8 ;  pt[1].y = 1 * cy / 8 ;
                         pt[2].x = 4 * cx / 8 ;  pt[2].y = 3 * cy / 8 ;

                         Triangle (pdis->hDC, pt) ;

                         pt[0].x = 7 * cx / 8 ;  pt[0].y = 3 * cy / 8 ;
                         pt[1].x = 7 * cx / 8 ;  pt[1].y = 5 * cy / 8 ;
                         pt[2].x = 5 * cx / 8 ;  pt[2].y = 4 * cy / 8 ;

                         Triangle (pdis->hDC, pt) ;

                         pt[0].x = 5 * cx / 8 ;  pt[0].y = 7 * cy / 8 ;
                         pt[1].x = 3 * cx / 8 ;  pt[1].y = 7 * cy / 8 ;
                         pt[2].x = 4 * cx / 8 ;  pt[2].y = 5 * cy / 8 ;

                         Triangle (pdis->hDC, pt) ;

                         pt[0].x = 1 * cx / 8 ;  pt[0].y = 5 * cy / 8 ;
                         pt[1].x = 1 * cx / 8 ;  pt[1].y = 3 * cy / 8 ;
                         pt[2].x = 3 * cx / 8 ;  pt[2].y = 4 * cy / 8 ;

                         Triangle (pdis->hDC, pt) ;

                         break ;

                    case IDC_LARGER :

                         pt[0].x = 5 * cx / 8 ;  pt[0].y = 3 * cy / 8 ;
                         pt[1].x = 3 * cx / 8 ;  pt[1].y = 3 * cy / 8 ;
                         pt[2].x = 4 * cx / 8 ;  pt[2].y = 1 * cy / 8 ;

                         Triangle (pdis->hDC, pt) ;

                         pt[0].x = 5 * cx / 8 ;  pt[0].y = 5 * cy / 8 ;
                         pt[1].x = 5 * cx / 8 ;  pt[1].y = 3 * cy / 8 ;
                         pt[2].x = 7 * cx / 8 ;  pt[2].y = 4 * cy / 8 ;

                         Triangle (pdis->hDC, pt) ;

                         pt[0].x = 3 * cx / 8 ;  pt[0].y = 5 * cy / 8 ;
                         pt[1].x = 5 * cx / 8 ;  pt[1].y = 5 * cy / 8 ;
                         pt[2].x = 4 * cx / 8 ;  pt[2].y = 7 * cy / 8 ;

                         Triangle (pdis->hDC, pt) ;

                         pt[0].x = 3 * cx / 8 ;  pt[0].y = 3 * cy / 8 ;
                         pt[1].x = 3 * cx / 8 ;  pt[1].y = 5 * cy / 8 ;
                         pt[2].x = 1 * cx / 8 ;  pt[2].y = 4 * cy / 8 ;

                         Triangle (pdis->hDC, pt) ;

                         break ;
                    }

                         // Invert the rectangle if the button is selected

               if (pdis->itemState & ODS_SELECTED)
                    InvertRect (pdis->hDC, &pdis->rcItem) ;

                         // Draw a focus rectangle if the button has the focus

               if (pdis->itemState & ODS_FOCUS)
                    {
                    pdis->rcItem.left   += cx / 16 ;
                    pdis->rcItem.top    += cy / 16 ;
                    pdis->rcItem.right  -= cx / 16 ;
                    pdis->rcItem.bottom -= cy / 16 ;

                    DrawFocusRect (pdis->hDC, &pdis->rcItem) ;
                    }

               return 0 ;

          case WM_DESTROY :
               PostQuitMessage (0) ;
               return 0 ;
          }
     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
     }
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6175496
The simplest solution is to add the BS_BITMAP style when you create the window, and then send it a BM_SETIMAGE message:

HWND hwndBmp= CreateWindow("BUTTON",..... WS_CHILD|BS_BITMAP,...);

HBITMAP hBmp= LoadBitmap( hInst, MAKEINTRESOURCE(IDB_BITMAP1) );

SendMessage( hwndBtn, BM_SETIMAGE, IMAGE_BITMAP, hbmpTheBitmap);

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6307076
for Professor,
Do you have any additional questions?  Do any comments need clarifiacation?

-- Dan
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6318531
Professor, I don't know, helps my comment or not, but
you don't react and minimum I 've sent you working
code. Alex
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6401476
This question was LOCKED with a PROPOSED ANSWER and awaits your decision today.  Once a question is LOCKED with a Proposed Answer, few new experts will step in to help on that question, since the assumption is, you've been helped.  If the Proposed Answer helped you, please accept it and award that expert.  If it did not help you, please reject it and add comments as to status and what else is needed.

If you wish to award multiple experts, just comment here with detail, I'll respond as soon as possible.  As it stands today, you asked the question, got help and not one expert was awarded for the contribution(s) made.  Your response is needed.  I'll monitor through month end, and if you've not returned to complete this, we'll need to decide.  Expert input is welcome (as always) to determine the outcome here if the Asker does not respond.

Your response in finalizing this (and ALL) your question(s) is appreciated.

Site-related HELP:  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
Moondancer
Community Support Moderator @ Experts Exchange
0
 

Author Comment

by:Professor
ID: 6401799
Well, I cant get how that stuff worx...

Thanx anywayz
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:Moondancer
ID: 6401866
Hi, Professor, just comment here as to what you'd like done with this question.  If an expert or more than one helped you here, advise detail and I'll help split points or whatever you need.  

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6402713
With which part of my simple solution do you need help? Do you need to know how to set a style flag for a button?  To you need to know how to load a bitmap resource?  

If you really want to solve your problem, you will need to communicate.  Also, you will find that posting some of your code will often get things moving.  When experts have a concrete example, they can get results faster.

-- Dan
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 6443887
hi Professor,
Do you have any additional questions?  Do any comments need clarifiacation?

-- Dan
0
 

Author Comment

by:Professor
ID: 6444580
Sorry, I will try the code again with VC 6 instead of the Borland C.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6678042
hi Professor,
Do you have any additional questions?  Do any comments need clarifiacation?

-- Dan
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6703243
Comment from DanRollins accepted as answer.

Thank you
Computer101
Community Support Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now