他のパネル内にある場合とない場合があるパネルがあります。私はそのパネルの目に見える境界を解決しようとしています。私は、GetWindowRect
またはがGetClientRect
トリックを行うだろうと思ったが、喜びはなかった.
これをテストするために、パネルと複数行のテキスト ボックスを含むフォームを作成しました。パネルがフォームよりも大きい (つまり、フォームの下部より下に伸びている)
したがって、私のフォームが 300 x 300 で、パネルが 10,10 にあり、100 x 500 の場合、可視領域が 100、290 であることを教えてくれるものが必要です (パネルの相対開始点が 0,0 であると仮定します)これは全体で 10,10 になります。
そのような方法は存在しますか?
興味のあるパネルの親ハンドルを取得してテストするなど、いくつかの異なる方法を試しましたが、直接の親が可視性を決定するものになるとは限りません。
テスト用に書いたテストアプリケーションのコードは次GetWindowRect
のGetClientRect
とおりです。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace clientRectTest
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
public static RECT FromRectangle(Rectangle rectangle)
{
RECT win32rect = new RECT();
win32rect.top = rectangle.Top;
win32rect.bottom = rectangle.Bottom;
win32rect.left = rectangle.Left;
win32rect.right = rectangle.Right;
return win32rect;
}
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
public Form1()
{
InitializeComponent();
this.AutoScrollMinSize = new Size(250, 500);
}
protected override void OnMouseClick(MouseEventArgs e)
{
NewLine("Click Location: " + e.Location.ToString());
base.OnMouseClick(e);
}
protected override void OnResize(EventArgs e)
{
this.textBox1.Text = "Panel Size" + this.panel1.Size.ToString();
NewLine( "Form Size" + this.Size.ToString());
// NewLine("Panel PARENT Client Rectangle: " + getPanelClientRectangle(this.panel1.Parent.Handle));
NewLine("Panel Client Rectangle: " + getPanelClientRectangle(this.panel1.Handle));
NewLine("Panel Window Rectangle: " + getPanelWindowRectangle(this.panel1.Handle));
NewLine("Panel Window Bounts: " + this.panel1.Bounds.ToString());
NewLine("Panel DC Client Rectangle: " + getPanelDCClientRectangle(this.panel1.Handle));
NewLine("Panel DC Window Rectangle: " + getPanelDCWindowRectangle(this.panel1.Handle));
NewLine("Panel Location: " + this.panel1.Location.ToString());
base.OnResize(e);
}
private string getPanelDCClientRectangle(IntPtr handle)
{
string cr = string.Empty;
RECT r1 = new RECT();
IntPtr dc = GetWindowDC(handle);
GetClientRect(dc, out r1);
cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
+ ", " + r1.bottom.ToString();
Point thisLocation = this.Location;
return cr;
}
private string getPanelDCWindowRectangle(IntPtr handle)
{
string cr = string.Empty;
RECT r1 = new RECT();
IntPtr dc = GetWindowDC(handle);
GetWindowRect(dc, out r1);
cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
+ ", " + r1.bottom.ToString();
Point thisLocation = this.Location;
return cr;
}
private string getPanelWindowRectangle(IntPtr handle)
{
string cr = string.Empty;
RECT r1 = new RECT();
GetWindowRect(handle, out r1);
cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
+ ", " + r1.bottom.ToString();
Point thisLocation = this.Location;
return cr;
}
private string getPanelClientRectangle(IntPtr handle)
{
string cr = string.Empty;
RECT r1 = new RECT();
GetClientRect(handle, out r1);
cr = r1.left.ToString() + ", " + r1.top.ToString() + ", " + r1.right.ToString()
+ ", " + r1.bottom.ToString();
Point thisLocation = this.Location;
return cr;
}
private void NewLine(string p)
{
this.textBox1.Text += Environment.NewLine + p;
}
}
}
編集:詳細情報が見つかりました:
行this.AutoScrollMinSize = new Size(250, 500);
が私の結果を台無しにしていたと思います。これにより、500 が表示されていないにもかかわらず、パネルが 500 になっているように見えました。テスト ケースの一部をやり直します。この行が問題を引き起こすとは思っていませんでした。