C 無邊框窗體移動和改變大小的實現

2021-06-18 11:01:10 字數 1558 閱讀 9048

自己給軟體做的**,將窗體設為無邊框後,想要其具有正常窗體的移動和改變大小功能,以下**可以實現。。。。

//需新增using system.runtime.interopservices;

[dllimport("user32.dll")]  

public static extern bool releasecapture();

[dllimport("user32.dll")]

public static extern bool sendmessage(intptr hwnd, int wmsg, int wparam, int lparam);

//常量

public const int wm_syscommand = 0x0112;

//窗體移動

public const int sc_move = 0xf010;

public const int htcaption = 0x0002; 

//改變窗體大小

public const int wmsz_left = 0xf001;

public const int wmsz_right = 0xf002;

public const int wmsz_top = 0xf003;

public const int wmsz_topleft = 0xf004;

public const int wmsz_topright = 0xf005;

public const int wmsz_bottom = 0xf006;

public const int wmsz_bottomleft = 0xf007;

public const int wmsz_bottomright = 0xf008;

宣告完函式和定義完這些常量後,就是在事件裡面具體的實現了。。。

//窗體移動

在要實現的控制項的mousedown事件中新增以下**:

private void panel_title_bar_mousedown(object sender, mouseeventargs e)

//窗體大小的改變

同理在控制項的

mousedown事件中新增以下**:

private void panel_bottom_mousedown(object sender, mouseeventargs e)

其中的wmsz_bottom為要改變的方向,在常量中8個方向都已經定義,其它方向換用對應常量即可。好了,可以試下效果,看是不是能移動和改變大小了,哈哈。。。

再加一條,就是無邊框最大化後會全屏顯示,只要在窗體載入中加下面這句來限制最大化窗體大小即可:

this.maximumsize = new size(screen.primaryscreen.workingarea.width,  

screen.primaryscreen.workingarea.height);

同理限定最小的也可以在窗體載入中加入以下語句:

this.minimumsize = new size(this.width, this.height);//窗體改變大小時最小限定在初始化大小

無邊框窗體拖動和改變大小

const int htleft 10 const int htright 11 const int httop 12 const int httopleft 13 const int httopright 14 const int htbottom 15 const int htbottomlef...

WPF 無邊框窗體滑鼠拖動改變大小和移動

protected override void onsourceinitialized eventargs e protected virtual intptr wndproc intptr hwnd,int msg,intptr wparam,intptr lparam,ref bool hand...

qt 無邊框窗體的拖到和改變大小

參考 這個類是關於無邊框窗體的拖動和改變大小的。如果是qwidget,請將繼承類改為qwidget,並將原始檔的qdialog用qwidget替換。我曾想將這個類作為中間類,讓視窗類繼承此類,這樣如果有多個無邊框視窗,就不用每個視窗都作重複改變了,但是沒有成功。由於時間關係,沒再做深入研究。以後有時...