Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions native/cocos/platform/win32/modules/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@
namespace cc {

int Screen::getDPI() const {
// 参考:https://learn.microsoft.com/zh-cn/windows/win32/api/wingdi/nf-wingdi-getdevicecaps
static int dpi = -1;
if (dpi == -1) {

dpi = 96;
HDC hScreenDC = GetDC(nullptr);
int PixelsX = GetDeviceCaps(hScreenDC, HORZRES);
int MMX = GetDeviceCaps(hScreenDC, HORZSIZE);
ReleaseDC(nullptr, hScreenDC);
dpi = static_cast<int>(254.0f * PixelsX / MMX / 10);
if (hScreenDC) {
// LOGPIXELSX 对应水平方向每逻辑英寸的像素点数
dpi = GetDeviceCaps(hScreenDC, LOGPIXELSX);
ReleaseDC(nullptr, hScreenDC);
}
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);
Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Remove or document this commented alternative implementation. If GetDpiForWindow is preferred for Windows 10 1607+, consider implementing version detection and using the appropriate API.

Suggested change
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
**style:** Remove or document this commented alternative implementation. If `GetDpiForWindow` is preferred for Windows 10 1607+, consider implementing version detection and using the appropriate API.

```suggestion
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code or convert to a version-detection strategy if GetDpiForWindow is needed for Windows 10 1607+.

Suggested change
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);
// Note: GetDpiForWindow is available on Windows 10 1607+ and provides per-window DPI
// but requires runtime OS version detection. Current implementation uses GetDeviceCaps
// for broader compatibility.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
Remove commented code or convert to a version-detection strategy if `GetDpiForWindow` is needed for Windows 10 1607+.

```suggestion
        // Note: GetDpiForWindow is available on Windows 10 1607+ and provides per-window DPI
        // but requires runtime OS version detection. Current implementation uses GetDeviceCaps
        // for broader compatibility.
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code - it creates clutter and is tracked in version control history if needed

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
Remove commented code - it creates clutter and is tracked in version control history if needed

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

}
return dpi;
}

float Screen::getDevicePixelRatio() const {
return 1;
return getDPI() / 96.0f;
}

void Screen::setKeepScreenOn(bool value) {
Expand Down Expand Up @@ -73,4 +80,4 @@ void Screen::setDisplayStats(bool isShow) {
se::ScriptEngine::getInstance()->evalString(commandBuf);
}

} // namespace cc
} // namespace cc
16 changes: 10 additions & 6 deletions native/cocos/platform/win32/modules/SystemWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "engine/EngineEvents.h"
#include "platform/SDLHelper.h"
#include "platform/win32/WindowsPlatform.h"
#include "platform/interfaces/modules/IScreen.h"

namespace cc {
SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle)
Expand All @@ -45,27 +46,30 @@ SystemWindow::~SystemWindow() {

bool SystemWindow::createWindow(const char *title,
int w, int h, int flags) {
_window = SDLHelper::createWindow(title, w, h, flags);
float dpr = cc::BasePlatform::getPlatform()->getInterface<cc::IScreen>()->getDevicePixelRatio();
_width = w * dpr;
_height = h * dpr;
_window = SDLHelper::createWindow(title, _width, _height, flags);
if (!_window) {
return false;
}

_width = w;
_height = h;
_windowHandle = SDLHelper::getWindowHandle(_window);
return true;
}

bool SystemWindow::createWindow(const char *title,
int x, int y, int w,
int h, int flags) {
_window = SDLHelper::createWindow(title, x, y, w, h, flags);
float dpr = cc::BasePlatform::getPlatform()->getInterface<cc::IScreen>()->getDevicePixelRatio();
_width = w * dpr;
_height = h * dpr;

_window = SDLHelper::createWindow(title, x, y, _width, _height, flags);
if (!_window) {
return false;
}

_width = w;
_height = h;
_windowHandle = SDLHelper::getWindowHandle(_window);

return true;
Expand Down
4 changes: 4 additions & 0 deletions native/tools/simulator/frameworks/runtime-src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ else()
add_executable(${LIB_NAME} ${PROJ_SOURCES} ${PROJ_EXTRA_SOURCE})
endif()

if(WINDOWS AND MSVC)
set_property(TARGET ${LIB_NAME} PROPERTY VS_DPI_AWARE "PerMonitor")
endif()

target_link_libraries(${LIB_NAME} ${ENGINE_NAME} simulator)
target_include_directories(${LIB_NAME} PRIVATE
Classes
Expand Down
10 changes: 8 additions & 2 deletions pal/input/native/mouse-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export class MouseInputSource {
private _getLocation (event: jsb.MouseEvent): Vec2 {
const window = this._windowManager.getWindow(event.windowId);
const windowSize = window.getViewSize();
const dpr = screenAdapter.devicePixelRatio;
let dpr = screenAdapter.devicePixelRatio;
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
Comment on lines +120 to +121

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Touch input in touch-input.ts doesn't have the same Windows-specific DPR handling. Touch coordinates will be incorrectly scaled by DPR on Windows while mouse coordinates won't, causing inconsistent behavior.

Suggested change
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
if (systemInfo.os === OS.WINDOWS) {
dpr = 1; // Native Windows coordinates are already in physical pixels after DPI awareness is enabled
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: pal/input/native/mouse-input.ts
Line: 120:121

Comment:
**logic:** Touch input in `touch-input.ts` doesn't have the same Windows-specific DPR handling. Touch coordinates will be incorrectly scaled by DPR on Windows while mouse coordinates won't, causing inconsistent behavior.

```suggestion
        if (systemInfo.os === OS.WINDOWS) {
            dpr = 1; // Native Windows coordinates are already in physical pixels after DPI awareness is enabled
        }
```

How can I resolve this? If you propose a fix, please make it concise.

}
const x = event.x * dpr;
const y = windowSize.height - event.y * dpr;
return new Vec2(x, y);
Expand Down Expand Up @@ -187,7 +190,10 @@ export class MouseInputSource {
const eventMouse = new EventMouse(eventType, false, this._preMousePos, mouseEvent.windowId);
eventMouse.setLocation(location.x, location.y);
eventMouse.setButton(button);
const dpr = screenAdapter.devicePixelRatio;
let dpr = screenAdapter.devicePixelRatio;
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
}
Comment on lines +194 to +196

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Duplicated DPR adjustment logic - consider extracting to a helper method to avoid duplication and potential inconsistency

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: pal/input/native/mouse-input.ts
Line: 194:196

Comment:
**style:** Duplicated DPR adjustment logic - consider extracting to a helper method to avoid duplication and potential inconsistency

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

eventMouse.movementX = typeof mouseEvent.xDelta === 'undefined' ? 0 : mouseEvent.xDelta * dpr;
eventMouse.movementY = typeof mouseEvent.yDelta === 'undefined' ? 0 : mouseEvent.yDelta * dpr;
// update previous mouse position.
Expand Down
1 change: 1 addition & 0 deletions templates/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ cc_windows_before_target(${EXECUTABLE_NAME})
add_executable(${EXECUTABLE_NAME}
${CC_ALL_SOURCES}
)
set_property(TARGET ${EXECUTABLE_NAME} PROPERTY VS_DPI_AWARE "PerMonitor")
cc_windows_after_target(${EXECUTABLE_NAME})
Loading