Skip to content

Commit 6d830f9

Browse files
author
XUANXUQAQ
committed
修复多屏幕贴靠模式定位不准确的问题
1 parent e669450 commit 6d830f9

File tree

7 files changed

+51
-28
lines changed

7 files changed

+51
-28
lines changed

C++/getDpi/getDpi/getDpi.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@
44
#pragma comment(lib, "User32.lib")
55
#pragma comment(lib, "Shcore.lib")
66

7-
float get_dpi();
7+
void get_dpi();
88

99
int main()
1010
{
11-
const float dpi = get_dpi();
12-
printf("%f", dpi);
11+
get_dpi();
1312
}
1413

1514
/**
1615
* 获取Windows缩放等级,适配高DPI
1716
*/
18-
float get_dpi()
17+
void get_dpi()
1918
{
2019
const auto hwnd = FindWindowA(nullptr, "File-Engine-SearchBar");
2120
const auto h_monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
2221
UINT x, y; // x == y
2322
GetDpiForMonitor(h_monitor, MDT_EFFECTIVE_DPI, &x, &y);
24-
return static_cast<float>(x) / 96;
23+
printf("%f\n", static_cast<float>(x) / 96); // dpi
24+
25+
MONITORINFO info;
26+
info.cbSize = sizeof(MONITORINFO);
27+
GetMonitorInfo(h_monitor, &info);
28+
const auto width = info.rcMonitor.right - info.rcMonitor.left;
29+
const auto height = info.rcMonitor.bottom - info.rcMonitor.top;
30+
printf("%ld\n", width);
31+
printf("%ld\n", height);
2532
}

