3 Replies - 1122 Views - Last Post: 07 August 2010 - 09:21 PM Rate Topic: -----

#1 rommi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-August 10

Toggle the color of a button by clicking

Posted 05 August 2010 - 12:34 AM

Hi all,
i wrote this code to to toggle the color of a button alway when i click on the button. Now i have two questions. If i start to click on the button the color dont change on the first two clicks. After two clicks the color change then well. My second question is if i do some clicks very fast in a row the button dont react on every click. It seems the button is realy slow and not so quick as a "normal" Button (for example the buttons from the windows calculator calc.exe) is!? Does anybody why is this and how i can change this?
#include <windows.h>
#include <stdio.h>

const char MainClassName[] = "MainWindowClass";

// the windows
HWND hWndButton;
HWND hWnd;

// a trigger to toggle
int trigg = 2;

// for drawing
COLORREF bckcolor1 = RGB(255, 0, 0);
HBRUSH hBrushUp,hBrushDown;

// the button text
char text1 [2] = "F";
char text2 [2] = "X";
char text3 [2] = "F";


//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//																		XXXXX
//			own functions												XXXXX
//																		XXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
int DrawItem(UINT wParam,LPARAM lParam)
{
	LPDRAWITEMSTRUCT lpdis = (DRAWITEMSTRUCT*)lParam;

	//XXX Color for Text and Textbackground
	SetTextColor(lpdis->hDC, RGB(0, 0, 0));
	SetBkColor(lpdis->hDC, bckcolor1);

	//XXX Text out
	ExtTextOut(lpdis->hDC,10,1,ETO_OPAQUE|ETO_CLIPPED,&lpdis->rcItem,text1,strlen("D"),NULL);

	//XXX draw edge sunken and raise
	DrawEdge(lpdis->hDC, &lpdis->rcItem,(lpdis->itemState & ODS_SELECTED ? EDGE_SUNKEN : EDGE_RAISED ), BF_RECT);
return 1;
}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//																		XXXXX
//			Callback Procedure											XXXXX
//																		XXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
LRESULT CALLBACK WndProc( HWND hWnd,UINT Msg,WPARAM  wParam,LPARAM  lParam )
{
     switch (Msg)
     {
		//_________________________________________
		case WM_CREATE:
        {
            hWndButton = CreateWindowEx(0,"BUTTON",NULL,WS_CHILD | BS_OWNERDRAW,15,15,30,20,hWnd,
			(HMENU)99,(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),NULL);
            ShowWindow(hWndButton, SW_SHOW);
        }
        break;
		//_________________________________________
		case WM_DRAWITEM:
			{
				DrawItem(wParam,lParam);
				break;
			}
		//_________________________________________
        case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
                case 99:
                {
                    switch(HIWORD(wParam))
                    {
                        case BN_CLICKED:

							if(trigg == 1)
							{
								bckcolor1 = RGB(255, 0, 0);
								strcpy(text1, text2);
								trigg = 2;
							}
							else
							{
								bckcolor1 = RGB(150, 150, 150);
								strcpy(text1, text3);
								trigg = 1;
							}
                        break;
                    }
                }
                break;
            }
            return 0;
        }
        break;
		//_________________________________________
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
		//_________________________________________
        default:
        return (DefWindowProc(hWnd, Msg, wParam, lParam));
    }

    return 0;
}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//																		XXXXX
//			Main														XXXXX
//																		XXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
INT WINAPI WinMain( HINSTANCE  hInstance,HINSTANCE  hPrevInstance,LPSTR lpCmdLine,INT nCmdShow )
{
    WNDCLASSEX wc;
    MSG wmsg;

    wc.cbSize           = sizeof(WNDCLASSEX);
    wc.style            = 0;
    wc.lpfnWndProc      = WndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground	= CreateSolidBrush(RGB(0, 128, 0));
	wc.lpszMenuName		= MainClassName;
    //wc.lpszMenuName     = MAKEINTRESOURCE(IDR_MENU1);	// wichtig um das Menü zu laden
    wc.lpszClassName    = MainClassName;
	wc.hIconSm			= LoadIcon(GetModuleHandle(NULL), IDI_APPLICATION);

    if(!RegisterClassEx(&wc)){return 0;}

    hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,MainClassName,"Color Buttons",WS_OVERLAPPEDWINDOW,
							CW_USEDEFAULT,CW_USEDEFAULT,240,120,NULL,NULL,hInstance, NULL);

    if (!hWnd){return 0;}
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    while (GetMessage(&wmsg, NULL, 0, 0))
    {
        TranslateMessage(&wmsg);
        DispatchMessage(&wmsg);
    }

    return wmsg.wParam;
}



Thanks a lot for helping, regards rommi

Is This A Good Question/Topic? 0
  • +

Replies To: Toggle the color of a button by clicking

#2 rommi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-August 10

Re: Toggle the color of a button by clicking

Posted 06 August 2010 - 05:07 AM

Hi all, no hint regarding the problm? The standard window buttons dont have this problems, only if i use Drawitem and so i guess this is the reason. Ok will try it in ohter forums may be i find there an answer. So this problem is resovled and the admin can close this thread!
bye, rommi
Was This Post Helpful? 0
  • +
  • -

#3 rommi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-August 10

Re: Toggle the color of a button by clicking

Posted 07 August 2010 - 05:27 PM

Hi all experts,

i guess i have to protect this super super expert forum with my solution cause i got no lousi small answer on such a simple question..
Ok i dont want to make it to easy for you experts so i will give you only a part of the solution hehe. Here are my hints. "s..le", "cl..s", "d.l". Uhhh thats enough now! If you want the quite solution all experts have to pay for the solution. Only novice user get the answer without any cost from me! And here some good forum too without any costs:

http://stackoverflow.com
http://forum.fachinformatiker.de german forum
http://cplusplus.com
http://www.tutorials.de german forum

byebye, rommi
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Toggle the color of a button by clicking

Posted 07 August 2010 - 09:21 PM

We are glad you figured it out on your own. ;)

That's the first step to becoming a true programmer, learning to solve problems on your own.

On a more serious note, Win32 API is not a common topic and therefore there is a lower knowledge base. Be Patient, sooner or later someone with the proper knowledge will see your topic.

This post has been edited by eker676: 07 August 2010 - 09:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1