Jump to content


Bs_ownerdraw


9 replies to this topic

#1 Edge_Master

    New Member

  • Members
  • Pip
  • 5 posts

Posted 24 May 2006 - 05:39 PM

Hi everybody. I'm having problems with the use of BS_OWNERDRAW buttons. I put the image in the button without problems but i don't know how to change the image when the button is pressed. I have tried with the DRAWITEMSTRUCT that the window receive as the lParam parameter of the WM_DRAWITEM message, but i don't know when the button is down so if somebody could help me i will thankful.
Sorry for my English, i will be thankful too if somebody corrects my errors.

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 24 May 2006 - 06:10 PM

You should check the itemState member of the DRAWITEMSTRUCT, as documented here. You may have to do some experimenting to find out which states are actually used by the button, as the documentation isn't very clear on that.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 Edge_Master

    New Member

  • Members
  • Pip
  • 5 posts

Posted 24 May 2006 - 07:31 PM

I have tried too many times with itemState and itemAction but i can't deal with the solution.I can't identify when the button have been pressed or realized.
This is the last code i tried, but i tried with other ones.

void Pintar(HWND hwnd) // THIS FUNCTION IS CALLED IN WM_PAINT

{

			BITMAP bm;

			PAINTSTRUCT ps;

			HDC hdc;

			hdc = BeginPaint(hwnd, &ps);

			HDC hdcMem = CreateCompatibleDC(hdc);//-/-/-/-

			SelectObject(hdcMem, Fondo);

			GetObject(Fondo, sizeof(bm), &bm);

			BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

			EndPaint(hwnd, &ps);

			hdc=BeginPaint(BtnSalir, &ps);

			hdcMem = CreateCompatibleDC(hdc);

			SelectObject(hdcMem, Cerrar);

			GetObject(Cerrar, sizeof(bm), &bm);

			BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

			EndPaint(BtnSalir, &ps);

			DeleteDC(hdcMem);


}


//THIS FUNCTION IS CALLED IN WM_DRAWITEM

void CambiarImagen(HWND hwnd, LPDRAWITEMSTRUCT Draw)

{	

			BITMAP bm;

			PAINTSTRUCT ps;

			HDC hdc;

			HDC hdcMem;



		if(Draw->itemState & ODS_SELECTED)

		{

			hdc=BeginPaint(BtnSalir, &ps);

			hdcMem = CreateCompatibleDC(hdc);

			SelectObject(hdcMem, Cerrar1);

			GetObject(Cerrar1, sizeof(bm), &bm);

			BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

		}


			

			EndPaint(BtnSalir, &ps);

			DeleteDC(hdcMem);

}

When i call "Pintar" it paint a bitmap in the background and paint a bitmap in the button "cerrar", but when WM_DRAWITEM call "CambiarImagen" it does´t change the bitmap of button "cerrar".
Sorry for my English again.

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 24 May 2006 - 09:08 PM

How about when the button is clicked, you pop up a message box showing what the value of itemState is. That should help you clue in to the right flag to use.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 Edge_Master

    New Member

  • Members
  • Pip
  • 5 posts

Posted 25 May 2006 - 03:33 AM

ok. I add this code in WM_DRAWITEM
	    char state[10];

		itoa(Draw->itemState,&state[0],10);

		SetWindowText(txtCerrar,state);

When the button is pressed the text says 17 and when the button is realized the text says 16. But if i do this:

void CambiarImagen(HWND hwnd, LPDRAWITEMSTRUCT Draw)

{	

			BITMAP bm;

			PAINTSTRUCT ps;

			HDC hdc;

			HDC hdcMem;


		char state[3];

		itoa(Draw->itemState,&state[0],10);


		if(atoi(&state[0])== 16)

		{

			hdc=BeginPaint(BtnSalir, &ps);

			hdcMem = CreateCompatibleDC(hdc);

			SelectObject(hdcMem, Cerrar1);

			GetObject(Cerrar1, sizeof(bm), &bm);

			BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

			MessageBox(hwnd,"HI",state,MB_OK);



		}

			EndPaint(BtnSalir, &ps);

			DeleteDC(hdcMem);


}

