1窗口

#include <Windows.h>

//wParam 一般处理最大化,最小化,关闭
//lParam 一般处理窗口宽、高的变化
#include<Windows.h>

#define WM_MYDESTROY 2

LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
/*MessageBox(hWnd,TEXT("窗口已创建"),TEXT("窗口"),MB_OKCANCEL)*/;break;
case WM_DESTROY:
//PostQuitMessage(0);break; //进入系统消息队列
SendMessage(hWnd,WM_QUIT,NULL,NULL); //直接调用窗口处理函数,无法关闭窗口
//PostMessage(hWnd,WM_QUIT,NULL,NULL);break;

case WM_MYDESTROY: // 定义自己的销毁值
PostMessage(hWnd,WM_QUIT,NULL,NULL)
case WM_SYSCOMMAND:
if(wParam==SC_CLOSE)
{
int nRect=MessageBox(hWnd,TEXT("确定关闭窗口"),TEXT("窗口"),MB_OKCANCEL);
if(nRect==IDOK)
{}
else
return 0;
break;
}
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}

void MyRegisterWindowClass(HINSTANCE hInstance,LPCWSTR lpClassName,WNDPROC wndproc)
{
WNDCLASS wc={0};
wc.lpszClassName=lpClassName;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.cbClsExtra=NULL;
wc.cbWndExtra=NULL;
wc.hbrBackground=(HBRUSH)COLOR_WINDOW;
wc.hCursor=NULL;
wc.hIcon=NULL;
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;
wc.lpszMenuName=NULL;
RegisterClass(&wc);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//注册主窗口
MyRegisterWindowClass(hInstance,TEXT("main"),WndProc);
//创建主窗口
HWND hWnd=CreateWindow(TEXT("main"),TEXT("TEXT"),WS_OVERLAPPEDWINDOW,200,200,1000,600,NULL,NULL,hInstance,NULL);
//注册子窗口
MyRegisterWindowClass(hInstance,TEXT("child"),WndProc);
//创建子窗口
HWND hWnd1=CreateWindow(TEXT("child"),TEXT
("TEXT1"),WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW/*前两个必备*/,100,200,500,300,hWnd,NULL,hInstance,NULL);
MoveWindow(hWnd1,100,100,800,400,NULL);
//显示窗口
ShowWindow(hWnd,SW_SHOW); //必须显示主窗口
UpdateWindow(hWnd);
//消息循环
MSG msg={0};
while(1)
{
if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) //非阻塞函数,是否捕捉到消息
{
if(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
return 0;
}
}
else
{
AllocConsole(); //为调用进程分配一个新的控制台。
//获取终端句柄
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
//从当前光标位置写入一个字符串到屏幕缓冲区
WriteConsole(handle,"学生",16,0,0);
}
}
/* while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}*/
return 0;
}

posted @ 2017-09-03 21:37  gd_沐辰  阅读(137)  评论(0编辑  收藏  举报