#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息
#include <windows.h>// Windows 头文件
#include "resource.h"
#include <tchar.h>
#include"stdio.h"
// 此代码模块中包含的函数的前向声明:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
class mywin
{
public:
ATOM m_Register(_In_ HINSTANCE hInstance);
BOOL m_Instance(_In_ HINSTANCE hInstance, _In_ int nCmdShow);
BOOL m_ShowWindow();
BOOL m_UpdateWindow();
HWND m_hWnd;
};
ATOM mywin::m_Register(_In_ HINSTANCE hInstance)
{
//第一步填充WNDCLASSEX, 窗口类结构体
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX); //结构体大小
wcex.style = CS_HREDRAW | CS_VREDRAW; //重画
wcex.lpfnWndProc = WndProc; //回调函数
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//背景颜色
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1); //菜单名字
wcex.lpszClassName = _T("Class"); //类名字
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);//第二步 调用注册函数
}
BOOL mywin::m_Instance(_In_ HINSTANCE hInstance, _In_ int nCmdShow)
{
m_hWnd = CreateWindow(_T("Class"), //LPCTSTR lpClassName,
_T("标题"), //LPCTSTR lpWindowName,
WS_OVERLAPPEDWINDOW,//DWORD dwStyle,
200, // int x,
100, //int y,
600, //int nWidth,
400, //int nHeight,
NULL, //HWND hWndParent,
NULL, //HMENU hMenu,
hInstance, //HINSTANCE hInstance,
NULL); //LPVOID lpParam
if (!m_hWnd)
{
MessageBox(NULL, TEXT("创建窗口失败"), TEXT("提示"), MB_ICONWARNING);
return FALSE;
}
ShowWindow(m_hWnd, SW_SHOWNORMAL); //显示窗口
UpdateWindow(m_hWnd);
return TRUE;
}
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
mywin Obj;
Obj.m_Register(hInstance);
Obj.m_Instance(hInstance, nCmdShow);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
int xx = 0, yy = 0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
switch (message)
{
case WM_COMMAND://菜单命令
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)// 分析菜单选择:
{
case ID_FIRST_NEW:
MessageBox(hWnd, _T("left"), _T("title"), MB_OK);//"LPCWSTR
break;
case ID_FIRST_OPEN:
MessageBox(hWnd, _T("left"), _T("title"), MB_OK);//"LPCWSTR
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CHAR: //键盘字符
{
HDC hdc;
hdc = GetDC(hWnd);
char ch[2];
sprintf(ch, "%c", wParam);
WCHAR p[2];
MultiByteToWideChar(CP_ACP, 0, ch, 2, p, 2);
TextOut(hdc, xx, yy, p, 1);
xx += 10;
if (xx > 300)
{
xx = 0;
yy += 20;
}
ReleaseDC(hWnd, hdc);
break;
}
case WM_LBUTTONDOWN://鼠标左键
MessageBox(hWnd, _T("left"), _T("title"), MB_OK);//"LPCWSTR
break;
case WM_RBUTTONDOWN://鼠标左键
MessageBox(hWnd, _T("right"), _T("title"), MB_OK);//"LPCWSTR
break;
case WM_PAINT:
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
WCHAR *wp;
wp = _T("123456");
TextOut(hdc, 30, 20, wp, wcslen(wp));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY: //窗口已经销毁
PostQuitMessage(0);//退出程序
break;
default: //缺省消息处理
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}