I want one of my buttons to toggle between On and Off, i mean when i click on On it must turn Off when i click on Off it must turn on, how can i do that?
I want one of my buttons to toggle between On and Off, i mean when i click on On it must turn Off when i click on Off it must turn on, how can i do that?
Maybe state which OS/Compiler/GUI Toolkit you're using?
Or maybe post the code you have to draw the button in the first place.
http://www.catb.org/~esr/faqs/smart-questions.html
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Dev C++, Windows
I want it to toggle between "Durdur" and "Calistir"
Code:#include <windows.h> LRESULT CALLBACK PencereProseduru (HWND, UINT, WPARAM, LPARAM); char YapiSinifAdi[ ] = "WindowsApp"; HWND DugmeTutmaci; HMENU Yavru1; int WINAPI WinMain (HINSTANCE AnaFonksiyonTutmaci, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND AnaPencereTutmaci; MSG Mesajlar; WNDCLASSEX PencereYapisi; PencereYapisi.hInstance = AnaFonksiyonTutmaci; PencereYapisi.lpszClassName = YapiSinifAdi; PencereYapisi.lpfnWndProc = PencereProseduru; PencereYapisi.style = CS_DBLCLKS; PencereYapisi.cbSize = sizeof (WNDCLASSEX); PencereYapisi.hIcon = LoadIcon (NULL, IDI_APPLICATION); PencereYapisi.hIconSm = LoadIcon (NULL, IDI_APPLICATION); PencereYapisi.hCursor = LoadCursor (NULL, IDC_ARROW); PencereYapisi.lpszMenuName = NULL; PencereYapisi.cbClsExtra = 0; PencereYapisi.cbWndExtra = 0; PencereYapisi.hbrBackground = (HBRUSH) COLOR_BACKGROUND; if (!RegisterClassEx (&PencereYapisi)) return 0; AnaPencereTutmaci = CreateWindowEx ( 0, YapiSinifAdi, "Windows App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, AnaFonksiyonTutmaci, NULL ); DugmeTutmaci = CreateWindowEx ( 0, "button", "Durdur", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 1, 1, 170, 30, AnaPencereTutmaci, (HMENU)1, AnaFonksiyonTutmaci, NULL ); ShowWindow (AnaPencereTutmaci, nFunsterStil); while (GetMessage (&Mesajlar, NULL, 0, 0)) { TranslateMessage(&Mesajlar); DispatchMessage(&Mesajlar); } return Mesajlar.wParam; } LRESULT CALLBACK PencereProseduru (HWND AnaPencereTutmaci, UINT message, WPARAM wParam, LPARAM lParam) { LPSTR Icerik; switch (message) { case WM_COMMAND: if(LOWORD(wParam) == 1 && HIWORD(wParam) == BN_CLICKED) { GetWindowText(DugmeTutmaci, Icerik, sizeof(Icerik)+1); if (Icerik == "Durdur") {SetWindowText (DugmeTutmaci,"Calistir"); break;} else {SetWindowText (DugmeTutmaci, "Durdur"); break;} } break; case WM_DESTROY: PostQuitMessage (0); break; default: return DefWindowProc (AnaPencereTutmaci, message, wParam, lParam); } return 0; }
Last edited by hileci555; 07-10-2008 at 01:17 PM. Reason: added code
> LPSTR Icerik;
This needs to be a STR (specifically an array of them), not merely an uninitialised pointer.
> GetWindowText(DugmeTutmaci, Icerik, sizeof(Icerik)+1);
Be honest about the size of things you're referring to. Adding 1 is unlikely to be a good idea.
> if (Icerik == "Durdur")
Use strcmp() to compare strings.
Unless of course you've compiled your code with UNICODE enabled (I don't think dev-c++ defaults to unicode at the moment), so you may be OK.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thanks for the reply here is the working code:
Code:#include <windows.h> LRESULT CALLBACK PencereProseduru (HWND, UINT, WPARAM, LPARAM); char YapiSinifAdi[ ] = "WindowsApp"; HWND DugmeTutmaci; HMENU Yavru1; int WINAPI WinMain (HINSTANCE AnaFonksiyonTutmaci, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND AnaPencereTutmaci; MSG Mesajlar; WNDCLASSEX PencereYapisi; PencereYapisi.hInstance = AnaFonksiyonTutmaci; PencereYapisi.lpszClassName = YapiSinifAdi; PencereYapisi.lpfnWndProc = PencereProseduru; PencereYapisi.style = CS_DBLCLKS; PencereYapisi.cbSize = sizeof (WNDCLASSEX); PencereYapisi.hIcon = LoadIcon (NULL, IDI_APPLICATION); PencereYapisi.hIconSm = LoadIcon (NULL, IDI_APPLICATION); PencereYapisi.hCursor = LoadCursor (NULL, IDC_ARROW); PencereYapisi.lpszMenuName = NULL; PencereYapisi.cbClsExtra = 0; PencereYapisi.cbWndExtra = 0; PencereYapisi.hbrBackground = (HBRUSH) COLOR_BACKGROUND; if (!RegisterClassEx (&PencereYapisi)) return 0; AnaPencereTutmaci = CreateWindowEx ( 0, YapiSinifAdi, "Windows App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, AnaFonksiyonTutmaci, NULL ); DugmeTutmaci = CreateWindowEx ( 0, "button", "Durdur", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 1, 1, 170, 30, AnaPencereTutmaci, (HMENU)1, AnaFonksiyonTutmaci, NULL ); ShowWindow (AnaPencereTutmaci, nFunsterStil); while (GetMessage (&Mesajlar, NULL, 0, 0)) { TranslateMessage(&Mesajlar); DispatchMessage(&Mesajlar); } return Mesajlar.wParam; } LRESULT CALLBACK PencereProseduru (HWND AnaPencereTutmaci, UINT message, WPARAM wParam, LPARAM lParam) { char Icerik[256]; switch (message) { case WM_COMMAND: if(LOWORD(wParam) == 1 && HIWORD(wParam) == BN_CLICKED) { GetWindowText(DugmeTutmaci, Icerik, sizeof(Icerik)+11); if (strcmp(Icerik, "Durdur")== 0) {SetWindowText (DugmeTutmaci,"Calistir"); break;} else {SetWindowText (DugmeTutmaci, "Durdur"); break;} } break; case WM_DESTROY: PostQuitMessage (0); break; default: return DefWindowProc (AnaPencereTutmaci, message, wParam, lParam); } return 0; }
Last edited by hileci555; 07-10-2008 at 04:50 PM. Reason: It is liviiiing
> sizeof(Icerik)+11
I said DELETE the addition, not make it bigger?
What if you really have more chars than will fit, and you lie about the buffer size?
Trashed stack, random crashing and "Huh?" posted on message boards is what awaits you.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Well it's a good example of how "working" != "bug free"
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.