- #include<windows.h>
- #include<tchar.h>
- BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- //主函数
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow)
- {
- MSG Message;
- if (!InitWindowClass(hInstance, nCmdShow))
- {
- MessageBox(NULL, _T("创建窗口失败!"), _T("创建窗口"), NULL);
- return 1;
- }
- while (GetMessage(&Message, 0, 0, 0)) //消息循环
- {
- TranslateMessage(&Message);
- DispatchMessage(&Message);
- }
- return (int)Message.wParam;
- }
- //消息处理函数
- LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam)
- {
- HDC hDC; //定义指向设备上下文的句柄
- PAINTSTRUCT PtStr; //定义指向包含绘图信息的结构体变量
- HBRUSH hBrush; //定义指向画刷的句柄
- HPEN hPen; //定义指向画笔的句柄
- HFONT myFont; //定义字体句柄
- LPCWSTR String2 = L"注 意 信 号 灯"; //创建一个变量存储要输出的字符串
- switch (iMessage) //处理消息
- {
- case WM_PAINT: //处理绘图消息
- hDC = BeginPaint(hWnd, &PtStr);
- SetMapMode(hDC, MM_TEXT); //设置映像模式
- hPen = CreatePen(PS_DOT, 30, RGB(0, 0, 0)); //设置画笔
- SelectObject(hDC, hPen); //选择画笔
- MoveToEx(hDC, 201, 48, NULL); //设置画笔当前位置
- LineTo(hDC, 36, 327); // /
- LineTo(hDC, 360, 327); // —
- LineTo(hDC, 201, 48); // \
- hPen = CreatePen(PS_DOT, 58, RGB(0, 0, 0)); //设置画笔
- SelectObject(hDC, hPen); //选择画笔
- MoveToEx(hDC, 201, 164, NULL); //设置画笔当前位置
- LineTo(hDC, 201, 275); // /
- hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //设置画笔
- hBrush = CreateSolidBrush(RGB(255, 0, 0)); //设置一个单色填充画刷
- SelectObject(hDC, hBrush); //选择画刷
- SelectObject(hDC, hPen); //选择画笔
- Ellipse(hDC, 179, 143, 224, 188); //红灯
- hPen = CreatePen(PS_SOLID, 1, RGB(236, 240, 20)); //设置画笔
- hBrush = CreateSolidBrush(RGB(236, 240, 20)); //设置一个单色填充画刷
- SelectObject(hDC, hBrush); //选择画刷
- SelectObject(hDC, hPen); //选择画笔
- Ellipse(hDC, 179, 199, 224, 199 + 45); //黄灯
- hPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0)); //设置画笔
- hBrush = CreateSolidBrush(RGB(0, 255, 0)); //设置一个单色填充画刷
- SelectObject(hDC, hBrush); //选择画刷
- SelectObject(hDC, hPen); //选择画笔
- Ellipse(hDC, 179, 251, 224, 251+45); //绿灯
- myFont = CreateFont( //创建一个自定义字体
- 55, //字体的高度(字体大小)
- 0, //由系统根据高宽比选取字体最佳宽度值
- 0, //文本的倾斜度为0,表示水平
- 0, //字体的倾斜度为0
- FW_NORMAL, //字体的粗度,FW_NORMAL为正常 FW_HEAVY为最粗
- 0, //非斜体字
- 0, //无下划线
- 0, //无删除线
- GB2312_CHARSET, //表示所用的字符集为ANSI_CHARSET
- OUT_DEFAULT_PRECIS, //输出精度为缺省精度
- CLIP_DEFAULT_PRECIS, //剪裁精度为缺省精度
- DEFAULT_QUALITY, //输出质量为缺省值
- DEFAULT_PITCH | FF_DONTCARE,//字间距和字体系列使用缺省值
- L"微软雅黑" //字体名称
- );
- SetTextColor(hDC, RGB(0, 0, 0)); //设置文本颜色为翠绿色
- SetBkMode(hDC, TRANSPARENT); //设置背景颜色为透明色
- SelectObject(hDC, myFont); //将自定义字体选入设备环境
- TextOutW(hDC, 57, 379, String2, wcslen(String2));//使用当前字体输出文本
- EndPaint(hWnd, &PtStr); //结束绘图
- DeleteObject(myFont); //删除自定义字体,释放内存
- return 0;
- case WM_DESTROY: //结束应用程序
- PostQuitMessage(0);
- return 0;
- default: //其他消息处理程序
- return(DefWindowProc(hWnd, iMessage, wParam, lParam));
- break;
- }
- return 0;
- }
- BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow) //定义窗口类
- {
- WNDCLASSEX wcex;
- HWND hWnd;
- TCHAR *szWindowClass = _T("注意信号灯");
- TCHAR *szTitle = _T("注意信号灯");
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = 0; //窗口类型为缺省类型
- wcex.lpfnWndProc = WndProc; //窗口处理函数为WndProc
- wcex.cbClsExtra = 0; //窗口类无扩展
- wcex.cbWndExtra = 0; //窗口实例无扩展
- wcex.hInstance = hInstance; //当前实例句柄
- wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
- //窗口的最小化图标为缺省图标
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- //窗口采用箭头光标
- wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- //窗口背景为白色
- wcex.lpszMenuName = NULL; //窗口中无菜单
- wcex.lpszClassName = szWindowClass;//窗口类名为"窗口示例"
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
- if (!RegisterClassEx(&wcex)) //如果注册失败则发出警告声音
- return FALSE;
- hWnd = CreateWindow(szWindowClass, //生成窗口
- szTitle,
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, //窗口左上角的X坐标
- 0, //窗口左上角的Y坐标
- 410, //窗口宽度
- 530, //窗口高度
- NULL,
- NULL,
- hInstance,
- NULL);
- if (!hWnd)
- return FALSE;
- ShowWindow(hWnd, nCmdShow); //显示窗口
- UpdateWindow(hWnd);
- return TRUE;
- }
运行效果如图
本文作者为mengkun,转载请注明。
编译错误
C:\Users\LiZhi\Documents\Tencent Files\1807365372\FileRecv\MobileFile\C++\not game,but we can use in our life\redgreenlight.cpp In function 'LRESULT WndProc(HWND, UINT, UINT, LONG)':
31 23 C:\Users\LiZhi\Documents\Tencent Files\1807365372\FileRecv\MobileFile\C++\not game,but we can use in our life\redgreenlight.cpp [Error] converting to execution character set: Illegal byte sequence
84 13 C:\Users\LiZhi\Documents\Tencent Files\1807365372\FileRecv\MobileFile\C++\not game,but we can use in our life\redgreenlight.cpp [Error] converting to execution character set: Illegal byte sequence
……
@easonlee是不是要用vc编译??