16,471
社区成员
发帖
与我相关
我的任务
分享
#include"windows.h"
#include"stdio.h"
#define _T(x) x
LRESULT CALLBACK MyProc( HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[]="窗口";
char lpszTitle[]="窗口示例";
wndclass.style=CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc =MyProc;
wndclass.cbWndExtra=0;
wndclass.cbClsExtra =0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=lpszClassName;
if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hwnd=CreateWindow(lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, hwnd, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MyProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CLOSE:
MessageBox(NULL,_T("收到WM_CLOSE消息,即将调用 DestroyWindow(hwnd);"),_T("注意了"),MB_OK);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
MessageBox(NULL,_T("已调用过DestroyWindow(hwnd),看不到窗口了吧;"),_T("注意了"),MB_OK);
MessageBox(NULL,_T("以下处理WM_DESTROY"),_T("注意了"),MB_OK);
PostQuitMessage(0);
break;
case WM_SYSCOMMAND:
if((LOWORD(wParam)&0xFFF0)==SC_CLOSE)
{
MessageBox(NULL,_T("你关闭了窗口,即将发送WM_CLOSE消息!"),_T("注意了"),MB_OK);
SendMessage(hwnd,WM_CLOSE,0,0);
}
//不要添加 break,否则不能响应其它WM_SYSCOMMAND消息
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return(0);
}
#include <Windows.h>
#include <TCHAR.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInst,
LPSTR lpszCmdLine,
int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[]="窗口";
char lpszTitle[]="窗口示例";
wndclass.style=CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc =WndProc;
wndclass.cbWndExtra=0;
wndclass.cbClsExtra =0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=lpszClassName;
if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hwnd=CreateWindow(lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(1)
{
GetMessage(&Msg,NULL,0,0);
if(Msg.message==WM_QUIT)
{
MessageBox(NULL,_T("收到WM_QUIT,即将跳出消息循环"),_T("注意了"),MB_OK);
break;
}
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
MessageBox(NULL,_T("已经跳出消息循环,应用程序即将结束!"),_T("注意了"),MB_OK);
return Msg.wParam;
}
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_CLOSE:
MessageBox(NULL,_T("收到WM_CLOSE消息,即将调用 DestroyWindow(hwnd);"),_T("注意了"),MB_OK);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
MessageBox(NULL,_T("已调用过DestroyWindow(hwnd),看不到窗口了吧;"),_T("注意了"),MB_OK);
MessageBox(NULL,_T("以下处理WM_DESTROY"),_T("注意了"),MB_OK);
PostQuitMessage(0);
break;
case WM_SYSCOMMAND:
if((LOWORD(wParam)&0xFFF0)==SC_CLOSE)
{
MessageBox(NULL,_T("你关闭了窗口,即将发送WM_CLOSE消息!"),_T("注意了"),MB_OK);
SendMessage(hwnd,WM_CLOSE,0,0);
}
//不要添加 break,否则不能响应其它WM_SYSCOMMAND消息
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return(0);
}