C++ win32应用如何删除按钮 5
例如这个创建的按钮,如何删除他呢?
不是删除资源也不是删除代码,而是程序过程中删除这个按钮
CreateWindow(L"Button", L"按钮一",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 0, 20, 200, 100, 窗口句柄,
(HMENU)AN_1, hinstance全局变量, NULL);
哪位大神给一个点一下按钮就删除这个按钮的代码 谢谢
是WIN32,不是MFC
给力的话加45财富 展开
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS 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.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
360,//CW_USEDEFAULT, // initial x size
270,//CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
static HWND hEdit;
static HWND hBtn;
static HWND hBtnNew = NULL;
TCHAR buf[1000];
static int hBtnClick = 0;
switch (message)
{
case WM_CREATE:
//PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT(""),
WS_CHILDWINDOW, 10, 10, 200, 30,
hwnd, NULL, NULL, NULL);
ShowWindow(hEdit, SW_SHOW);
hBtn = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("BUTTON"), TEXT("创建按钮"),
WS_CHILDWINDOW, 240, 10, 100, 30,
hwnd, NULL, NULL, NULL);
ShowWindow(hBtn, SW_SHOW);
return 0 ;
case WM_COMMAND:
if(lParam == (LPARAM)hBtn)
{
if(!hBtnClick)
{
hBtnNew = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("BUTTON"), TEXT("按钮2"),
WS_CHILDWINDOW, 50, 50, 100, 30,
hwnd, NULL, NULL, NULL);
hBtnClick = 1;
ShowWindow(hBtnNew, SW_SHOW);
sprintf(buf, "创建按钮2 句柄%d",hBtnNew);
SetWindowText(hEdit, buf);
SetWindowText(hBtn, "删除 按钮2");
}
else
{
DestroyWindow(hBtnNew);
sprintf(buf, "删除 按钮2 句柄%d",hBtnNew);
hBtnNew = NULL;
hBtnClick = 0;
SetWindowText(hEdit, buf);
SetWindowText(hBtn, "创建 按钮2");
}
}
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows XP!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
if(hBtnNew) DestroyWindow(hBtnNew);
DestroyWindow(hEdit);
DestroyWindow(hBtn);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}