在我的應用程序中,我必須調整窗體大小和其對鼠標拖動效果的所有控件,並且窗體應該具有投影效果,問題是我的所有窗體都是自定義窗體(沒有邊框)。在c sharp中調整自定義窗體(帶陰影效果)和鼠標拖動事件的控件大小?
在此先感謝
在我的應用程序中,我必須調整窗體大小和其對鼠標拖動效果的所有控件,並且窗體應該具有投影效果,問題是我的所有窗體都是自定義窗體(沒有邊框)。在c sharp中調整自定義窗體(帶陰影效果)和鼠標拖動事件的控件大小?
在此先感謝
我覺得你必須自己實現
的原因,我建議動態事件綁定,這樣你們可以指定其控制或區域應具備鼠標按下
無邊框(或控制),你打算如何調整?圖將這部分分離出來,那就試試這個代碼在您的形式:
public class CustomForm : Form
{
private const int WmNcLButtonDown = 0xA1;
private const int HtBottomRight = 17;
[DllImport("user32.dll")]
private static extern int ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);
// elsewhere
void ResizeForm()
{
ReleaseCapture();
SendMessage(this.Handle, WmNcLButtonDown, HtBottomRight, 0);
}
}
此代碼將調整您的形式,雖然使用了右下角。查找不同位置的HT_BOTTOMRIGHT和其他HT_常量來調整大小。
我不確定陰影效果,但您應該可以通過在右下角放置一個按鈕並使用某個適當的圖標來調整窗體大小。當用戶點擊並拖動此按鈕時,它將調整窗體大小。下面是一些示例代碼:
public partial class Form1 : Form
{
private int bottomBorder;
private int rightBorder;
private Point mouseStart;
private bool isResizing = false;
public Form1()
{
InitializeComponent();
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (isResizing)
{
var newLocation = button1.Location;
newLocation.Offset(
e.X - mouseStart.X,
e.Y - mouseStart.Y);
button1.Location = newLocation;
this.Height = button1.Bottom + bottomBorder;
this.Width = button1.Right + rightBorder;
button1.Refresh();
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
isResizing = true;
mouseStart = e.Location;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
isResizing = false;
}
private void Form1_Load(object sender, EventArgs e)
{
bottomBorder = this.Height - button1.Bottom;
rightBorder = this.Width - button1.Right;
}
}
一個更精確。 – 2010-10-20 06:37:32
我用唐柯比和馬修·費雷拉的解決方案,並創建了自己的解決方案結合兩者。我添加了一個名爲「resizeHandle」的StatusStrip,使其尺寸爲20x20像素並聽取它的事件。
public class CustomForm : Form
{
private const int WmNcLButtonDown = 0xA1;
private const int HtBottomRight = 17;
private const int wmNcLButtonUp = 0xA2;
private bool isResizing = false;
[DllImport("user32.dll")]
private static extern int ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);
private void resizeHandle_MouseDown(object sender, MouseEventArgs e)
{
isResizing = true;
}
private void resizeHandle_MouseMove(object sender, MouseEventArgs e)
{
if (isResizing)
{
// Check if we have released the Left mouse button
isResizing = (e.Button == MouseButtons.Left);
ReleaseCapture();
if (isResizing)
{
SendMessage(Handle, wmNcLButtonDown, HtBottomRight, 0);
}
else
{
// Left Mouse button was released, end resizing.
SendMessage(Handle, wmNcLButtonUp, HtBottomRight, 0);
}
}
}
我已經實施了幾天前相同的方式重新大小。但我想我應該選擇你的答案,因爲你是第一個在這裏發帖的人。 – 2010-10-20 06:34:24