WPF custom form size change

Source: Internet
Author: User
Front-end code: <Window X: class = "wpfresizewindow. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1" Height = "300" width = "300" windowstyle = "NONE" allowstransparency = "true">
<Grid>
<Grid. rowdefinitions>
<Rowdefinition Height = "4"/>
<Rowdefinition/>
<Rowdefinition Height = "4"/>
</Grid. rowdefinitions>

<Grid. columndefinitions>
<Columndefinition width = "4"/>
<Columndefinition/>
<Columndefinition width = "4"/>
</Grid. columndefinitions>

<Rectangle name = "resizetopleft" fill = "black" grid. Row = "0" grid. Column = "0" mousemove = "resizepressed" mousedown = "resizepressed"/>
<Rectangle name = "resizetop" fill = "black" grid. Row = "0" grid. Column = "1" mousemove = "resizepressed" mousedown = "resizepressed"/>
<Rectangle name = "resizetopright" fill = "black" grid. Row = "0" grid. Column = "2" mousemove = "resizepressed" mousedown = "resizepressed"/>
<Rectangle name = "resizeleft" fill = "black" grid. Row = "1" grid. Column = "0" mousemove = "resizepressed" mousedown = "resizepressed"/>
<Rectangle name = "resizeright" fill = "black" grid. Row = "1" grid. Column = "3" mousemove = "resizepressed" mousedown = "resizepressed"/>
<Rectangle name = "resizebottomleft" fill = "black" grid. Row = "3" grid. Column = "0" mousemove = "resizepressed" mousedown = "resizepressed"/>
<Rectangle name = "resizebottom" fill = "black" grid. Row = "3" grid. Column = "1" mousemove = "resizepressed" mousedown = "resizepressed"/>
<Rectangle name = "resizebottomright" fill = "black" grid. Row = "3" grid. Column = "2" mousemove = "resizepressed" mousedown = "resizepressed"/>
</GRID>
</WINDOW>

Background code:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. Data;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. imaging;
Using system. Windows. shapes;
Using system. runtime. interopservices;
Using system. Windows. InterOP;

Namespace simplezigbee
{
/// <Summary>
/// Mouse direction
/// </Summary>
Public Enum resizedirection
{
/// <Summary>
/// Left
/// </Summary>
Left = 1,
/// <Summary>
/// Right
/// </Summary>
Right = 2,
/// <Summary>
/// Upload
/// </Summary>
Top = 3,
/// <Summary>
/// Top left
/// </Summary>
Topleft = 4,
/// <Summary>
/// Upper right
/// </Summary>
Topright = 5,
/// <Summary>
/// Bottom
/// </Summary>
Bottom = 6,
/// <Summary>
/// Bottom left
/// </Summary>
Bottomleft = 7,
/// <Summary>
/// Bottom right
/// </Summary>
Bottomright = 8,
}
/// <Summary>
/// Interaction logic of window1.xaml
/// </Summary>
Public partial class window1: Window
{
Private hwndsource _ hwndsource;
Private const int wm_syscommand = 0x112;
[Dllimport ("user32.dll", charset = charset. Auto)]
Private Static extern intptr sendmessage (intptr hwnd, uint MSG, intptr wparam, intptr lparam );

Private dictionary <resizedirection, cursor> cursors = new dictionary <resizedirection, cursor>
{
{Resizedirection. Top, cursors. sizens },
{Resizedirection. Bottom, cursors. sizens },
{Resizedirection. Left, cursors. sizewe },
{Resizedirection. Right, cursors. sizewe },
{Resizedirection. topleft, cursors. sizenwse },
{Resizedirection. bottomright, cursors. sizenwse },
{Resizedirection. topright, cursors. sizenesw },
{Resizedirection. bottomleft, cursors. sizenesw}
};

Public window1 ()
{
Initializecomponent ();

This. sourceinitialized + = delegate (Object sender, eventargs E)
{
This. _ hwndsource = presentationsource. fromvisual (visual) sender) as hwndsource;
};

This. mousemove + = new mouseeventhandler (window_mousemove );
}

Void window_mousemove (Object sender, mouseeventargs E)
{
If (mouse. leftbutton! = Mousebuttonstate. Pressed)
{
Frameworkelement element = E. originalsource as frameworkelement;
If (element! = NULL &&! Element. Name. Contains ("resize") This. cursor = cursors. Arrow;
}
}

Private void resizepressed (Object sender, mouseeventargs E)
{
Frameworkelement element = sender as frameworkelement;
Resizedirection direction = (resizedirection) enum. parse (typeof (resizedirection), element. Name. Replace ("resize ",""));

This. cursor = cursors [direction];
If (E. leftbutton = mousebuttonstate. Pressed) resizewindow (Direction );
}

Private void resizewindow (resizedirection direction)
{
Sendmessage (_ hwndsource. Handle, wm_syscommand, (intptr) (61440 + direction), intptr. Zero );
}
}
}

Another codeproject has encapsulated class library (with the corresponding source code download): http://www.codeproject.com/Articles/23138/WPF-Window-Resizing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.