PDA

View Full Version : سوال: حذف نوار عنوان و خصوط حاشیه یک برنامه خارج از برنامه اصلی توسط توابع API



ROSTAM2
یک شنبه 15 خرداد 1401, 15:56 عصر
سلام.
من می خوام نوار عنوان و خطوط حاشیه پنجره یک برنامه خارج از برنامه اصلی مثل Notepad یا یک console app رو با کدهای API حذف کنم.
دستور موجود هیچ تغییراتی روی پنجره ایجاد نمی کنه:

ثابت ها و توابع API:


Public Const GWL_STYLE As Short = -16
Public Const WS_BORDER = &H800000

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long




متود سفارشی:


Sub SetWindowBorder(Handle As IntPtr)
Dim lStyle As Long
lStyle = GetWindowLong(Handle, GWL_STYLE)
lStyle = lStyle And Not WS_BORDER
SetWindowLong(Handle, GWL_STYLE, lStyle)
End Sub



مشکل از کجاست؟

mazoolagh
دوشنبه 16 خرداد 1401, 08:24 صبح
سلام و روز خوش

در همین منبع اصلی:
pinvoke.net: GetWindowLongPtr (user32) (https://www.pinvoke.net/default.aspx/user32/GetWindowLongPtr.html)

اومده که برای ویندوز 64 بیت باید از این تابع استفاده کنین.

mazoolagh
دوشنبه 16 خرداد 1401, 08:24 صبح
<DllImport("user32.dll", EntryPoint:="SetWindowLong")>
Public Function SetWindowLong32(
ByVal hWnd As IntPtr,
<MarshalAs(UnmanagedType.I4)> nIndex As WindowLongFlags,
ByVal dwNewLong As Integer) As Integer
End Function


<DllImport("user32.dll", EntryPoint:="SetWindowLongPtr")>
Public Function SetWindowLongPtr64(
ByVal hWnd As IntPtr,
<MarshalAs(UnmanagedType.I4)> nIndex As WindowLongFlags,
ByVal dwNewLong As IntPtr) As IntPtr
End Function


Public Function SetWindowLongPtr(ByVal hWnd As IntPtr, nIndex As WindowLongFlags, ByVal dwNewLong As IntPtr) As IntPtr
If IntPtr.Size = 8 Then
Return SetWindowLongPtr64(hWnd, nIndex, dwNewLong)
Else
Return New IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32))
End If
End Function

