Skip to content

Commit

Permalink
新增自动记忆上次窗口位置尺寸
Browse files Browse the repository at this point in the history
  • Loading branch information
jark006 committed Oct 5, 2024
1 parent 7e22e83 commit 110fb3b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
18 changes: 18 additions & 0 deletions jarkViewer/include/D2D1App.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
#include "Utils.h"


struct SettingParameter {
uint8_t header[32];
RECT rect{};
uint32_t showCmd = SW_MAXIMIZE;

uint32_t padding[51];
};

static_assert(sizeof(SettingParameter) == 256, "sizeof(SettingParameter) != 256");

class D2D1App
{
public:
Expand Down Expand Up @@ -41,6 +51,9 @@ class D2D1App
// 丢弃设备有关资源
void DiscardDeviceResources();

void loadSettings();
void saveSettings();

// 消息处理:鼠标
virtual void OnMouseDown(WPARAM btnState, int x, int y) { }
virtual void OnMouseUp(WPARAM btnState, int x, int y) { }
Expand Down Expand Up @@ -86,4 +99,9 @@ class D2D1App
D3D_FEATURE_LEVEL m_featureLevel;
// 手动交换链
DXGI_PRESENT_PARAMETERS m_parameters;

wstring exePath;
wstring settingPath;
string_view settingHeader = "JarkViewerSetting";
SettingParameter settingPar;
};
71 changes: 61 additions & 10 deletions jarkViewer/src/D2D1App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

D2D1App::D2D1App()
{
loadSettings();
m_parameters.DirtyRectsCount = 0;
m_parameters.pDirtyRects = nullptr;
m_parameters.pScrollRect = nullptr;
Expand All @@ -10,12 +11,68 @@ D2D1App::D2D1App()

D2D1App::~D2D1App()
{
saveSettings();
this->DiscardDeviceResources();
Utils::SafeRelease(m_pD2DFactory);
Utils::SafeRelease(m_pWICFactory);
Utils::SafeRelease(m_pDWriteFactory);
}

void D2D1App::loadSettings() {
const int MAX_PATH_LEN = 512;
TCHAR szPath[MAX_PATH_LEN];

if (GetModuleFileName(NULL, szPath, MAX_PATH_LEN) != 0) {

exePath = wstring(szPath);
settingPath = exePath.substr(0, exePath.length() - 3) + L"db";

auto f = _wfopen(settingPath.c_str(), L"rb");
if (f) {
SettingParameter tmp{ 0 };
int readLen = fread(&tmp, 1, sizeof(SettingParameter), f);
fclose(f);

if (readLen == sizeof(SettingParameter) && !memcmp(settingHeader.data(), tmp.header, settingHeader.length() + 1))
settingPar = tmp;

if (settingPar.showCmd == SW_NORMAL) {
int screenWidth = (::GetSystemMetrics(SM_CXFULLSCREEN));
int screenHeight = (::GetSystemMetrics(SM_CYFULLSCREEN));

if (settingPar.rect.left > screenWidth || settingPar.rect.top > screenHeight ||
(settingPar.rect.right - settingPar.rect.left) > screenWidth ||
(settingPar.rect.bottom - settingPar.rect.top) > screenHeight) {
settingPar.rect = { 100, 100, 100 + screenWidth / 2, 100 + screenHeight / 2 };
}
}
}
}
}

void D2D1App::saveSettings() {
WINDOWPLACEMENT wp;
wp.length = sizeof(WINDOWPLACEMENT);

// 获取窗口的显示状态和位置信息
if (GetWindowPlacement(m_hWnd, &wp) && wp.showCmd == SW_NORMAL) {
settingPar.showCmd = SW_NORMAL;
settingPar.rect = wp.rcNormalPosition;
}
else {
settingPar.showCmd = SW_MAXIMIZE;
settingPar.rect = {};
}

memcpy(settingPar.header, settingHeader.data(), settingHeader.length() + 1);

auto f = _wfopen(settingPath.c_str(), L"wb");
if (f) {
fwrite(&settingPar, 1, sizeof(SettingParameter), f);
fclose(f);
}
}

// 初始化
HRESULT D2D1App::Initialize(HINSTANCE hInstance, int nCmdShow)
{
Expand All @@ -34,17 +91,11 @@ HRESULT D2D1App::Initialize(HINSTANCE hInstance, int nCmdShow)
wcex.hIcon = nullptr;
// 注册窗口
RegisterClassExW(&wcex);
// 计算窗口大小
RECT window_rect = { 0, 0, 1080, 640 };

RECT window_rect = settingPar.showCmd == SW_NORMAL ? settingPar.rect : RECT{ 0, 0, 800, 600 };
DWORD window_style = WS_OVERLAPPEDWINDOW;
AdjustWindowRect(&window_rect, window_style, FALSE);
window_rect.right -= window_rect.left;
window_rect.bottom -= window_rect.top;
window_rect.left = (::GetSystemMetrics(SM_CXFULLSCREEN) - window_rect.right) / 2;
window_rect.top = (::GetSystemMetrics(SM_CYFULLSCREEN) - window_rect.bottom) / 2;
// 创建窗口
m_hWnd = CreateWindowExW(0, L"D2D1WndClass", m_wndCaption.c_str(), window_style,
window_rect.left, window_rect.top, window_rect.right, window_rect.bottom,
window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top,
0, 0, hInstance, this);
hr = m_hWnd ? S_OK : E_FAIL;

Expand All @@ -54,7 +105,7 @@ HRESULT D2D1App::Initialize(HINSTANCE hInstance, int nCmdShow)
CreateDeviceIndependentResources();
CreateDeviceResources();

ShowWindow(m_hWnd, SW_MAXIMIZE);
ShowWindow(m_hWnd, settingPar.showCmd == SW_NORMAL ? SW_NORMAL : SW_MAXIMIZE);
UpdateWindow(m_hWnd);
}
return hr;
Expand Down
4 changes: 2 additions & 2 deletions jarkViewer/src/jarkViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class JarkViewerApp : public D2D1App {
}break;

case WM_RBUTTONUP: {//右键
exit(0);
operateQueue.push({ ActionENUM::requitExit });
}break;

case WM_MBUTTONUP: {//中键
Expand Down Expand Up @@ -547,7 +547,7 @@ class JarkViewerApp : public D2D1App {
} break;

case ActionENUM::requitExit: {
exit(0);
PostMessage(m_hWnd, WM_DESTROY, 0, 0);
} break;

default:
Expand Down

0 comments on commit 110fb3b

Please sign in to comment.