201 lines
6.0 KiB
C++
Raw Normal View History

2024-02-07 20:59:24 +08:00
#include"window.h"
#include"controls.h"
#include"Lang/Lang.h"
2024-02-11 23:49:11 +08:00
#include<shellapi.h>
2024-02-12 00:17:58 +08:00
HICON GetExeIcon(const std::wstring& filePath) {
2024-02-11 23:49:11 +08:00
SHFILEINFO fileInfo;
HICON hIcon = NULL;
2024-02-12 00:17:58 +08:00
if (SHGetFileInfo(filePath.c_str(), 0, &fileInfo, sizeof(fileInfo), SHGFI_ICON | SHGFI_LARGEICON)) {
2024-02-11 23:49:11 +08:00
hIcon = fileInfo.hIcon;
}
return hIcon;
}
2024-02-09 09:25:26 +08:00
void basewindow::setfont(int sz,LPCWSTR fn){
if(fn==0)fn=DefaultFont;
hfont=CreateFont(sz, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET , OUT_DEFAULT_PRECIS,
2024-02-07 20:59:24 +08:00
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
2024-02-09 09:25:26 +08:00
DEFAULT_PITCH | FF_DONTCARE, fn);
SendMessage(winId, WM_SETFONT, (WPARAM)hfont, TRUE);
2024-02-07 20:59:24 +08:00
}
std::wstring basewindow::text(){
int textLength = GetWindowTextLength(winId);
std::vector<wchar_t> buffer(textLength + 1);
GetWindowText(winId, buffer.data(), buffer.size());
return buffer.data();
}
void basewindow::settext(const std::wstring& text){
SetWindowText(winId,text.c_str());
}
void basewindow::setgeo(int x,int y,int w,int h){
MoveWindow(winId,x,y,w,h,TRUE);
2024-03-28 11:01:44 +08:00
on_size(w,h);
2024-02-07 20:59:24 +08:00
}
RECT basewindow::getgeo(){
RECT rect;
GetWindowRect(winId,&rect);
return rect;
}
LRESULT mainwindow::wndproc(UINT message, WPARAM wParam, LPARAM lParam){
switch (message)
{
case WM_SHOWWINDOW:
{
on_show();
2024-02-09 09:25:26 +08:00
if(hfont==0)hfont=parent->hfont;
if(hfont)
EnumChildWindows(winId, [](HWND hwndChild, LPARAM lParam)
{
if(0==SendMessage(hwndChild, WM_GETFONT, 0, 0))
SendMessage(hwndChild, WM_SETFONT, lParam, TRUE);
return TRUE;
}, (LPARAM)hfont);
2024-02-07 20:59:24 +08:00
break;
}
case WM_SIZE:
{
int width = LOWORD(lParam);
int height = HIWORD(lParam);
on_size(width,height);
break;
}
2024-02-11 23:49:11 +08:00
case WM_NOTIFY:
{
NMHDR* pnmhdr = (NMHDR*)lParam;
for(auto ctl:controls)
{
if(pnmhdr->hwndFrom==ctl->winId)
{
ctl->dispatch_2(wParam,lParam);break;
}
}
}
2024-02-07 20:59:24 +08:00
case WM_COMMAND:
{
if(lParam==0){
for(auto ctl:controls){
if(lastcontexthwnd==ctl->winId){
if(ctl->menu)
ctl->menu.value().dispatch(wParam);
break;
2024-02-07 20:59:24 +08:00
}
}
}
else
for(auto ctl:controls){
if((HWND)lParam==ctl->winId){
ctl->dispatch(wParam);break;
}
}
break;
}
case WM_CONTEXTMENU:
{
bool succ=false;lastcontexthwnd=0;
for(auto ctl:controls){
if((HWND)wParam==ctl->winId){
auto hm=ctl->on_menu();
ctl->menu=hm;
2024-02-07 20:59:24 +08:00
if(hm){
int xPos = LOWORD(lParam);
int yPos = HIWORD(lParam);
TrackPopupMenu(hm.value().load(), TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
2024-02-07 20:59:24 +08:00
xPos, yPos, 0, winId, NULL);
lastcontexthwnd=ctl->winId;
succ=true;
}
break;
}
}
if(succ==false)return DefWindowProc(winId, message, wParam, lParam);
break;
}
case WM_CLOSE:
{
on_close();
if(parent==0)PostQuitMessage(0);
else ShowWindow(winId,SW_HIDE);
break;
}
default:
return DefWindowProc(winId, message, wParam, lParam);
}
return 0;
}
2024-02-09 09:25:26 +08:00
std::pair<int,int>mainwindow::calculateXY(int w,int h){
int cx,cy;
if(parent==0){
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
cx = screenWidth / 2;
cy = screenHeight / 2;
}
else{
auto rect=parent->getgeo();
cx=(rect.left+rect.right)/2;
cy=(rect.top+rect.bottom)/2;
}
return {cx-w/2,cy-h/2};
}
void mainwindow::setcentral(int w,int h){
auto [x,y]=calculateXY(w,h);
setgeo(x,y,w,h);
}
2024-03-28 11:01:44 +08:00
void mainwindow::setlayout(control* _l){
layout=_l;
}
2024-02-07 20:59:24 +08:00
mainwindow::mainwindow(mainwindow* _parent){
2024-03-28 11:01:44 +08:00
layout=0;
2024-02-07 20:59:24 +08:00
const wchar_t CLASS_NAME[] = L"LunaHostWindow";
WNDCLASS wc = {};
wc.lpfnWndProc = [](HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
mainwindow* _window = reinterpret_cast<mainwindow *>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
if ((!_window)||(_window->winId!=hWnd)) return DefWindowProc(hWnd, message, wParam, lParam);
return _window->wndproc(message,wParam,lParam);
};
wc.hInstance = GetModuleHandle(0);
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW );
2024-02-12 00:17:58 +08:00
wc.hIcon= GetExeIcon(GetModuleFilename().value());//LoadIconW(GetModuleHandle(0),L"IDI_ICON1");
2024-02-11 23:49:11 +08:00
2024-02-07 20:59:24 +08:00
static auto _=RegisterClass(&wc);
HWND hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,CLASS_NAME,CLASS_NAME,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
_parent?_parent->winId:NULL,NULL,GetModuleHandle(0),this
);
winId = hWnd;
parent=_parent;
SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)this);
}
void mainwindow::show(){
ShowWindow(winId, SW_SHOW);
SetForegroundWindow(winId);
}
void mainwindow::close(){
ShowWindow(winId, SW_HIDE);
}
void mainwindow::run(){
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void mainwindow::on_close(){}
void mainwindow::on_show(){}
2024-03-28 11:01:44 +08:00
void mainwindow::on_size(int w,int h){
if(layout)
{
layout->setgeo(0,0,w,h);
}
}
void basewindow::on_size(int w,int h){}