<DllImport("user32.dll", EntryPoint:="GetWindowLong")>
Public Function GetWindowLongPtr32(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
End Function


<DllImport("user32.dll", EntryPoint:="GetWindowLongPtr")>
Public Function GetWindowLongPtr64(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
End Function


Public Function GetWindowLongPtr(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
If IntPtr.Size = 8 Then
Return GetWindowLongPtr64(hWnd, nIndex)
Else
Return GetWindowLongPtr32(hWnd, nIndex)
End If
End Function

mazoolagh
دوشنبه 16 خرداد 1401, 08:25 صبح
Public Enum WindowLongFlags As Integer
GWL_EXSTYLE = -20
GWLP_HINSTANCE = -6
GWLP_HWNDPARENT = -8
GWL_ID = -12
GWL_STYLE = -16
GWL_USERDATA = -21
GWL_WNDPROC = -4
DWLP_USER = &H8
DWLP_MSGRESULT = &H0
DWLP_DLGPROC = &H4
End Enum

mazoolagh
دوشنبه 16 خرداد 1401, 08:26 صبح
Public Enum GWL
GWL_WNDPROC = -4
GWL_HINSTANCE = -6
GWL_HWNDPARENT = -8
GWL_STYLE = -16
GWL_EXSTYLE = -20
GWL_USERDATA = -21
GWL_ID = -12
End Enum

mazoolagh
دوشنبه 16 خرداد 1401, 08:28 صبح
''' <summary>
''' Window Styles.
''' The following styles can be specified wherever a window style is required. After the control has been created, these styles cannot be modified, except as noted.
''' </summary>
<Flags()> Public Enum WindowStyles As UInteger
''' <summary>The window has a thin-line border.</summary>
WS_BORDER = &H800000


''' <summary>The window has a title bar (includes the WS_BORDER style).</summary>
WS_CAPTION = &HC00000


''' <summary>The window is a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style.</summary>
WS_CHILD = &H40000000


''' <summary>Excludes the area occupied by child windows when drawing occurs within the parent window. This style is used when creating the parent window.</summary>
WS_CLIPCHILDREN = &H2000000


''' <summary>
''' Clips child windows relative to each other; that is, when a particular child window receives a WM_PAINT message, the WS_CLIPSIBLINGS style clips all other overlapping child windows out of the region of the child window to be updated.
''' If WS_CLIPSIBLINGS is not specified and child windows overlap, it is possible, when drawing within the client area of a child window, to draw within the client area of a neighboring child window.
''' </summary>
WS_CLIPSIBLINGS = &H4000000


''' <summary>The window is initially disabled. A disabled window cannot receive input from the user. To change this after a window has been created, use the EnableWindow function.</summary>
WS_DISABLED = &H8000000


''' <summary>The window has a border of a style typically used with dialog boxes. A window with this style cannot have a title bar.</summary>
WS_DLGFRAME = &H400000


''' <summary>
''' The window is the first control of a group of controls. The group consists of this first control and all controls defined after it, up to the next control with the WS_GROUP style.
''' The first control in each group usually has the WS_TABSTOP style so that the user can move from group to group. The user can subsequently change the keyboard focus from one control in the group to the next control in the group by using the direction keys.
''' You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use the SetWindowLong function.
''' </summary>
WS_GROUP = &H20000


''' <summary>The window has a horizontal scroll bar.</summary>
WS_HSCROLL = &H100000


''' <summary>The window is initially maximized.</summary>
WS_MAXIMIZE = &H1000000


''' <summary>The window has a maximize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.</summary>
WS_MAXIMIZEBOX = &H10000


''' <summary>The window is initially minimized.</summary>
WS_MINIMIZE = &H20000000


''' <summary>The window has a minimize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.</summary>
WS_MINIMIZEBOX = &H20000


''' <summary>The window is an overlapped window. An overlapped window has a title bar and a border.</summary>
WS_OVERLAPPED = &H0


''' <summary>The window is an overlapped window.</summary>
WS_OVERLAPPEDWINDOW = WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_SIZEFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX


''' <summary>The window is a pop-up window. This style cannot be used with the WS_CHILD style.</summary>
WS_POPUP = &H80000000UI


''' <summary>The window is a pop-up window. The WS_CAPTION and WS_POPUPWINDOW styles must be combined to make the window menu visible.</summary>
WS_POPUPWINDOW = WS_POPUP Or WS_BORDER Or WS_SYSMENU


''' <summary>The window has a sizing border.</summary>
WS_SIZEFRAME = &H40000


''' <summary>The window has a window menu on its title bar. The WS_CAPTION style must also be specified.</summary>
WS_SYSMENU = &H80000


''' <summary>
''' The window is a control that can receive the keyboard focus when the user presses the TAB key.
''' Pressing the TAB key changes the keyboard focus to the next control with the WS_TABSTOP style.
''' You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use the SetWindowLong function.
''' For user-created windows and modeless dialogs to work with tab stops, alter the message loop to call the IsDialogMessage function.
''' </summary>
WS_TABSTOP = &H10000


''' <summary>The window is initially visible. This style can be turned on and off by using the ShowWindow or SetWindowPos function.</summary>
WS_VISIBLE = &H10000000


''' <summary>The window has a vertical scroll bar.</summary>
WS_VSCROLL = &H200000


WS_NOBORDER = Not (WS_BORDER Or WindowStyles.WS_SIZEFRAME)
End Enum

mazoolagh
دوشنبه 16 خرداد 1401, 08:34 صبح
چون ویرایشگر اینجا کدها رو به هم میریزه کل ماژول رو بعنوان یک فایل پیوست میکنم:

mazoolagh
دوشنبه 16 خرداد 1401, 08:38 صبح
بخش تعریف WindowStyles کلا از اینجا برداشته شده:
pinvoke.net: WindowStyles (Enums) (https://www.pinvoke.net/default.aspx/Enums/WindowStyles.html)

فقط یک مورد دیگه بهش اضافه کردم:
WS_NOBORDER = Not (WS_BORDER Or WindowStyles.WS_SIZEFRAME)

mazoolagh
دوشنبه 16 خرداد 1401, 08:41 صبح
Public Class Form1
Dim WS_original As WindowStyles
Dim hWnd As IntPtr


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim prc() As Process = Process.GetProcessesByName("cmd")
If prc.Length > 0 AndAlso prc(0).MainWindowHandle.ToInt32 > 0 Then
hWnd = prc(0).MainWindowHandle
WS_original = GetWindowLongPtr(hWnd, GWL.GWL_STYLE)
Dim WS_new As WindowStyles = WS_original And WindowStyles.WS_NOBORDER
SetWindowLongPtr(hWnd, GWL.GWL_STYLE, WS_new)
Else
MsgBox("Application/Process Not Found", vbCritical, "")
End If
End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SetWindowLongPtr(hWnd, GWL.GWL_STYLE, WS_original)
End Sub
End Class

mazoolagh
دوشنبه 16 خرداد 1401, 08:46 صبح
153816

153817

mazoolagh
دوشنبه 16 خرداد 1401, 08:50 صبح
این ساده ترین حالت هست،
ممکنه لازم باشه شما با توجه به شرایط تعریف دیگه ای برای WS_NOBORDER بنویسین.
یا نیاز باشه که از WindowStylesEx هم استفاده کنین.

ROSTAM2
دوشنبه 16 خرداد 1401, 19:23 عصر
سلام.
ممنون برای صدا زدن پروسس به اون صورت جواب می ده اما من ی Console App رو با خود نرم افزار اجرا می کنم همزمان با لود شدن فرم:


Dim P As Process
Private Sub MainForm_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim s As ProcessStartInfo = New ProcessStartInfo(Application.StartupPath + "\Console\ConsoleApp.exe")
With s
.UseShellExecute = True
.WorkingDirectory = Application.StartupPath + "\Console"
.CreateNoWindow = True
.WindowStyle = ProcessWindowStyle.Maximized
End With
P = Process.Start(s)
End Sub


و در رویداد Shown از فرم دستورات حذف نوار عنوان قرار دادم ولی روی اون app اجرا نمی شه در صورتی که با همین روش توسط کد های api شیء والد برای اون تعیین کردم و درست عمل می کنه.



SetWindowParent(Me.ConsolePanel, P)


Dim WS_original As WindowStyles
WS_original = GetWindowLong(P.MainWindowHandle, GWL.GWL_STYLE)
Dim WS_new As WindowStyles = WS_original And WindowStyles.WS_NOBORDER
SetWindowLong(P.MainWindowHandle, GWL.GWL_STYLE, WS_new)


153820

آیا این دستورات می تونن یک پروسس تعریف شده در خود برنامه رو پشتیبانی کنن یا باید فکر دیگه ای کرد؟!

mazoolagh
سه شنبه 17 خرداد 1401, 12:09 عصر
سلام دوباره
این کد اگر window handle مشخص باشه مشکلی نداره صرفنظر از این که parent اون چی باشه.
فکر کنم وقتی رخداد window shown اجرا میشه هنوز پروسه p کاملا اجرا نشده و بنابراین window handle اون مشخص نیست.
برای اطمینان 2 راه هست:

1- کد رو بجای window shown در یک button click بذارین و نتیجه رو چک کنین.
اگر اجرا شد مشکل همینه

2- در ابتدای رخداد window shown و قبل از کد اصلی، کد زیر رو اضافه کنین و window handle رو چک کنین:
debug.print (p.mainwindowhandle)
اگر 0 باشه مشکل همینه

اگر مشکل همین مشخص نبودن window handle بود کد زیر رو قبل از کد اصلی اضافه کنین - باحتمال زیاد برطرف میشه.
do while p.mainwindowhandle=0
loop