tagCANDY CGI VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板)
VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板)
[ツリー表示へ]  [ワード検索]  [Home]

タイトル Re: [×解決] AcceptButton / CancelButton プロパティの利用
投稿日: 2008/09/07(Sun) 22:05
投稿者neptune
こんにちは

ちょっとお邪魔します。

求めてないとは思いますが、Hookのサンプルありました。ドイツ語ですけど。
http://www.vb-fun.de/cgi-bin/loadframe.pl?ID=dotnet/tipps/tip0157.shtml
ちょっと改造したら、直ぐ使えるようです。

動作確認したら、少しは修正したかもわかりませんが、殆どそのまんまでサンプルは
動きました。

で、下記を書いて位置修正してみたら、出来るようです。
サンプルのDialogHookProcから下記のAdjustWindowを呼びます。
追加分のみ。vb2008で作成

#Region "Class ProcessingSys"
    Public Class ProsessingSys

        <StructLayout(LayoutKind.Sequential)> _
        Public Structure RECT
            <MarshalAs(UnmanagedType.I4)> _
            Public Left As Integer
            <MarshalAs(UnmanagedType.I4)> _
            Public Top As Integer
            <MarshalAs(UnmanagedType.I4)> _
            Public Right As Integer
            <MarshalAs(UnmanagedType.I4)> _
            Public Bottom As Integer
        End Structure

        Public Structure RECT2
            Public rc As RECT

            Public ReadOnly Property Width() As Integer
                Get
                    Return rc.Right - rc.Left
                End Get
            End Property

            Public ReadOnly Property Height() As Integer
                Get
                    Return rc.Bottom - rc.Top
                End Get
            End Property

        End Structure

        <DllImport("user32.dll")> _
      Private Shared Function GetWindowRect(ByVal hwnd As IntPtr, ByRef lpRect As RECT) As Integer
        End Function


        <DllImport("user32.dll")> _
        Public Shared Function MoveWindow _
                    (ByVal hwnd As IntPtr, _
                     ByVal x As Integer, _
                     ByVal y As Integer, _
                     ByVal nWidth As Integer, _
                     ByVal nHeight As Integer, _
                     ByVal bRepaint As Integer) As Integer

        End Function

        ''' <summary>
        ''' Window位置の調整を行う。渡されたWindowを画面の中央に配置する
        ''' </summary>
        ''' <param name="pHandle">MessageBoxのHWND</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function AdjustWindow(ByVal pHandle As IntPtr) As Boolean
            Dim hWnd As IntPtr = pHandle
            'ディスプレイの大きさ取得
            Dim iWidth As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width()
            Dim iHeight As Integer = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height()

            'WindowのRECTを取得
            Dim rc As New RECT
            GetWindowRect(pHandle, rc)
            Dim rc2 As New RECT2
            rc2.rc = rc

            Dim x, y As Integer
            x = CInt((iWidth - rc2.Width) / 2)
            y = CInt((iHeight - rc2.Height) / 2)

            Dim ret As Integer = MoveWindow(pHandle, x, y, rc2.Width, rc2.Height, 1)

            Return CBool(ret)
        End Function
    End Class
#End Region

ここで質問
1.FrameWorkで代替があるかどうかわからなかったのでGetWindowRect、MoveWindow 
APIを使いました。FrameWorkの方法があれば教えて下さい。
それと構造としてもっと賢い書き方があれば教えて下さい。

2.'Dim thisThread As Integer = AppDomain.GetCurrentThreadId()
で、「こちらは古い方式」と警告が出る。そこで
Dim thisThread As Integer = System.AppDomain.GetCurrentThreadId()
と書いてみたら、こんどは
'こちらは「インスタンスを経由する共有メンバ、定数メンバ、列挙型メンバ、または
'入れ子にされた型へのアクセスです。正規の式は評価されません。」と警告が出る
なので
'Dim thisThread As Integer = System.Threading.Thread.CurrentThread.ManagedThreadId()
と書いてみたら、今度は、上記2つとは違うスレッドを拾ってくるみたいでHookに失敗する。

警告や、エラーが出ない書き方はどうやるんでしょう?

3.
        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        Private Shared Function SetWindowsHookEx( _
            ByVal idHook As Integer, _
            ByVal lpfn As HookProc, _  ←サンプルのこのやり方はMSDNに書いているんですかね?
            ByVal hInstance As IntPtr, _
            ByVal threadId As Integer) As IntPtr
        End Function

以上、暇なときで結構ですからご教示下さい。便乗ですが宜しくお願いします。

感想:
Hookはframeworkではとても面倒。C#のサンプルはあるが、VBのサンプルは殆どない。
さすがにフックするだけあってと、クラスか、メソッドを増やしていけば、背景色や、
文字のフォントとか拡張はどうとでもなりそうだし、使い回しが効くので
一度作れば楽チンかな?。

Webの翻訳サービスはエライ!

ちなみに・・・ボチボチと、私には難しいので理解と、実験に3日掛かりました。^ ^;;
でも正直まだ理屈は良く判らんです。

長々と失礼しました。

- 関連一覧ツリー をクリックするとツリー全体を一括表示します)

古いスレッドにレスはつけられません。