Windoows窗口程序六

时间:2023-03-09 04:56:36
Windoows窗口程序六
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h> HINSTANCE g_hInstance=;
HANDLE g_hOutput=;///接收标准输出句柄
HWND g_hWndChild = ;//子窗口句柄 void OnCreate(HWND hWnd,LPARAM lParam)
{
CREATESTRUCT *cs = (CREATESTRUCT *)lParam;
char *pText = (char *)cs->lpCreateParams;
//MessageBox(NULL, pText, "info", MB_OK);
//创建子窗口(子窗口的起始位置0,0是相对于父窗口而言的,并非指屏幕)
g_hWndChild=CreateWindowEx(, "EDIT", "OK", WS_CHILD | WS_VISIBLE | WS_BORDER,
, , cs->cx, cs->cy, hWnd, NULL, g_hInstance, NULL);
} void OnSize(HWND hWnd, LPARAM lParam)
{
int nWidth = LOWORD(lParam);
int nHight = HIWORD(lParam);
CHAR buf[] = { };
sprintf(buf, "width=%d,hight=%d\n", nWidth, nHight);
WriteConsole(g_hOutput, buf, strlen(buf),NULL,NULL);//输出到DOS窗口
//排除窗口刚创建时的WM_SIZE消息
if (NULL == g_hWndChild)
return;
//移动子窗口
MoveWindow(g_hWndChild, , , nWidth, nHight, true);
} LRESULT WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_SIZE:
//窗口创建时会接收到WM_SIZE消息
OnSize(hWnd,lParam);
break;
case WM_SYSCOMMAND:
if (SC_CLOSE == wParam)
{
int nRet=MessageBox(NULL, "是否退出!", "info", MB_YESNO);
if (nRet!=IDYES)
{
return ;
}
}
break;
case WM_CREATE://在窗口生成之前执行
OnCreate(hWnd,lParam);
break;
case WM_DESTROY:
PostQuitMessage();
break;
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
} int Register(HINSTANCE hInstance,LPCTSTR lpClassName)
{
int ret = ;
WNDCLASSEX wce = { };
wce.cbSize = sizeof(wce);
wce.style = CS_HREDRAW | CS_VREDRAW;
wce.lpfnWndProc = (WNDPROC)WndProc;
wce.cbClsExtra = ; wce.cbWndExtra = ;
wce.hInstance = hInstance;
wce.hIcon = NULL;
wce.hCursor = NULL; wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
wce.lpszMenuName = NULL;
wce.lpszClassName = lpClassName;
wce.hIconSm = NULL;
ATOM nAtom = RegisterClassEx(&wce);
ret = nAtom == ? : ;
return ret;
} void Display(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
} void MyMessage()
{
MSG nMsg = { };
while (GetMessage(&nMsg, NULL, , ))
{
TranslateMessage(&nMsg);
DispatchMessage(&nMsg);
}
} HWND CreateWnd(LPSTR lpClsssName, LPSTR lpWndName, HINSTANCE hInstance)
{
HWND hWnd = CreateWindowEx(, lpClsssName, lpWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT
, , , NULL, NULL, hInstance,"HELL");
return hWnd;
} int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
AllocConsole();//打开DOS窗口
g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出句柄
g_hInstance = hInstance;
if (!Register(hInstance, "Main"))
{
return -;
}
HWND hWnd=CreateWnd("Main", "hello", hInstance);
Display(hWnd);
MyMessage();
return ;
}