• QT源码拾贝6-10(qwindowswindow)


    目录

    6  窗口激活相关的函数QWindowsForeignWindow::setParent

    7  QDebug运算符<<重载

    8  vscode的windows的变量提示很有意思

    9  windows常用类型名

    10  拿到所有窗体的方法QGuiApplication::allWindows()

    11  QSharedPointer智能指针使用


    上一篇博客:

    (175条消息) QT源码拾贝0-5(qimage和qpainter)_yantuguiguziPGJ的博客-CSDN博客

    6  窗口激活相关的函数QWindowsForeignWindow::setParent

    C:\Qt\6.5.0\Src\qtbase\src\plugins\platforms\windows\qwindowswindow.cpp

    1. void QWindowsForeignWindow::setParent(const QPlatformWindow *newParentWindow)
    2. {
    3. const bool wasTopLevel = isTopLevel_sys();
    4. const HWND newParent = newParentWindow ? reinterpret_cast(newParentWindow->winId()) : HWND(nullptr);
    5. const bool isTopLevel = !newParent;
    6. const DWORD oldStyle = style();
    7. qCDebug(lcQpaWindow) << __FUNCTION__ << window() << "newParent="
    8. << newParentWindow << newParent << "oldStyle=" << debugWinStyle(oldStyle);
    9. SetParent(m_hwnd, newParent);
    10. if (wasTopLevel != isTopLevel) { // Top level window flags need to be set/cleared manually.
    11. DWORD newStyle = oldStyle;
    12. if (isTopLevel) {
    13. newStyle = m_topLevelStyle;
    14. } else {
    15. m_topLevelStyle = oldStyle;
    16. newStyle &= ~(WS_OVERLAPPEDWINDOW | WS_POPUPWINDOW);
    17. newStyle |= WS_CHILD;
    18. }
    19. SetWindowLongPtr(m_hwnd, GWL_STYLE, newStyle);
    20. }
    21. }
    22. void WindowCreationData::fromWindow(const QWindow *w, const Qt::WindowFlags flagsIn,
    23. unsigned creationFlags)
    24. {
    25. flags = flagsIn;
    26. // Sometimes QWindow doesn't have a QWindow parent but does have a native parent window,
    27. // e.g. in case of embedded ActiveQt servers. They should not be considered a top-level
    28. // windows in such cases.
    29. QVariant prop = w->property(QWindowsWindow::embeddedNativeParentHandleProperty);
    30. if (prop.isValid()) {
    31. embedded = true;
    32. parentHandle = reinterpret_cast(prop.value());
    33. }
    34. if (creationFlags & ForceChild) {
    35. topLevel = false;
    36. } else if (embedded) {
    37. // Embedded native windows (for example Active X server windows) are by
    38. // definition never toplevel, even though they do not have QWindow parents.
    39. topLevel = false;
    40. } else {
    41. topLevel = (creationFlags & ForceTopLevel) ? true : w->isTopLevel();
    42. }
    43. if (topLevel)
    44. fixTopLevelWindowFlags(flags);
    45. type = static_cast(int(flags) & Qt::WindowType_Mask);
    46. switch (type) {
    47. case Qt::Dialog:
    48. case Qt::Sheet:
    49. dialog = true;
    50. break;
    51. case Qt::Drawer:
    52. case Qt::Tool:
    53. tool = true;
    54. break;
    55. case Qt::Popup:
    56. popup = true;
    57. break;
    58. default:
    59. break;
    60. }
    61. if ((flags & Qt::MSWindowsFixedSizeDialogHint))
    62. dialog = true;
    63. // This causes the title bar to drawn RTL and the close button
    64. // to be left. Note that this causes:
    65. // - All DCs created on the Window to have RTL layout (see SetLayout)
    66. // - ClientToScreen() and ScreenToClient() to work in reverse as well.
    67. // - Mouse event coordinates to be mirrored.
    68. // - Positioning of child Windows.
    69. if (QGuiApplication::layoutDirection() == Qt::RightToLeft
    70. && (QWindowsIntegration::instance()->options() & QWindowsIntegration::RtlEnabled) != 0) {
    71. exStyle |= WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;
    72. }
    73. // Parent: Use transient parent for top levels.
    74. if (popup) {
    75. flags |= Qt::WindowStaysOnTopHint; // a popup stays on top, no parent.
    76. } else if (!embedded) {
    77. if (const QWindow *parentWindow = topLevel ? w->transientParent() : w->parent())
    78. parentHandle = QWindowsWindow::handleOf(parentWindow);
    79. }
    80. if (popup || (type == Qt::ToolTip) || (type == Qt::SplashScreen)) {
    81. style = WS_POPUP;
    82. } else if (topLevel) {
    83. if (flags & Qt::FramelessWindowHint)
    84. style = WS_POPUP; // no border
    85. else if (flags & Qt::WindowTitleHint)
    86. style = WS_OVERLAPPED;
    87. else
    88. style = 0;
    89. } else {
    90. style = WS_CHILD;
    91. }
    92. // if (!testAttribute(Qt::WA_PaintUnclipped))
    93. // ### Commented out for now as it causes some problems, but
    94. // this should be correct anyway, so dig some more into this
    95. #ifdef Q_FLATTEN_EXPOSE
    96. if (windowIsOpenGL(w)) // a bit incorrect since the is-opengl status may change from false to true at any time later on
    97. style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; // see SetPixelFormat
    98. #else
    99. style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN ;
    100. #endif
    101. if (topLevel) {
    102. if ((type == Qt::Window || dialog || tool)) {
    103. if (!(flags & Qt::FramelessWindowHint)) {
    104. style |= WS_POPUP;
    105. if (flags & Qt::MSWindowsFixedSizeDialogHint) {
    106. style |= WS_DLGFRAME;
    107. } else {
    108. style |= WS_THICKFRAME;
    109. }
    110. if (flags & Qt::WindowTitleHint)
    111. style |= WS_CAPTION; // Contains WS_DLGFRAME
    112. }
    113. if (flags & Qt::WindowSystemMenuHint)
    114. style |= WS_SYSMENU;
    115. else if (dialog && (flags & Qt::WindowCloseButtonHint) && !(flags & Qt::FramelessWindowHint)) {
    116. style |= WS_SYSMENU | WS_BORDER; // QTBUG-2027, dialogs without system menu.
    117. exStyle |= WS_EX_DLGMODALFRAME;
    118. }
    119. const bool showMinimizeButton = flags & Qt::WindowMinimizeButtonHint;
    120. if (showMinimizeButton)
    121. style |= WS_MINIMIZEBOX;
    122. const bool showMaximizeButton = shouldShowMaximizeButton(w, flags);
    123. if (showMaximizeButton)
    124. style |= WS_MAXIMIZEBOX;
    125. if (showMinimizeButton || showMaximizeButton)
    126. style |= WS_SYSMENU;
    127. if (tool)
    128. exStyle |= WS_EX_TOOLWINDOW;
    129. if ((flags & Qt::WindowContextHelpButtonHint) && !showMinimizeButton
    130. && !showMaximizeButton)
    131. exStyle |= WS_EX_CONTEXTHELP;
    132. } else {
    133. exStyle |= WS_EX_TOOLWINDOW;
    134. }
    135. // make mouse events fall through this window
    136. // NOTE: WS_EX_TRANSPARENT flag can make mouse inputs fall through a layered window
    137. if (flagsIn & Qt::WindowTransparentForInput)
    138. exStyle |= WS_EX_LAYERED | WS_EX_TRANSPARENT;
    139. }
    140. }
    141. // Returns topmost QWindowsWindow ancestor even if there are embedded windows in the chain.
    142. // Returns this window if it is the topmost ancestor.
    143. QWindow *QWindowsWindow::topLevelOf(QWindow *w)
    144. {
    145. while (QWindow *parent = w->parent())
    146. w = parent;
    147. if (const QPlatformWindow *handle = w->handle()) {
    148. const auto *ww = static_cast<const QWindowsWindow *>(handle);
    149. if (ww->isEmbedded()) {
    150. HWND parentHWND = GetAncestor(ww->handle(), GA_PARENT);
    151. const HWND desktopHwnd = GetDesktopWindow();
    152. const QWindowsContext *ctx = QWindowsContext::instance();
    153. while (parentHWND && parentHWND != desktopHwnd) {
    154. if (QWindowsWindow *ancestor = ctx->findPlatformWindow(parentHWND))
    155. return topLevelOf(ancestor->window());
    156. parentHWND = GetAncestor(parentHWND, GA_PARENT);
    157. }
    158. }
    159. }
    160. return w;
    161. }

    C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\WinUser.h

    1. WINUSERAPI
    2. HWND
    3. WINAPI
    4. GetParent(
    5. _In_ HWND hWnd);
    6. WINUSERAPI
    7. HWND
    8. WINAPI
    9. SetParent(
    10. _In_ HWND hWndChild,
    11. _In_opt_ HWND hWndNewParent);
    12. WINUSERAPI
    13. BOOL
    14. WINAPI
    15. GetWindowPlacement(
    16. _In_ HWND hWnd,
    17. _Inout_ WINDOWPLACEMENT *lpwndpl);
    18. WINUSERAPI
    19. BOOL
    20. WINAPI
    21. SetWindowPlacement(
    22. _In_ HWND hWnd,
    23. _In_ CONST WINDOWPLACEMENT *lpwndpl);
    24. WINUSERAPI
    25. BOOL
    26. WINAPI
    27. DestroyWindow(
    28. _In_ HWND hWnd);
    29. WINUSERAPI
    30. BOOL
    31. WINAPI
    32. ShowWindow(
    33. _In_ HWND hWnd,
    34. _In_ int nCmdShow);
    35. /* Types use for passing & returning polymorphic values */
    36. typedef UINT_PTR WPARAM;
    37. typedef LONG_PTR LPARAM;
    38. typedef LONG_PTR LRESULT;

    QDebug运算符<<重载

    1. #ifndef QT_NO_DEBUG_STREAM
    2. QDebug operator<<(QDebug d, const RECT &r)
    3. {
    4. QDebugStateSaver saver(d);
    5. d.nospace();
    6. d << "RECT(left=" << r.left << ", top=" << r.top
    7. << ", right=" << r.right << ", bottom=" << r.bottom
    8. << " (" << r.right - r.left << 'x' << r.bottom - r.top << "))";
    9. return d;
    10. }

    8  vscode的windows的变量提示很有意思

    1. void WindowCreationData::applyWindowFlags(HWND hwnd) const
    2. {
    3. // Keep enabled and visible from the current style.
    4. const LONG_PTR oldStyle = GetWindowLongPtr(hwnd, GWL_STYLE);
    5. const LONG_PTR oldExStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
    6. const LONG_PTR newStyle = style | (oldStyle & (WS_DISABLED|WS_VISIBLE));
    7. if (oldStyle != newStyle)
    8. SetWindowLongPtr(hwnd, GWL_STYLE, newStyle);
    9. const LONG_PTR newExStyle = exStyle;
    10. if (newExStyle != oldExStyle)
    11. SetWindowLongPtr(hwnd, GWL_EXSTYLE, newExStyle);
    12. qCDebug(lcQpaWindow).nospace() << __FUNCTION__ << hwnd << *this
    13. << "\n Style from " << debugWinStyle(DWORD(oldStyle)) << "\n to "
    14. << debugWinStyle(DWORD(newStyle)) << "\n ExStyle from "
    15. << debugWinExStyle(DWORD(oldExStyle)) << " to "
    16. << debugWinExStyle(DWORD(newExStyle));
    17. }

    9  windows常用类型名

    C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\minwindef.h

    1. /****************************************************************************
    2. * *
    3. * minwindef.h -- Basic Windows Type Definitions for minwin partition *
    4. * *
    5. * Copyright (c) Microsoft Corporation. All rights reserved. *
    6. * *
    7. ****************************************************************************/
    8. #ifndef _MINWINDEF_
    9. #define _MINWINDEF_
    10. #pragma once
    11. #include
    12. #include
    13. #pragma region Application Family or OneCore Family or Games Family
    14. #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES)
    15. #ifndef NO_STRICT
    16. #ifndef STRICT
    17. #define STRICT 1
    18. #endif
    19. #endif /* NO_STRICT */
    20. // Win32 defines _WIN32 automatically,
    21. // but Macintosh doesn't, so if we are using
    22. // Win32 Functions, we must do it here
    23. #ifdef _MAC
    24. #ifndef _WIN32
    25. #define _WIN32
    26. #endif
    27. #endif //_MAC
    28. #ifndef WIN32
    29. #define WIN32
    30. #endif
    31. #ifdef __cplusplus
    32. extern "C" {
    33. #endif
    34. /*
    35. * BASETYPES is defined in ntdef.h if these types are already defined
    36. */
    37. #ifndef BASETYPES
    38. #define BASETYPES
    39. typedef unsigned long ULONG;
    40. typedef ULONG *PULONG;
    41. typedef unsigned short USHORT;
    42. typedef USHORT *PUSHORT;
    43. typedef unsigned char UCHAR;
    44. typedef UCHAR *PUCHAR;
    45. typedef _Null_terminated_ char *PSZ;
    46. #endif /* !BASETYPES */
    47. #define MAX_PATH 260
    48. #ifndef NULL
    49. #ifdef __cplusplus
    50. #define NULL 0
    51. #else
    52. #define NULL ((void *)0)
    53. #endif
    54. #endif
    55. #ifndef FALSE
    56. #define FALSE 0
    57. #endif
    58. #ifndef TRUE
    59. #define TRUE 1
    60. #endif
    61. #ifndef IN
    62. #define IN
    63. #endif
    64. #ifndef OUT
    65. #define OUT
    66. #endif
    67. #ifndef OPTIONAL
    68. #define OPTIONAL
    69. #endif
    70. #undef far
    71. #undef near
    72. #undef pascal
    73. #define far
    74. #define near
    75. #if (!defined(_MAC)) && ((_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED))
    76. #define pascal __stdcall
    77. #else
    78. #define pascal
    79. #endif
    80. #if defined(DOSWIN32) || defined(_MAC)
    81. #define cdecl _cdecl
    82. #ifndef CDECL
    83. #define CDECL _cdecl
    84. #endif
    85. #else
    86. #define cdecl
    87. #ifndef CDECL
    88. #define CDECL
    89. #endif
    90. #endif
    91. #ifdef _MAC
    92. #define CALLBACK PASCAL
    93. #define WINAPI CDECL
    94. #define WINAPIV CDECL
    95. #define APIENTRY WINAPI
    96. #define APIPRIVATE CDECL
    97. #ifdef _68K_
    98. #define PASCAL __pascal
    99. #else
    100. #define PASCAL
    101. #endif
    102. #elif (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)
    103. #define CALLBACK __stdcall
    104. #define WINAPI __stdcall
    105. #define WINAPIV __cdecl
    106. #define APIENTRY WINAPI
    107. #define APIPRIVATE __stdcall
    108. #define PASCAL __stdcall
    109. #else
    110. #define CALLBACK
    111. #define WINAPI
    112. #define WINAPIV
    113. #define APIENTRY WINAPI
    114. #define APIPRIVATE
    115. #define PASCAL pascal
    116. #endif
    117. #ifndef _M_CEE_PURE
    118. #ifndef WINAPI_INLINE
    119. #define WINAPI_INLINE WINAPI
    120. #endif
    121. #endif
    122. #undef FAR
    123. #undef NEAR
    124. #define FAR far
    125. #define NEAR near
    126. #ifndef CONST
    127. #define CONST const
    128. #endif
    129. typedef unsigned long DWORD;
    130. typedef int BOOL;
    131. typedef unsigned char BYTE;
    132. typedef unsigned short WORD;
    133. typedef float FLOAT;
    134. typedef FLOAT *PFLOAT;
    135. typedef BOOL near *PBOOL;
    136. typedef BOOL far *LPBOOL;
    137. typedef BYTE near *PBYTE;
    138. typedef BYTE far *LPBYTE;
    139. typedef int near *PINT;
    140. typedef int far *LPINT;
    141. typedef WORD near *PWORD;
    142. typedef WORD far *LPWORD;
    143. typedef long far *LPLONG;
    144. typedef DWORD near *PDWORD;
    145. typedef DWORD far *LPDWORD;
    146. typedef void far *LPVOID;
    147. typedef CONST void far *LPCVOID;
    148. typedef int INT;
    149. typedef unsigned int UINT;
    150. typedef unsigned int *PUINT;
    151. #ifndef NT_INCLUDED
    152. #include
    153. #endif /* NT_INCLUDED */
    154. /* Types use for passing & returning polymorphic values */
    155. typedef UINT_PTR WPARAM;
    156. typedef LONG_PTR LPARAM;
    157. typedef LONG_PTR LRESULT;
    158. #ifndef NOMINMAX
    159. #ifndef max
    160. #define max(a,b) (((a) > (b)) ? (a) : (b))
    161. #endif
    162. #ifndef min
    163. #define min(a,b) (((a) < (b)) ? (a) : (b))
    164. #endif
    165. #endif /* NOMINMAX */
    166. #define MAKEWORD(a, b) ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) | ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8))
    167. #define MAKELONG(a, b) ((LONG)(((WORD)(((DWORD_PTR)(a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(b)) & 0xffff))) << 16))
    168. #define LOWORD(l) ((WORD)(((DWORD_PTR)(l)) & 0xffff))
    169. #define HIWORD(l) ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
    170. #define LOBYTE(w) ((BYTE)(((DWORD_PTR)(w)) & 0xff))
    171. #define HIBYTE(w) ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))
    172. typedef HANDLE NEAR *SPHANDLE;
    173. typedef HANDLE FAR *LPHANDLE;
    174. typedef HANDLE HGLOBAL;
    175. typedef HANDLE HLOCAL;
    176. typedef HANDLE GLOBALHANDLE;
    177. typedef HANDLE LOCALHANDLE;
    178. #ifndef _MANAGED
    179. #if _MSC_VER >= 1200
    180. #pragma warning(push)
    181. #pragma warning(disable:4255) // () treated as (void)
    182. #endif
    183. #ifndef _MAC
    184. #ifdef _WIN64
    185. typedef INT_PTR (FAR WINAPI *FARPROC)();
    186. typedef INT_PTR (NEAR WINAPI *NEARPROC)();
    187. typedef INT_PTR (WINAPI *PROC)();
    188. #else
    189. typedef int (FAR WINAPI *FARPROC)();
    190. typedef int (NEAR WINAPI *NEARPROC)();
    191. typedef int (WINAPI *PROC)();
    192. #endif // _WIN64
    193. #else
    194. typedef int (CALLBACK *FARPROC)();
    195. typedef int (CALLBACK *NEARPROC)();
    196. typedef int (CALLBACK *PROC)();
    197. #endif
    198. #if _MSC_VER >= 1200
    199. #pragma warning(pop)
    200. #endif
    201. #else
    202. typedef INT_PTR (WINAPI *FARPROC)(void);
    203. typedef INT_PTR (WINAPI *NEARPROC)(void);
    204. typedef INT_PTR (WINAPI *PROC)(void);
    205. #endif
    206. typedef WORD ATOM; //BUGBUG - might want to remove this from minwin
    207. DECLARE_HANDLE(HKEY);
    208. typedef HKEY *PHKEY;
    209. DECLARE_HANDLE(HMETAFILE);
    210. DECLARE_HANDLE(HINSTANCE);
    211. typedef HINSTANCE HMODULE; /* HMODULEs can be used in place of HINSTANCEs */
    212. DECLARE_HANDLE(HRGN);
    213. DECLARE_HANDLE(HRSRC);
    214. DECLARE_HANDLE(HSPRITE);
    215. DECLARE_HANDLE(HLSURF);
    216. DECLARE_HANDLE(HSTR);
    217. DECLARE_HANDLE(HTASK);
    218. DECLARE_HANDLE(HWINSTA);
    219. DECLARE_HANDLE(HKL);
    220. #ifndef _MAC
    221. typedef int HFILE;
    222. #else
    223. typedef short HFILE;
    224. #endif
    225. //
    226. // File System time stamps are represented with the following structure:
    227. //
    228. typedef struct _FILETIME {
    229. DWORD dwLowDateTime;
    230. DWORD dwHighDateTime;
    231. } FILETIME, *PFILETIME, *LPFILETIME;
    232. #define _FILETIME_
    233. #ifdef __cplusplus
    234. }
    235. #endif
    236. #endif /* WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES) */
    237. #pragma endregion
    238. #endif // _MINWINDEF_
    1. //C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\minwindef.h
    2. typedef WORD ATOM; //BUGBUG - might want to remove this from minwin
    3. DECLARE_HANDLE(HKEY);
    4. typedef HKEY *PHKEY;
    5. DECLARE_HANDLE(HMETAFILE);
    6. DECLARE_HANDLE(HINSTANCE);
    7. typedef HINSTANCE HMODULE; /* HMODULEs can be used in place of HINSTANCEs */
    8. DECLARE_HANDLE(HRGN);
    9. DECLARE_HANDLE(HRSRC);
    10. DECLARE_HANDLE(HSPRITE);
    11. DECLARE_HANDLE(HLSURF);
    12. DECLARE_HANDLE(HSTR);
    13. DECLARE_HANDLE(HTASK);
    14. DECLARE_HANDLE(HWINSTA);
    15. DECLARE_HANDLE(HKL);
    16. //C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\winnt.h
    17. //
    18. // Handle to an Object
    19. //
    20. #ifdef STRICT
    21. typedef void *HANDLE;
    22. #if 0 && (_MSC_VER > 1000)
    23. #define DECLARE_HANDLE(name) struct name##__; typedef struct name##__ *name
    24. #else
    25. #define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
    26. #endif
    27. #else
    28. typedef PVOID HANDLE;
    29. #define DECLARE_HANDLE(name) typedef HANDLE name
    30. #endif
    31. typedef HANDLE *PHANDLE;

    10  拿到所有窗体的方法QGuiApplication::allWindows()

    1. void QWindowsWindow::handleWindowStateChange(Qt::WindowStates state)
    2. {
    3. qCDebug(lcQpaWindow) << __FUNCTION__ << this << window()
    4. << "\n from " << m_windowState << " to " << state;
    5. m_windowState = state;
    6. QWindowSystemInterface::handleWindowStateChanged(window(), state);
    7. if (state & Qt::WindowMinimized) {
    8. handleHidden();
    9. QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents); // Tell QQuickWindow to stop rendering now.
    10. } else {
    11. // QTBUG-17548: We send expose events when receiving WM_Paint, but for
    12. // layered windows and transient children, we won't receive any WM_Paint.
    13. QWindow *w = window();
    14. bool exposeEventsSent = false;
    15. if (isLayered()) {
    16. fireFullExpose();
    17. exposeEventsSent = true;
    18. }
    19. const QWindowList allWindows = QGuiApplication::allWindows();
    20. for (QWindow *child : allWindows) {
    21. if (child != w && child->isVisible() && child->transientParent() == w) {
    22. QWindowsWindow *platformWindow = QWindowsWindow::windowsWindowOf(child);
    23. if (platformWindow && platformWindow->isLayered()) {
    24. platformWindow->fireFullExpose();
    25. exposeEventsSent = true;
    26. }
    27. }
    28. }
    29. if (exposeEventsSent && !QWindowsContext::instance()->asyncExpose())
    30. QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
    31. }
    32. }
    33. /*!
    34. \brief Change the window state.
    35. \note Window frames change when maximized;
    36. the top margin shrinks somewhat but that cannot be obtained using
    37. AdjustWindowRectEx().
    38. \note Some calls to SetWindowLong require a subsequent call
    39. to ShowWindow.
    40. */
    41. void QWindowsWindow::setWindowState_sys(Qt::WindowStates newState)
    42. {
    43. const Qt::WindowStates oldState = m_windowState;
    44. if (oldState == newState)
    45. return;
    46. qCDebug(lcQpaWindow) << '>' << __FUNCTION__ << this << window()
    47. << " from " << oldState << " to " << newState;
    48. const bool visible = isVisible();
    49. auto stateChange = oldState ^ newState;
    50. if (stateChange & Qt::WindowFullScreen) {
    51. if (newState & Qt::WindowFullScreen) {
    52. #ifndef Q_FLATTEN_EXPOSE
    53. UINT newStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_POPUP;
    54. #else
    55. UINT newStyle = WS_POPUP;
    56. #endif
    57. // Save geometry and style to be restored when fullscreen
    58. // is turned off again, since on Windows, it is not a real
    59. // Window state but emulated by changing geometry and style.
    60. if (!m_savedStyle) {
    61. m_savedStyle = style();
    62. if ((oldState & Qt::WindowMinimized) || (oldState & Qt::WindowMaximized)) {
    63. const QRect nf = normalFrameGeometry(m_data.hwnd);
    64. if (nf.isValid())
    65. m_savedFrameGeometry = nf;
    66. } else {
    67. m_savedFrameGeometry = frameGeometry_sys();
    68. }
    69. }
    70. if (newState & Qt::WindowMaximized)
    71. setFlag(MaximizeToFullScreen);
    72. if (m_savedStyle & WS_SYSMENU)
    73. newStyle |= WS_SYSMENU;
    74. if (visible)
    75. newStyle |= WS_VISIBLE;
    76. if (testFlag(HasBorderInFullScreen))
    77. newStyle |= WS_BORDER;
    78. setStyle(newStyle);
    79. // Use geometry of QWindow::screen() within creation or the virtual screen the
    80. // window is in (QTBUG-31166, QTBUG-30724).
    81. const QScreen *screen = window()->screen();
    82. if (!screen)
    83. screen = QGuiApplication::primaryScreen();
    84. const QRect r = screen ? QHighDpi::toNativePixels(screen->geometry(), window()) : m_savedFrameGeometry;
    85. if (newState & Qt::WindowMinimized) {
    86. setMinimizedGeometry(m_data.hwnd, r);
    87. if (stateChange & Qt::WindowMaximized)
    88. setRestoreMaximizedFlag(m_data.hwnd, newState & Qt::WindowMaximized);
    89. } else {
    90. const UINT swpf = SWP_FRAMECHANGED | SWP_NOACTIVATE;
    91. const bool wasSync = testFlag(SynchronousGeometryChangeEvent);
    92. setFlag(SynchronousGeometryChangeEvent);
    93. SetWindowPos(m_data.hwnd, HWND_TOP, r.left(), r.top(), r.width(), r.height(), swpf);
    94. if (!wasSync)
    95. clearFlag(SynchronousGeometryChangeEvent);
    96. clearFlag(MaximizeToFullScreen);
    97. QWindowSystemInterface::handleGeometryChange(window(), r);
    98. QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
    99. }
    100. } else {
    101. // Restore saved state.
    102. unsigned newStyle = m_savedStyle ? m_savedStyle : style();
    103. if (visible)
    104. newStyle |= WS_VISIBLE;
    105. setStyle(newStyle);
    106. const QScreen *screen = window()->screen();
    107. if (!screen)
    108. screen = QGuiApplication::primaryScreen();
    109. // That area of the virtual desktop might not be covered by a screen anymore.
    110. if (const auto platformScreen = screen->handle()) {
    111. if (!platformScreen->geometry().intersects(m_savedFrameGeometry))
    112. m_savedFrameGeometry.moveTo(platformScreen->geometry().topLeft());
    113. }
    114. if (newState & Qt::WindowMinimized) {
    115. setMinimizedGeometry(m_data.hwnd, m_savedFrameGeometry);
    116. if (stateChange & Qt::WindowMaximized)
    117. setRestoreMaximizedFlag(m_data.hwnd, newState & Qt::WindowMaximized);
    118. } else {
    119. UINT swpf = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
    120. if (!m_savedFrameGeometry.isValid())
    121. swpf |= SWP_NOSIZE | SWP_NOMOVE;
    122. const bool wasSync = testFlag(SynchronousGeometryChangeEvent);
    123. setFlag(SynchronousGeometryChangeEvent);
    124. // After maximized/fullscreen; the window can be in a maximized state. Clear
    125. // it before applying the normal geometry.
    126. if (windowVisibility_sys(m_data.hwnd) == QWindow::Maximized)
    127. ShowWindow(m_data.hwnd, SW_SHOWNOACTIVATE);
    128. SetWindowPos(m_data.hwnd, nullptr, m_savedFrameGeometry.x(), m_savedFrameGeometry.y(),
    129. m_savedFrameGeometry.width(), m_savedFrameGeometry.height(), swpf);
    130. if (!wasSync)
    131. clearFlag(SynchronousGeometryChangeEvent);
    132. // preserve maximized state
    133. if (visible) {
    134. setFlag(WithinMaximize);
    135. ShowWindow(m_data.hwnd,
    136. (newState & Qt::WindowMaximized) ? SW_MAXIMIZE : SW_SHOWNA);
    137. clearFlag(WithinMaximize);
    138. }
    139. }
    140. m_savedStyle = 0;
    141. m_savedFrameGeometry = QRect();
    142. }
    143. } else if ((oldState & Qt::WindowMaximized) != (newState & Qt::WindowMaximized)) {
    144. if (visible && !(newState & Qt::WindowMinimized)) {
    145. setFlag(WithinMaximize);
    146. if (newState & Qt::WindowFullScreen)
    147. setFlag(MaximizeToFullScreen);
    148. ShowWindow(m_data.hwnd,
    149. (newState & Qt::WindowMaximized) ? SW_MAXIMIZE : SW_SHOWNOACTIVATE);
    150. clearFlag(WithinMaximize);
    151. clearFlag(MaximizeToFullScreen);
    152. } else if (visible && (oldState & newState & Qt::WindowMinimized)) {
    153. // change of the maximized state while keeping minimized
    154. setRestoreMaximizedFlag(m_data.hwnd, newState & Qt::WindowMaximized);
    155. }
    156. }
    157. if (stateChange & Qt::WindowMinimized) {
    158. if (visible) {
    159. ShowWindow(m_data.hwnd,
    160. (newState & Qt::WindowMinimized) ? SW_MINIMIZE :
    161. (newState & Qt::WindowMaximized) ? SW_MAXIMIZE : SW_SHOWNORMAL);
    162. if ((newState & Qt::WindowMinimized) && (stateChange & Qt::WindowMaximized))
    163. setRestoreMaximizedFlag(m_data.hwnd, newState & Qt::WindowMaximized);
    164. }
    165. }
    166. qCDebug(lcQpaWindow) << '<' << __FUNCTION__ << this << window() << newState;
    167. }

    11  QSharedPointer智能指针使用

    1. using QWindowCreationContextPtr = QSharedPointer;
    2. static QPoint calcPosition(const QWindow *w, const QWindowCreationContextPtr &context, const QMargins &invMargins)
    3. const QWindowCreationContextPtr context(new QWindowCreationContext(w, screen, data.geometry,
    4. rect, data.customMargins,
    5. style, exStyle));
    6. // Clear the creation context as the window can be found in QWindowsContext's map.
    7. QWindowCreationContextPtr creationContext =
    8. QWindowsContext::instance()->setWindowCreationContext(QWindowCreationContextPtr());

  • 相关阅读:
    C语言入门Day_18 判断和循坏的小结
    P11机器学习--李宏毅笔记(Transformer Decoder)Testing部分
    【EasyRL学习笔记】第十二章 Deep Deterministic Policy Gradient 深度确定性策略梯度(DDPG)算法
    vue3 el-table多级表头收缩扩展的实现
    Fortinet Universal SASE能力再度进化!保障用户访问任意应用程序便捷无忧
    从简历被拒到收割8个大厂offer,我用了3个月成功破茧成蝶
    Java注解(Annotation)与元注解
    Java 世界破破烂烂,电音小猫缝缝补补
    比较器和运放
    redis在实际项目作用
  • 原文地址:https://blog.csdn.net/yantuguiguziPGJ/article/details/128124218