PDA

View Full Version : قطعه کد برای بردر استایل



amir000555
پنج شنبه 09 دی 1395, 17:12 عصر
با سلام خدمت همه.بنده در سی شارپ در ویژوال استادیو . یه پروژه جدید ساختم که FormBorderStyle آن را روی None قرار دادم و اون علامت Exit , Minimize , RestoreDown رفت تا خودم بسازم . وقتی این کارار کردن یه پنل گذاشتم تا دکمه هایم را در اون آن قرار بدم و بعد از تموم شدم کارم برنامه را که اجرا کردن متاسفانه نمیتونستم پنجره برنامه را جا به جا کنم میدونم یه کدی باید در پنل بزنم تا بتونم جا به جا کنم ولی یادم رفتم ممنون میشم کد را به من بگید.

golestan1
پنج شنبه 09 دی 1395, 17:30 عصر
نحوه جابجايي فرم با استفاده از
API
ابتدا Namespace زير رو به برنامه اضافه کنيد

using System.Runtime.InteropServices;

سپس کد زير رو به ابتداي برنامه اضافه کنيد

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

حالا کد زير را در رويداد MouseDown هر شي که خواستيد اضافه کنيد

if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, 0xa1, 0x2, 0);
}
..............................
جابه جا کردن فرم با کليک بر روي هر قسمت آن با توابع
API


using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern int ReleaseCapture();
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
static extern int SendMessage(int hwnd, int wMsg, int wParam, object lParam);
private const int WM_NCLBUTTONDOWN = 161;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle.ToInt32(), WM_NCLBUTTONDOWN, 2, 0);
}
.................................
جابه جا کردن فرم با کليک بر روي هر قسمت آن

private bool dragging;
private Point pointClicked;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Turn drag mode on and store the point clicked.
dragging = true;
pointClicked = new Point(e.X, e.Y);
}
else
{
dragging = false;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point pointMoveTo;
// Find the current mouse position in screen coordinates.
pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));
// Compensate for the position the control was clicked.
pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
// Move the form.
this.Location = pointMoveTo;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}
.....................................

amir000555
پنج شنبه 09 دی 1395, 17:41 عصر
داداش خیلی ببخشیدا ولی من هنگ کردم میشه واضع تر بگی من فقط میخام وقتی با موس اون پنل بالا رو تکون میدم کلا برنامه هم با این کار جا به جا بتونم بکنم همین.
یا بهم واضع تر بگو لطفا یا به یه برنامه خودت تستی بساز بدش بهم.واقعا ممنونت میشم.

یونس ابراهیمی
پنج شنبه 09 دی 1395, 19:00 عصر
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern int ReleaseCapture();
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
static extern int SendMessage(int hwnd, int wMsg, int wParam, object lParam);
private const int WM_NCLBUTTONDOWN = 161;

public Form1()
{
InitializeComponent();
}

private void Panel1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle.ToInt32(), WM_NCLBUTTONDOWN, 2, 0);
}
}
}


یا
یکی از قابلیت های ساده اما بسیار کاربردی که راحتی کار با برنامه شما را برای کاربر بیشتر می کند ، انتفال فرم با ماوس با کلیک کردن روی هر نقطه ای از آن است .
با استفاده از کد زیر که از توابع API در آن استفاده شده است ، می توانید به آسانی این کار را انجام دهید .
ابتدا فضای نام زیر را در برنامه وارد کنید

using System.Runtime.InteropServices; سپس از کد زیر در رویداد MouseDown فرم استفاده کنید :

[DllImport("user32.dll")]
static extern int ReleaseCapture();
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
static extern int SendMessage(int hwnd, int wMsg, int wParam, object lParam);
private const int WM_NCLBUTTONDOWN = 161;

private void Panel1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle.ToInt32(), WM_NCLBUTTONDOWN, 2, 0);
}

منبع : w3-farsi.com (http://www.w3-farsi.com)

amir000555
پنج شنبه 09 دی 1395, 19:07 عصر
داداش کد دئمی خوب دادی ولی من میخام وقتی روی panel1 کلیک کردن و اونو darg کرد بتونه جا به جا کنه فقط اون قسمتو بتونه

یونس ابراهیمی
پنج شنبه 09 دی 1395, 19:08 عصر
داداش کد دئمی خوب دادی ولی من میخام وقتی روی panel1 کلیک کردن و اونو darg کرد بتونه جا به جا کنه فقط اون قسمتو بتونه
کد رو اصلاح کردم، اگر panel رو Dock کردین، تو رویداد mouseDown پنل بنویسین درست میشه

amir000555
پنج شنبه 09 دی 1395, 19:12 عصر
یه اصلاح کنید دوباره ارور می ده کاراکتر های { این ارور میده

یونس ابراهیمی
پنج شنبه 09 دی 1395, 19:15 عصر
حق با شماست کد زیر اصلا مشکل نداره



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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
}
}

amir000555
پنج شنبه 09 دی 1395, 19:16 عصر
این را باید کجا قرار بدم؟

amir000555
پنج شنبه 09 دی 1395, 19:20 عصر
ببینید من این کد زیر را وارد کردم

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace New_Browser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
}
}


ولی ارور میده میشه روی همین بگین

یونس ابراهیمی
پنج شنبه 09 دی 1395, 19:22 عصر
این را باید کجا قرار بدم؟

یه کم به کد بالا دقت کنید متوجه میشین

143952

amir000555
پنج شنبه 09 دی 1395, 19:25 عصر
مرسی داداش درستش کردم واقعا کمکم کردی خیلی کمک بزرگی بود.راستی میخوام بهم یاد بدی سی شارپ آخه سال بعد این را باید بلد باشیم ولی من بلد نیستم می تونی بهم یا دبدی؟