195 lines
5.9 KiB
C++
195 lines
5.9 KiB
C++
#include "pch.h"
|
||
#include "ui.h"
|
||
#include <thread>
|
||
#include <atomic>
|
||
|
||
ezui::Label* m_bottomInfo; //底部信息条
|
||
ezui::ProgressBar* m_progressBar; // 保存进度条指针
|
||
std::atomic<bool> m_isRunning{ false }; // 进度条运行状态
|
||
|
||
using namespace ezui;
|
||
|
||
enum MenuIds {
|
||
ID_MENU_TEST = 1001,
|
||
ID_MENU_EXIT = 1002,
|
||
ID_MENU_DEMO = 1003
|
||
};
|
||
|
||
// 进度条测试
|
||
DWORD __stdcall ProgressBarTest(LPVOID lpParam)
|
||
{
|
||
m_isRunning = true;
|
||
for (int i = 0; i <= 100; i++) {
|
||
float val = i / 100.0f;
|
||
m_progressBar->SetProgress(val);
|
||
Sleep(30);
|
||
}
|
||
|
||
m_isRunning = false;
|
||
return 0;
|
||
}
|
||
|
||
class SimpleMenuWindow : public Window {
|
||
public:
|
||
SimpleMenuWindow() : Window(1000, 620) {
|
||
SetText(L"示例窗口");
|
||
SetMiniSize(Size(1000, 620));
|
||
|
||
BuildBlankLayout(); // 创建UI控件
|
||
CreateMenuBar(); // 创建菜单栏
|
||
|
||
Show();
|
||
}
|
||
protected:
|
||
// 处理命令
|
||
virtual LRESULT WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam) override {
|
||
if (uMsg == WM_COMMAND) {
|
||
UINT id = LOWORD(wParam);
|
||
|
||
switch (id)
|
||
{
|
||
case ID_MENU_TEST:
|
||
MessageBoxW(Hwnd(), L"测试菜单被点击", L"提示", MB_OK | MB_ICONINFORMATION);
|
||
break;
|
||
case ID_MENU_DEMO: // 测试菜单
|
||
{
|
||
if (m_isRunning) {
|
||
std::cout << "测试线程已在运行中" << std::endl;
|
||
}
|
||
else {
|
||
HANDLE hThread = CreateThread(nullptr, 0, ProgressBarTest, nullptr, 0, nullptr);
|
||
if (hThread) CloseHandle(hThread);
|
||
}
|
||
}
|
||
break;
|
||
case ID_MENU_EXIT:
|
||
Close();
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
return Window::WndProc(uMsg, wParam, lParam);
|
||
}
|
||
virtual void OnClose(bool& bClose) override {
|
||
// 可以在这里做确认,当前直接关闭
|
||
Application::Exit(0);
|
||
}
|
||
|
||
// 重写OnSize方法,响应窗口大小变化
|
||
virtual void OnSize(const Size& sz) override {
|
||
Window::OnSize(sz); // 先调用基类方法
|
||
|
||
// 更新编辑框的位置和大小
|
||
if (m_textBox) {
|
||
UpdateTextBoxLayout();
|
||
}
|
||
}
|
||
private:
|
||
Control m_root; // 绝对布局
|
||
TextBox* m_textBox = nullptr; // 保存编辑框指针
|
||
|
||
void BuildBlankLayout() {
|
||
|
||
// 创建标签
|
||
auto* label = new Label();
|
||
label->SetText(L"标签:");
|
||
label->SetLocation({ 15, 27 });
|
||
label->SetAutoWidth(true);
|
||
label->SetAutoHeight(true);
|
||
label->Style.FontSize = 14;
|
||
m_root.Add(label);
|
||
|
||
// 创建彩色按钮
|
||
auto* colorButton = new Button();
|
||
colorButton->SetText(L"彩色按钮");
|
||
colorButton->SetFixedSize(Size(100, 35));
|
||
colorButton->SetLocation({ 70, 20 }); //坐标设置
|
||
colorButton->Style.FontSize = 14;
|
||
//colorButton->SetEnabled(false);
|
||
colorButton->Style.BackColor = Color(52, 152, 219);
|
||
colorButton->Style.ForeColor = Color::White;
|
||
colorButton->HoverStyle.BackColor = Color(41, 128, 185);
|
||
colorButton->ActiveStyle.BackColor = Color(31, 97, 141);
|
||
m_root.Add(colorButton);
|
||
// 绑定点击事件
|
||
colorButton->EventHandler = [this](Control* sender, EventArgs& args) {
|
||
if (args.EventType == Event::OnMouseUp) {
|
||
|
||
}
|
||
};
|
||
|
||
// 添加编辑框1
|
||
m_textBox = new TextBox;
|
||
m_textBox->Style.Border = 1;
|
||
m_textBox->Style.Border.Color = Color(127, 127, 127, 127);
|
||
m_textBox->Style.Border.Style = StrokeStyle::Solid;
|
||
m_textBox->SetText(L"我是一个多行文本框");
|
||
m_textBox->SetMultiLine(true);
|
||
|
||
// 初始化编辑框的位置和大小
|
||
UpdateTextBoxLayout();
|
||
|
||
ScrollBar* sBar = m_textBox->GetScrollBar();
|
||
sBar->ActiveStyle.ForeColor = Color::Black;
|
||
sBar->Style.Border.Radius = 10;
|
||
sBar->SetFixedWidth(10);
|
||
|
||
m_root.Add(m_textBox);
|
||
|
||
// 创建进度条
|
||
m_progressBar = new ProgressBar();
|
||
m_progressBar->Style.Border.Radius = 4;
|
||
m_progressBar->SetLocation({ 200, 25 });
|
||
m_progressBar->SetFixedSize({ 200, 25 });
|
||
m_progressBar->SetProgressColor(Color(3, 191, 3, 220));
|
||
m_progressBar->SetBackgroundColor(Color(50, 50, 50, 50));
|
||
m_root.Add(m_progressBar);
|
||
|
||
SetLayout(&m_root);
|
||
}
|
||
|
||
// 更新编辑框布局的方法
|
||
void UpdateTextBoxLayout() {
|
||
if (!m_textBox) return;
|
||
|
||
// 根据窗口大小计算编辑框的位置和大小
|
||
int windowWidth = Width();
|
||
int windowHeight = Height();
|
||
|
||
// 编辑框规格:固定高度300,宽度为窗口宽度-20,X坐标10,Y坐标为窗口高度-200
|
||
int textBoxWidth = windowWidth - 20;
|
||
int textBoxHeight = 300;
|
||
int textBoxX = 10;
|
||
int textBoxY = windowHeight - 200;
|
||
|
||
m_textBox->SetLocation({ textBoxX, textBoxY });
|
||
m_textBox->SetFixedSize({ textBoxWidth, textBoxHeight });
|
||
}
|
||
|
||
|
||
void CreateMenuBar() {
|
||
HMENU hMenuBar = ::CreateMenu();
|
||
HMENU hOpen = ::CreatePopupMenu();
|
||
::AppendMenuW(hOpen, MF_STRING, ID_MENU_TEST, L"测试");
|
||
::AppendMenuW(hOpen, MF_STRING, ID_MENU_DEMO, L"测试进度条");
|
||
::AppendMenuW(hOpen, MF_SEPARATOR, 0, nullptr);
|
||
::AppendMenuW(hOpen, MF_STRING, ID_MENU_EXIT, L"退出");
|
||
::AppendMenuW(hMenuBar, MF_POPUP, (UINT_PTR)hOpen, L"打开");
|
||
::SetMenu(Hwnd(), hMenuBar);
|
||
}
|
||
};
|
||
|
||
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
|
||
_In_opt_ HINSTANCE hPrevInstance,
|
||
_In_ LPWSTR lpCmdLine,
|
||
_In_ int nCmdShow)
|
||
{
|
||
Application app;
|
||
app.EnableHighDpi(); // 启用高DPI适配
|
||
|
||
SimpleMenuWindow window;
|
||
|
||
window.Show(nCmdShow);
|
||
return app.Exec();
|
||
} |