C# 폼 이동 코드

C# 2014. 11. 4. 09:22

Borderless Form에서 이동가능하게하고싶을 때

        
        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();

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


폼 사이즈 조절 코드

protected override void WndProc(ref Message m)
{
public const UInt32 WM_NCHITTEST = 0x0084;
public const UInt32 WM_MOUSEMOVE = 0x0200;

public const UInt32 HTLEFT = 10;
public const UInt32 HTRIGHT = 11;
public const UInt32 HTBOTTOMRIGHT = 17;
public const UInt32 HTBOTTOM = 15;
public const UInt32 HTBOTTOMLEFT = 16;
public const UInt32 HTTOP = 12;
public const UInt32 HTTOPLEFT = 13;
public const UInt32 HTTOPRIGHT = 14;
 
int RESIZE_HANDLESIZE = 10;
bool handled = false;

if (m.Msg == Win32.WM_NCHITTEST || m.Msg == Win32.WM_MOUSEMOVE)
{
Size formSize = this.Size;
Point screenPoint = new Point(m.LParam.ToInt32());
Point clientPoint = this.PointToClient(screenPoint);

Dictionary boxes = new Dictionary() {
{Win32.HTBOTTOMLEFT, new Rectangle(0, formSize.Height - RESIZE_HANDLESIZE, RESIZE_HANDLESIZE, RESIZE_HANDLESIZE)},
{Win32.HTBOTTOM, new Rectangle(RESIZE_HANDLESIZE, formSize.Height - RESIZE_HANDLESIZE, formSize.Width - 2*RESIZE_HANDLESIZE, RESIZE_HANDLESIZE)},
{Win32.HTBOTTOMRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLESIZE, formSize.Height - RESIZE_HANDLESIZE, RESIZE_HANDLESIZE, RESIZE_HANDLESIZE)},
{Win32.HTRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLESIZE, RESIZE_HANDLESIZE, RESIZE_HANDLESIZE, formSize.Height - 2*RESIZE_HANDLESIZE)},
{Win32.HTTOPRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLESIZE, 0, RESIZE_HANDLESIZE, RESIZE_HANDLESIZE) },
 {Win32.HTTOP, new Rectangle(RESIZE_HANDLESIZE, 0, formSize.Width - 2*RESIZE_HANDLESIZE, RESIZE_HANDLESIZE) },
{Win32.HTTOPLEFT, new Rectangle(0, 0, RESIZE_HANDLESIZE, RESIZE_HANDLESIZE) },
{Win32.HTLEFT, new Rectangle(0, RESIZE_HANDLESIZE, RESIZE_HANDLESIZE, formSize.Height - 2*RESIZE_HANDLESIZE) }
};

foreach (KeyValuePair hitBox in boxes)
{
if(this.WindowState != FormWindowState.Maximized
 && hitBox.Value.Contains(clientPoint))
{
                        m.Result = (IntPtr)hitBox.Key;
                        handled = true;
                        break;
                    }
                }
 }
if (!handled)
      base.WndProc(ref m);
}

'C#' 카테고리의 다른 글

[C#] TextBox에 숫자만 입력받기  (0) 2014.11.10
C# Spy++  (0) 2014.11.06
C# 폼 FullScreen 전체화면으로 만들기  (0) 2014.11.05
C# 투명한 폼 만들기  (2) 2014.11.04
C# 전역 마우스 후킹(hooking) 및 block  (0) 2014.11.03
Posted by outshine90
,