Reputation: 3
Hello everyone i have this problem that i have been trying to solve for a long time . I'm creating a win 32 gui application with C using code::blocks ;
and i wanted to create a menu using buttons so when you click on a button ; a list of buttons will appear; if you click on an other button the list will change and so on.
I managed to create the list of buttons , but when i need to destroy them the DestroyWindow function does nothing and the button remains (I've also tried Sending WM_CLOSE and WM_DESTROY messages to the button ) . i will give a sample code and please tell me what am i doing wrong , it's driving me crazy.
PS: i'm using c and not c++ because i kinda have to.
Thank you all in advance
#include <windows.h>
#include <wchar.h>
#include <stdbool.h>
#define B 115
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,WPARAM wParam, LPARAM lParam)
{
HWND button1,button2;
switch(msg)
{case WM_CREATE:
button1 = CreateWindowW(L"Button", L"Button to push", WS_VISIBLE | WS_CHILD|BS_FLAT|BS_PUSHBUTTON|WS_BORDER,
0,0, 100, 80, hwnd, (HMENU) B, GetModuleHandle(NULL), NULL);
button2 = CreateWindowW(L"Button", L"Button to kill", WS_VISIBLE | WS_CHILD|BS_FLAT|BS_PUSHBUTTON|WS_BORDER,
100,0, 100, 80, hwnd, 0, GetModuleHandle(NULL), NULL);
break;
case WM_COMMAND:
if(LOWORD(wParam)==B)
{
DestroyWindow(button2); //here i destroy the button 2 if button 1 was clicked
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND W;
WNDCLASSW ClassP;
ClassP.style = CS_HREDRAW | CS_VREDRAW;
ClassP.cbClsExtra = 0;
ClassP.cbWndExtra = 0;
ClassP.lpszClassName = L"FenetrePrincipale";
ClassP.hInstance = hInstance;
ClassP.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
ClassP.lpszMenuName = NULL;
ClassP.lpfnWndProc = WndProc;
ClassP.hCursor = LoadCursor(NULL, IDC_ARROW);
ClassP.hIcon = LoadIcon(NULL, IDI_ERROR);
RegisterClassW(&ClassP);
W = CreateWindowW(ClassP.lpszClassName, L"PROBLEM SAMPLE CODE",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 300, 300, NULL, NULL, hInstance, NULL);
ShowWindow(W, nCmdShow);
UpdateWindow(W);
while (GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
Upvotes: 0
Views: 2616
Reputation: 597
Your problem lies in local scope of button1 and button2.
After calling WndProc with WM_COMMAND message you gave uninitialized pointer, so no button is deleted.
Solution:
..........
#define B 115
HWND button1,button2; //<-add here
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,WPARAM wParam, LPARAM lParam)
{
//HWND button1,button2; //<-comment or delete this
switch(msg)
{case WM_CREATE:
..............
Upvotes: 1