C++/getHandle/getHandle/dllmain.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ void checkMouseThread()
504504
void checkTopWindowThread()
505505
{
506506
RECT window_rect;
507+
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE);
507508
while (is_running)
508509
{
509510
HWND hwnd = GetForegroundWindow();

C++/getHandle/getHandle/getHandle.vcxproj

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
<GenerateDebugInformation>true</GenerateDebugInformation>
133133
<EnableUAC>false</EnableUAC>
134134
</Link>
135+
<Manifest>
136+
<EnableDpiAwareness>PerMonitorHighDPIAware</EnableDpiAwareness>
137+
</Manifest>
135138
</ItemDefinitionGroup>
136139
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
137140
<ClCompile>
@@ -151,6 +154,9 @@
151154
<GenerateDebugInformation>true</GenerateDebugInformation>
152155
<EnableUAC>false</EnableUAC>
153156
</Link>
157+
<Manifest>
158+
<EnableDpiAwareness>PerMonitorHighDPIAware</EnableDpiAwareness>
159+
</Manifest>
154160
</ItemDefinitionGroup>
155161
<ItemGroup>
156162
<ClInclude Include="checkHwnd.h" />

src/main/java/file/engine/frames/SearchBar.java

+12-20
Original file line numberDiff line numberDiff line change
@@ -3024,17 +3024,7 @@ private void switchSearchBarShowingMode() {
30243024
EventManagement eventManagement = EventManagement.getInstance();
30253025
GetHandle.INSTANCE.start();
30263026
AllConfigs allConfigs = AllConfigs.getInstance();
3027-
var screenInfo = new Object() {
3028-
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小
3029-
long screenSizeUpdateTime = System.currentTimeMillis();
3030-
};
3031-
final long updateScreenTimeLimit = 5000;
30323027
while (eventManagement.notMainExit()) {
3033-
// 每隔5s更新一次屏幕大小
3034-
if (System.currentTimeMillis() - screenInfo.screenSizeUpdateTime > updateScreenTimeLimit) {
3035-
screenInfo.screenSizeUpdateTime = System.currentTimeMillis();
3036-
screenInfo.screenSize = Toolkit.getDefaultToolkit().getScreenSize();
3037-
}
30383028
if (showingMode == Constants.Enums.ShowingSearchBarMode.EXPLORER_ATTACH) {
30393029
String searchInfoLabelText = searchInfoLabel.getText();
30403030
if (searchInfoLabelText != null && searchInfoLabelText.isEmpty()) {
@@ -3043,10 +3033,11 @@ private void switchSearchBarShowingMode() {
30433033
.getInstance()
30443034
.getTranslation("Double-click shift to switch here")));
30453035
}
3046-
getExplorerSizeAndChangeSearchBarSizeExplorerMode(screenInfo.screenSize);
3036+
getExplorerSizeAndChangeSearchBarSizeExplorerMode();
30473037
} else {
3048-
int width = screenInfo.screenSize.width;
3049-
int height = screenInfo.screenSize.height;
3038+
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
3039+
int width = screenSize.width;
3040+
int height = screenSize.height;
30503041
int searchBarWidth = (int) (width * SEARCH_BAR_WIDTH_RATIO);
30513042
int searchBarHeight = (int) (height * SEARCH_BAR_HEIGHT_RATIO);
30523043
int positionX, positionY;
@@ -3092,14 +3083,15 @@ private void switchSearchBarShowingMode() {
30923083
/**
30933084
* 获取explorer窗口大小,并修改显示模式和大小
30943085
*/
3095-
private void getExplorerSizeAndChangeSearchBarSizeExplorerMode(Dimension screenSize) {
3096-
double dpi = DpiUtil.getDpi();
3097-
long explorerWidth = (long) (GetHandle.INSTANCE.getExplorerWidth() / dpi);
3098-
long explorerHeight = (long) (GetHandle.INSTANCE.getExplorerHeight() / dpi);
3099-
long explorerX = (long) (GetHandle.INSTANCE.getExplorerX() / dpi);
3100-
long explorerY = (long) (GetHandle.INSTANCE.getExplorerY() / dpi);
3086+
private void getExplorerSizeAndChangeSearchBarSizeExplorerMode() {
3087+
Dimension dimension = new Dimension();
3088+
float dpi = DpiUtil.getDpi(dimension);
3089+
long explorerWidth = GetHandle.INSTANCE.getExplorerWidth();
3090+
long explorerHeight = GetHandle.INSTANCE.getExplorerHeight();
3091+
long explorerX = GetHandle.INSTANCE.getExplorerX();
3092+
long explorerY = GetHandle.INSTANCE.getExplorerY();
31013093
int searchBarWidth = (int) (explorerWidth * SEARCH_BAR_WIDTH_RATIO);
3102-
int searchBarHeight = (int) (screenSize.height * SEARCH_BAR_HEIGHT_RATIO);
3094+
int searchBarHeight = (int) (dimension.height / dpi * SEARCH_BAR_HEIGHT_RATIO);
31033095

31043096
int labelHeight = searchBarHeight / 9;
31053097
//explorer窗口大于20像素才开始显示,防止误判其他系统窗口

src/main/java/file/engine/utils/DpiUtil.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
import lombok.SneakyThrows;
44

5+
import java.awt.*;
56
import java.io.BufferedReader;
67
import java.io.InputStreamReader;
78
import java.nio.file.Path;
89

910
public class DpiUtil {
1011

11-
private static volatile double dpi = 1;
12+
private static volatile float dpi = 1;
13+
private static volatile Dimension screenDimension = new Dimension();
1214
private static volatile long getDpiTime = 0;
1315
private static final String DPI_CHECK_PROC = "user/getDpi.exe";
1416

17+
public static float getDpi() {
18+
return getDpi(null);
19+
}
20+
1521
@SneakyThrows
16-
public static double getDpi() {
22+
public static float getDpi(Dimension dimension) {
1723
if (System.currentTimeMillis() - getDpiTime <= 1000) {
24+
if (dimension != null) {
25+
dimension.setSize(screenDimension);
26+
}
1827
return dpi;
1928
}
2029
getDpiTime = System.currentTimeMillis();
@@ -23,7 +32,15 @@ public static double getDpi() {
2332
try (var reader = new BufferedReader(new InputStreamReader(exec.getInputStream()))) {
2433
String dpiStr = reader.readLine();
2534
if (dpiStr != null) {
26-
dpi = Double.parseDouble(dpiStr);
35+
dpi = Float.parseFloat(dpiStr);
36+
}
37+
if (dimension != null) {
38+
String screenWidth = reader.readLine();
39+
int width = Integer.parseInt(screenWidth);
40+
String ScreenHeight = reader.readLine();
41+
int height = Integer.parseInt(ScreenHeight);
42+
dimension.setSize(width, height);
43+
screenDimension = new Dimension(width, height);
2744
}
2845
}
2946
return dpi;
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)