外挂窗体、内嵌窗体

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    Private Const GWL_STYLE = (-16)
    Private Const WS_SYSMENU = &H80000
    Private Const WS_CAPTION = &HC00000
    Private Const WS_SIZEBOX = &H40000
    Private Const WS_MAXIMIZEBOX = &H10000
    Private Const WS_MINIMIZEBOX = &H20000
    Private Const SWP_NOZORDER = &H4
    Private Const SWP_FRAMECHANGED = &H20
    Private Const SWP_NOREPOSITION = &H200
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer

    Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    Private Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Integer
    Private Declare Function MoveWindow Lib "user32" Alias "MoveWindow" (ByVal hwnd As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Integer

    Dim skyHwnd As Integer = FindWindow("tSkMainForm.UnicodeClass", vbNullString)
    Dim lStyle As Integer

    Private Structure RECT
        Dim Left As Integer
        Dim Top As Integer
        Dim Right As Integer
        Dim Bottom As Integer
    End Structure

    Dim ret As New RECT

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        lStyle = lStyle Or WS_SYSMENU
        lStyle = lStyle Or WS_CAPTION
        lStyle = lStyle Or WS_SIZEBOX
        lStyle = lStyle Or WS_MAXIMIZEBOX
        lStyle = lStyle Or WS_MINIMIZEBOX

        SetWindowLong(skyHwnd, GWL_STYLE, lStyle)
        SetParent(skyHwnd, GetDesktopWindow)

        MoveWindow(skyHwnd, ret.Left, ret.Top, ret.Right - ret.Left, ret.Bottom - ret.Top, 1)
        ShowWindow(skyHwnd, SW_SHOW)
    End Sub

    Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    Private Const SW_RESTORE = 9
    Private Const SW_SHOW = 5
    Private Const SW_SHOWNOACTIVATE = 4

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GetWindowRect(skyHwnd, ret)

        lStyle = lStyle And Not WS_SYSMENU
        lStyle = lStyle And Not WS_CAPTION
        lStyle = lStyle And Not WS_SIZEBOX
        lStyle = lStyle And Not WS_MAXIMIZEBOX
        lStyle = lStyle And Not WS_MINIMIZEBOX

        SetWindowLong(skyHwnd, GWL_STYLE, lStyle)

        SetParent(skyHwnd, Me.Handle.ToInt32)
        MoveWindow(skyHwnd, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, 1)
        ShowWindow(skyHwnd, SW_SHOW)
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.WindowState <> FormWindowState.Minimized Then
            MoveWindow(skyHwnd, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, 1)
        End If
    End Sub  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VS2022 WinForms 内嵌 Chrome 浏览器的方法如下: 首先,在 VS2022 中创建一个 WinForms 应用程序项目。然后,确保已安装 CefSharp NuGet 包,这是 C# 中一个流行的将 Chrome 内嵌到应用程序中的库。 接下来,打开 MainForm(主窗体)的设计视图。在工具箱中,如果找不到 CefSharp 控件,则可以右键单击工具箱,选择“选择项”,然后在“COM 组件”选项卡中查找并勾选“CefSharp Browser Control”。 现在,可以在工具箱中找到 CefSharp 控件。将一个 CefSharp 控件拖放到 MainForm 的窗体上,将其大小调整为适合窗体的大小。 然后,打开 MainForm 的代码视图。在窗体的构造函数中添加以下代码: ```csharp using CefSharp; using CefSharp.WinForms; public MainForm() { InitializeComponent(); InitializeBrowser(); } private void InitializeBrowser() { CefSettings settings = new CefSettings(); Cef.Initialize(settings); ChromiumWebBrowser browser = new ChromiumWebBrowser("https://www.google.com"); // 要加载的网页地址 browser.Dock = DockStyle.Fill; Controls.Add(browser); } ``` 在此代码中,首先引入了 CefSharp 和 CefSharp.WinForms 命名空间。然后,在窗体的构造函数中调用 InitializeBrowser() 方法来初始化浏览器控件。在 InitializeBrowser() 方法中,创建了一个 CefSettings 对象,并使用 Cef.Initialize(settings) 初始化了 CefSharp。接下来,创建了一个 ChromiumWebBrowser 控件并设置其 Dock 属性为 Fill,以便填充整个窗体。最后,将浏览器控件添加到窗体的控件集合中。 编译并运行该应用程序,将会在 WinForms 窗体中嵌入一个 Chrome 浏览器,并加载指定的网页地址。 请注意,进行这些步骤之前,确保已经采取了适当的授权和遵守了相关法规和政策,以确保合法使用 Chrome 浏览器技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值