04-01-22, 01:12 PM
(آخر تعديل لهذه المشاركة : 04-01-22, 01:18 PM {2} بواسطة معاند الحظ.)
بسم الله الرحمن الرحيم
مشروع برنامج لتثبيت اي نافذة برنامج اعلى الشاشة او ازالتها
جعل النافذة في وضع TopMost او TopScreen
مرحبا بكم اخواني الى هذا الموضوع الذي اكشف فيه عن بعض العقد النفسية (اشياء اكرهها)
والتي اواجهها اثناء العمل على جهاز الحاسوب
فانا ككثيرين غيري لابد وان يوجد شيء في نظام التشغيل اود اضافته او ازالته او التحكم فيه
في كلا الحالتين وغيرها
ومن هذه الاشياء (العقد) اني اكره ان يتم تغطية بعض البرامج وجعلها في اسفل ترتيب ZOrder
للنوافذ مثل برنامج داونلود مانيجر فانا ارغب ان تبقى نافذته دائما في الاعلى
اثناء التحميل
وهنا فيديو للتوضيح
لذا فقد قمت بعمل هذا البرنامج الذي يقوم باضهار كل نوافذ البرامج قيد التشغيل
في قائمة ListView مع اضهار حالة النافذة سواء كانت TopScreen او لا
ولهذا فقد استعملت المكتبات التالية
اولا: مكتبة GetWindowLong
PHP كود :
Const GWL_EXSTYLE As Integer = -20
Const WS_EX_TOPMOST As Integer = &H8
<DllImport("user32.dll", SetLastError:=True)> _
Shared Function GetWindowLong(hWnd As IntPtr, nIndex As Integer) As Integer
End Function
والتي استخدمها لمعرفة حالة نافذة البرنامج الخارجي سواء كانت TopScreen او لا
باستخدام الوضيفة التالية :IsWindowTopMost
PHP كود :
''' <summary>
''' Get the external window state
''' </summary>
''' <param name="hWnd"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function IsWindowTopMost(hWnd As IntPtr) As Boolean
Dim exStyle As Integer = GetWindowLong(hWnd, GWL_EXSTYLE)
Return (exStyle And WS_EX_TOPMOST) = WS_EX_TOPMOST
End Function
وهذه الوضيفة تعيد عند استدعائها حالة النافذة
ثانيا: المكتبة SetWindowPos
PHP كود :
Private ReadOnly HWND_BOTTOM As New IntPtr(1)
Private ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Private ReadOnly HWND_TOP As New IntPtr(0)
Private ReadOnly HWND_TOPMOST As New IntPtr(-1)
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As SetWindowPosFlags) As Boolean
End Function
<Flags()> _
Private Enum SetWindowPosFlags As UInteger
''' <summary>If the calling thread and the thread that owns the window are attached to different input queues,
''' the system posts the request to the thread that owns the window. This prevents the calling thread from
''' blocking its execution while other threads process the request.</summary>
''' <remarks>SWP_ASYNCWINDOWPOS</remarks>
SynchronousWindowPosition = &H4000
''' <summary>Prevents generation of the WM_SYNCPAINT message.</summary>
''' <remarks>SWP_DEFERERASE</remarks>
DeferErase = &H2000
''' <summary>Draws a frame (defined in the window's class description) around the window.</summary>
''' <remarks>SWP_DRAWFRAME</remarks>
DrawFrame = &H20
''' <summary>Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to
''' the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE
''' is sent only when the window's size is being changed.</summary>
''' <remarks>SWP_FRAMECHANGED</remarks>
FrameChanged = &H20
''' <summary>Hides the window.</summary>
''' <remarks>SWP_HIDEWINDOW</remarks>
HideWindow = &H80
''' <summary>Does not activate the window. If this flag is not set, the window is activated and moved to the
''' top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter
''' parameter).</summary>
''' <remarks>SWP_NOACTIVATE</remarks>
DoNotActivate = &H10
''' <summary>Discards the entire contents of the client area. If this flag is not specified, the valid
''' contents of the client area are saved and copied back into the client area after the window is sized or
''' repositioned.</summary>
''' <remarks>SWP_NOCOPYBITS</remarks>
DoNotCopyBits = &H100
''' <summary>Retains the current position (ignores X and Y parameters).</summary>
''' <remarks>SWP_NOMOVE</remarks>
IgnoreMove = &H2
''' <summary>Does not change the owner window's position in the Z order.</summary>
''' <remarks>SWP_NOOWNERZORDER</remarks>
DoNotChangeOwnerZOrder = &H200
''' <summary>Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to
''' the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent
''' window uncovered as a result of the window being moved. When this flag is set, the application must
''' explicitly invalidate or redraw any parts of the window and parent window that need redrawing.</summary>
''' <remarks>SWP_NOREDRAW</remarks>
DoNotRedraw = &H8
''' <summary>Same as the SWP_NOOWNERZORDER flag.</summary>
''' <remarks>SWP_NOREPOSITION</remarks>
DoNotReposition = &H200
''' <summary>Prevents the window from receiving the WM_WINDOWPOSCHANGING message.</summary>
''' <remarks>SWP_NOSENDCHANGING</remarks>
DoNotSendChangingEvent = &H400
''' <summary>Retains the current size (ignores the cx and cy parameters).</summary>
''' <remarks>SWP_NOSIZE</remarks>
IgnoreResize = &H1
''' <summary>Retains the current Z order (ignores the hWndInsertAfter parameter).</summary>
''' <remarks>SWP_NOZORDER</remarks>
IgnoreZOrder = &H4
''' <summary>Displays the window.</summary>
''' <remarks>SWP_SHOWWINDOW</remarks>
ShowWindow = &H40
End Enum
وهي تستخدم كالتالي:
PHP كود :
Dim hwnd As IntPtr = ' the handle of window
Dim theFlags As String = SetWindowPosFlags.IgnoreMove Or SetWindowPosFlags.IgnoreResize 'Or SetWindowPosFlags.ShowWindow
Make the window TopMost'
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, theFlags)
Make the window not TopMost'
SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, theFlags)
هذا تقريبا كل شيء
اخيرا اليكم الكود الكامل للبرنامج لمن لايريد تحميل المثال المرفق
PHP كود :
Imports System.Runtime.InteropServices
Public Class Form1
#Region " Api..........................."
Const GWL_EXSTYLE As Integer = -20
Const WS_EX_TOPMOST As Integer = &H8
<DllImport("user32.dll", SetLastError:=True)> _
Shared Function GetWindowLong(hWnd As IntPtr, nIndex As Integer) As Integer
End Function
''' <summary>
''' Get the external window state
''' </summary>
''' <param name="hWnd"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function IsWindowTopMost(hWnd As IntPtr) As Boolean
Dim exStyle As Integer = GetWindowLong(hWnd, GWL_EXSTYLE)
Return (exStyle And WS_EX_TOPMOST) = WS_EX_TOPMOST
End Function
Private ReadOnly HWND_BOTTOM As New IntPtr(1)
Private ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Private ReadOnly HWND_TOP As New IntPtr(0)
Private ReadOnly HWND_TOPMOST As New IntPtr(-1)
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As SetWindowPosFlags) As Boolean
End Function
<Flags()> _
Private Enum SetWindowPosFlags As UInteger
''' <summary>If the calling thread and the thread that owns the window are attached to different input queues,
''' the system posts the request to the thread that owns the window. This prevents the calling thread from
''' blocking its execution while other threads process the request.</summary>
''' <remarks>SWP_ASYNCWINDOWPOS</remarks>
SynchronousWindowPosition = &H4000
''' <summary>Prevents generation of the WM_SYNCPAINT message.</summary>
''' <remarks>SWP_DEFERERASE</remarks>
DeferErase = &H2000
''' <summary>Draws a frame (defined in the window's class description) around the window.</summary>
''' <remarks>SWP_DRAWFRAME</remarks>
DrawFrame = &H20
''' <summary>Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to
''' the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE
''' is sent only when the window's size is being changed.</summary>
''' <remarks>SWP_FRAMECHANGED</remarks>
FrameChanged = &H20
''' <summary>Hides the window.</summary>
''' <remarks>SWP_HIDEWINDOW</remarks>
HideWindow = &H80
''' <summary>Does not activate the window. If this flag is not set, the window is activated and moved to the
''' top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter
''' parameter).</summary>
''' <remarks>SWP_NOACTIVATE</remarks>
DoNotActivate = &H10
''' <summary>Discards the entire contents of the client area. If this flag is not specified, the valid
''' contents of the client area are saved and copied back into the client area after the window is sized or
''' repositioned.</summary>
''' <remarks>SWP_NOCOPYBITS</remarks>
DoNotCopyBits = &H100
''' <summary>Retains the current position (ignores X and Y parameters).</summary>
''' <remarks>SWP_NOMOVE</remarks>
IgnoreMove = &H2
''' <summary>Does not change the owner window's position in the Z order.</summary>
''' <remarks>SWP_NOOWNERZORDER</remarks>
DoNotChangeOwnerZOrder = &H200
''' <summary>Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to
''' the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent
''' window uncovered as a result of the window being moved. When this flag is set, the application must
''' explicitly invalidate or redraw any parts of the window and parent window that need redrawing.</summary>
''' <remarks>SWP_NOREDRAW</remarks>
DoNotRedraw = &H8
''' <summary>Same as the SWP_NOOWNERZORDER flag.</summary>
''' <remarks>SWP_NOREPOSITION</remarks>
DoNotReposition = &H200
''' <summary>Prevents the window from receiving the WM_WINDOWPOSCHANGING message.</summary>
''' <remarks>SWP_NOSENDCHANGING</remarks>
DoNotSendChangingEvent = &H400
''' <summary>Retains the current size (ignores the cx and cy parameters).</summary>
''' <remarks>SWP_NOSIZE</remarks>
IgnoreResize = &H1
''' <summary>Retains the current Z order (ignores the hWndInsertAfter parameter).</summary>
''' <remarks>SWP_NOZORDER</remarks>
IgnoreZOrder = &H4
''' <summary>Displays the window.</summary>
''' <remarks>SWP_SHOWWINDOW</remarks>
ShowWindow = &H40
End Enum
#End Region
Dim checkedItems As Dictionary(Of Integer, Boolean)
Dim Status As String
Dim AlwaysOnTop As Boolean
Dim MinimizeAfterEvents As Boolean
Dim AlwaysResetWindows As Boolean
Dim SelIndex As Integer
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If chkAlwaysReset.Checked Then
For Each item As ListViewItem In ListView1.Items
Dim hwnd As IntPtr = item.SubItems(2).Text
Dim theFlags As String = SetWindowPosFlags.IgnoreMove Or SetWindowPosFlags.IgnoreResize 'Or SetWindowPosFlags.ShowWindow
SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, theFlags)
Next
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
checkedItems = New Dictionary(Of Integer, Boolean)
Timer1.Enabled = True
ListView1.CheckBoxes = True
'ListView1.DefaultForeColor = Color.Azure
AlwaysOnTop = My.Settings.AlwaysOnTop
chkAlwaysOnTop.Checked = AlwaysOnTop
MinimizeAfterEvents = My.Settings.MinimizeAfterEvents
chkMinimize.Checked = MinimizeAfterEvents
AlwaysResetWindows = My.Settings.AlwaysResetWindows
chkAlwaysReset.Checked = AlwaysResetWindows
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static i As Integer
ListView1.SuspendLayout()
ListView1.Items.Clear()
i = 0
For Each proc As Process In Process.GetProcesses
If proc.MainWindowTitle <> "" Then
If proc.MainWindowTitle <> Me.Text Then
Dim itemHandle As New ListViewItem
Dim itemCounter As New ListViewItem
Dim itemStatus As New ListViewItem
If IsWindowTopMost(proc.MainWindowHandle) Then
Status = "TopMost"
itemStatus.ForeColor = Color.Green
Else
Status = "Normal"
End If
itemStatus.Text = Status
Dim newItem As New ListViewItem
newItem.Text = i + 1
If checkedItems.Keys.Contains(i) Then
newItem.Checked = checkedItems.Values(i)
Else
checkedItems.Add(i, False)
End If
itemCounter.SubItems(0).Text = (proc.MainWindowTitle)
itemHandle.SubItems(0).Text = (proc.MainWindowHandle)
newItem.UseItemStyleForSubItems = False
newItem.SubItems.Add(itemCounter.SubItems(0))
newItem.SubItems.Add(itemHandle.SubItems(0))
newItem.SubItems.Add(itemStatus.SubItems(0))
ListView1.Items.Add(newItem)
i += 1
End If
End If
Next
ListView1.ResumeLayout()
End Sub
Private Sub btnTopMost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTopMost.Click
For Each itm As ListViewItem In ListView1.Items
If itm.Checked Then
Dim hwnd As IntPtr = itm.SubItems(2).Text
Dim theFlags As String = SetWindowPosFlags.IgnoreMove Or SetWindowPosFlags.IgnoreResize Or SetWindowPosFlags.ShowWindow
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, theFlags)
itm.Checked = False
checkedItems(itm.Index) = False
If chkMinimize.Checked Then
Me.WindowState = FormWindowState.Minimized
Else
Me.WindowState = FormWindowState.Normal
Me.BringToFront()
End If
End If
Next
End Sub
Private Sub btnNotTopMost_Click(sender As System.Object, e As System.EventArgs) Handles btnNotTopMost.Click
For Each itm As ListViewItem In ListView1.Items
If itm.Checked = True Then
Dim hwnd As IntPtr = itm.SubItems(2).Text
Dim theFlags As String = SetWindowPosFlags.IgnoreMove Or SetWindowPosFlags.IgnoreResize Or SetWindowPosFlags.ShowWindow
SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, theFlags)
itm.Checked = False
checkedItems(itm.Index) = False
If chkMinimize.Checked Then
Me.WindowState = FormWindowState.Minimized
Else
Me.WindowState = FormWindowState.Normal
Me.BringToFront()
End If
End If
Next
End Sub
Private Sub btnResetAll_Click(sender As System.Object, e As System.EventArgs) Handles btnResetAll.Click
For Each item As ListViewItem In ListView1.Items
Dim hwnd As IntPtr = item.SubItems(2).Text
Dim theFlags As String = SetWindowPosFlags.IgnoreMove Or SetWindowPosFlags.IgnoreResize Or SetWindowPosFlags.ShowWindow
SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, theFlags)
Next
End Sub
Private Sub ListView1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Left Then
If Not ListView1.Items(SelIndex).Checked Then
ListView1.Items(SelIndex).Checked = True
If checkedItems.Keys.Contains(ListView1.Items(SelIndex).Index) Then
checkedItems(ListView1.Items(SelIndex).Index) = True
Else
checkedItems.Add(ListView1.Items(SelIndex).Index, True)
End If
Else
ListView1.Items(SelIndex).Checked = False
If checkedItems.Keys.Contains(ListView1.Items(SelIndex).Index) Then
checkedItems(ListView1.Items(SelIndex).Index) = False
Else
checkedItems.Add(ListView1.Items(SelIndex).Index, False)
End If
End If
End If
End Sub
Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
SelIndex = ListView1.SelectedItems(0).Index
End Sub
Private Sub ShowToolStrip_Click(sender As System.Object, e As System.EventArgs) Handles ShowToolStrip.Click
Me.WindowState = FormWindowState.Normal
End Sub
Private Sub HideToolStrip_Click(sender As System.Object, e As System.EventArgs) Handles HideToolStrip.Click
Me.ShowInTaskbar = False
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub CloseToolStrip_Click(sender As System.Object, e As System.EventArgs) Handles CloseToolStrip.Click
Application.Exit()
End Sub
Private Sub chkAlwaysOnTop_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkAlwaysOnTop.CheckedChanged
Dim hwnd As IntPtr = Me.Handle
Dim theFlags As String = SetWindowPosFlags.IgnoreMove Or SetWindowPosFlags.IgnoreResize Or SetWindowPosFlags.ShowWindow
If chkAlwaysOnTop.Checked Then
My.Settings.AlwaysOnTop = True
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, theFlags)
Else
My.Settings.AlwaysOnTop = False
SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, theFlags)
End If
My.Settings.Save()
End Sub
Private Sub chkMinimize_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkMinimize.CheckedChanged
If chkMinimize.Checked Then
My.Settings.MinimizeAfterEvents = True
Else
My.Settings.MinimizeAfterEvents = False
End If
My.Settings.Save()
End Sub
Private Sub chkAlwaysReset_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkAlwaysReset.CheckedChanged
If chkAlwaysReset.Checked Then
My.Settings.AlwaysResetWindows = True
Else
My.Settings.AlwaysResetWindows = False
End If
My.Settings.Save()
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.ShowInTaskbar = True
Me.Show()
End If
End Sub
End Class
اخيرا تحميل البرنامج
PinTopScreen.zip (الحجم : 452.44 ك ب / التحميلات : 154)