The messagebox appear when i press de button not when i realized it, really I do not understand.
I'm not sure if in English you say "realize" when you stop pushing down a button.
:wallbash:

#6 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 25 May 2006 - 07:02 AM

I think the word you are looking for is "released" the button. Anyway, I'm afraid I can't help you further...you should try googling for some more articles on owner-draw controls.
reedbeta.com - developer blog, OpenGL demos, and other projects

#7 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 25 May 2006 - 12:50 PM

The itemState you are interested in is ODS_SELECTED. It has a value of 0x01. The reason you are getting 16 and 17 is because the control has focus and ODS_FOCUS is 0x10.
ODS_FOCUS == 16
ODS_FOCUS | ODS_SELECTED == 17
Since you don't care if the control has focus, use the following code to determine if your button is pressed:
void OnDrawItem(HWND hwnd, LPDRAWITEMSTRUCT lpDrawItem)
{
    if (lpDrawItem->itemState & ODS_SELECTED) {
        // if you get here, the button is pressed
    }
    else {
        // otherwise, it is not pressed (released)
    }
}

monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#8 Edge_Master

    New Member

  • Members
  • Pip
  • 5 posts

Posted 25 May 2006 - 08:20 PM

I googled very much, but all the articles that i found are for MFC.

i already try ODS_SELECTED but probably this code only work for MFC.
I try with this code:

void CambiarImagen(HWND hwnd, LPDRAWITEMSTRUCT Draw)

{	

			BITMAP bm;

			PAINTSTRUCT ps;

			HDC hdc;

			HDC hdcMem;


		char state[3];

		itoa(Draw->itemState,&state[0],10);


	

		if(Draw->itemState & ODS_SELECTED)

		{

			hdc=BeginPaint(BtnSalir, &ps);

			hdcMem = CreateCompatibleDC(hdc);

			SelectObject(hdcMem, Cerrar1);

			GetObject(Cerrar1, sizeof(bm), &bm);

			BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

		}

		else

		{

			MessageBox(hwnd,state,"hola",MB_OK);


		}

			EndPaint(BtnSalir, &ps);

			DeleteDC(hdcMem);

}

The message appear also when the button is down.
I am going to keep on trying.
:wallbash: :wallbash:

#9 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 25 May 2006 - 11:50 PM

MFC is just a thin wrapper over the Windows API, so any code that works in MFC can be translated. If there's a specific article you're referring to, can you post a link to it?
reedbeta.com - developer blog, OpenGL demos, and other projects

#10 Edge_Master

    New Member

  • Members
  • Pip
  • 5 posts

Posted 26 May 2006 - 05:56 AM

Thanks for your help, i found the solution, the problem was that i don't used the HDC that PAINTSTRUCT has. I change this:

void CambiarImagen(HWND hwnd, LPDRAWITEMSTRUCT Draw)

{	

			BITMAP bm;

			HDC hdcMem;


		char state[3];

		itoa(Draw->itemState,&state[0],10);


		if(Draw->itemState & ODS_SELECTED)

		{


			hdcMem = CreateCompatibleDC(Draw->hDC);

			SelectObject(hdcMem, Cerrar1);

			GetObject(Cerrar1, sizeof(bm), &bm);

			BitBlt(Draw->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

		}

		else

		{

			hdcMem = CreateCompatibleDC(Draw->hDC);

			SelectObject(hdcMem, Cerrar);

			GetObject(Cerrar, sizeof(bm), &bm);

			BitBlt(Draw->hDC, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);


		}

			DeleteDC(hdcMem);


}

It doesn't look important but i really want that somebody correct my mistakes because don't have another way to talk better. Thank you again, bye.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users