1 Star 0 Fork 0

Janisa/GUI

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Switch.h 4.92 KB
一键复制 编辑 原始数据 按行查看 历史
Janisa 提交于 2024-07-09 19:24 +08:00 . 添加编译工程脚本
#pragma once
#include <ControlBase.h>
class Switch : public Control
{
float last_width = 0.0f;
public:
virtual UIClass Type() { return UIClass::UI_Switch; }
bool IsContainer() override
{
return false;
}
D2D1_COLOR_F UnderMouseColor = Colors::DarkSlateGray;
float Boder = 1.5f;
Switch(int x = 0, int y = 0, int width = 60, int height = 22)
{
this->Location = POINT{x, y};
this->Size = SIZE{width, height};
auto bc = this->BackColor;
bc.a = 0.0f;
this->BackColor = bc;
}
void Update() override
{
if (this->IsVisual == false)
return;
bool isUnderMouse = this->MostParent->UnderMouse == this;
bool isSelected = this->MostParent->Selected == this;
auto d2d = this->Render;
auto abslocation = this->AbsLocation;
auto size = this->ActualSize();
auto absRect = this->AbsRect;
if (last_width > size.cx)
{
absRect.right += last_width - size.cx;
size.cx = last_width;
}
d2d->PushDrawRect(absRect.left, absRect.top, absRect.right - absRect.left, absRect.bottom - absRect.top);
{
Control *tmpc = this;
List<Control *> need_render_background;
while (tmpc->BackColor.a < 1.0f)
{
tmpc = tmpc->Parent;
need_render_background.Add(tmpc);
}
if (this->BackColor.a < 1.0f)
{
for (int i = need_render_background.Count - 1; i >= 0; i--)
{
d2d->FillRect(abslocation.x, abslocation.y, size.cx, size.cy, need_render_background[i]->BackColor);
if (need_render_background[i]->Image)
{
need_render_background[i]->RenderImage();
}
}
}
float r = size.cy / 2.0f;
float x1 = abslocation.x + r;
float x2 = abslocation.x + (size.cx - r);
float y = abslocation.y + r;
d2d->FillRect((float)abslocation.x,
(float)abslocation.y,
(float)size.cx,
(float)size.cy, this->BackColor);
d2d->FillEllipse({{x1, y}, r, r}, this->Checked ? Colors::Green : Colors::Red);
d2d->FillEllipse({{x2, y}, r, r}, this->Checked ? Colors::Green : Colors::Red);
d2d->FillRect(x1, abslocation.y, x2 - x1, size.cy, this->Checked ? Colors::Green : Colors::Red);
d2d->FillEllipse({{this->Checked ? x2 : x1, y}, r - 2.0f, r - 2.0f}, Colors::White);
}
d2d->PopDrawRect();
last_width = size.cx;
}
bool ProcessMessage(UINT message, WPARAM wParam, LPARAM lParam, int xof, int yof) override
{
switch (message)
{
case WM_DROPFILES:
{
HDROP hDropInfo = HDROP(wParam);
UINT uFileNum = DragQueryFile(hDropInfo, 0xffffffff, NULL, 0);
TCHAR strFileName[MAX_PATH];
List<std::wstring> files;
for (UINT i = 0; i < uFileNum; i++)
{
DragQueryFile(hDropInfo, i, strFileName, MAX_PATH);
files.Add(strFileName);
}
DragFinish(hDropInfo);
if (files.Count > 0)
{
this->OnDropFile(this, files);
}
}
break;
case WM_MOUSEWHEEL: // mouse wheel
{
MouseEventArgs event_obj = MouseEventArgs(MouseButtons::MouseButtons_None, 0, xof, yof, GET_WHEEL_DELTA_WPARAM(wParam));
this->OnMouseWheel(this, event_obj);
}
break;
case WM_MOUSEMOVE: // mouse move
{
this->MostParent->UnderMouse = this;
MouseEventArgs event_obj = MouseEventArgs(MouseButtons::MouseButtons_None, 0, xof, yof, HIWORD(wParam));
this->OnMouseMove(this, event_obj);
}
break;
case WM_LBUTTONDOWN: // mouse down
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
{
if (WM_LBUTTONDOWN == message)
{
auto lastSelected = this->MostParent->Selected;
this->MostParent->Selected = this;
if (lastSelected && lastSelected != this)
{
lastSelected->SingleUpdate();
}
}
MouseEventArgs event_obj = MouseEventArgs(FromParamToMouseButtons(message), 0, xof, yof, HIWORD(wParam));
this->OnMouseDown(this, event_obj);
this->SingleUpdate();
}
break;
case WM_LBUTTONUP: // mouse up
case WM_RBUTTONUP:
case WM_MBUTTONUP:
{
if (WM_LBUTTONUP == message && this->MostParent->Selected == this)
{
this->Checked = !this->Checked;
MouseEventArgs event_obj = MouseEventArgs(FromParamToMouseButtons(message), 0, xof, yof, HIWORD(wParam));
this->OnMouseClick(this, event_obj);
}
this->MostParent->Selected = NULL;
MouseEventArgs event_obj = MouseEventArgs(FromParamToMouseButtons(message), 0, xof, yof, HIWORD(wParam));
this->OnMouseUp(this, event_obj);
this->SingleUpdate();
}
break;
case WM_LBUTTONDBLCLK: // mouse double click
{
auto lastSelected = this->MostParent->Selected;
this->MostParent->Selected = this;
if (lastSelected && lastSelected == this)
{
this->Checked = !this->Checked;
MouseEventArgs event_obj = MouseEventArgs(FromParamToMouseButtons(message), 0, xof, yof, HIWORD(wParam));
this->OnMouseDoubleClick(this, event_obj);
this->SingleUpdate();
}
}
break;
case WM_KEYDOWN: // keyboard down
{
KeyEventArgs event_obj = KeyEventArgs((Keys)(wParam | 0));
this->OnKeyDown(this, event_obj);
}
break;
case WM_KEYUP: // keyboard up
{
KeyEventArgs event_obj = KeyEventArgs((Keys)(wParam | 0));
this->OnKeyUp(this, event_obj);
}
break;
}
return true;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/Janisa/gui.git
git@gitee.com:Janisa/gui.git
Janisa
gui
GUI
master

搜索帮助