2011-10-25 29 views
-3

好吧,我正在Microsoft Visual Studio C++ 2010中製作簡單的便籤應用程序(Winodws Forms)in C++。 我試圖製作一個可拖動的無邊框窗體。 我現在的密碼是:C++ Draggable無國界形式

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) 
{ 
this->dragging = false; 
} 

private: System::Void Form1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) 
{ 
this->dragging = true; 
this->offset = Point(e->X, e->Y); 

} 


private: System::Void Form1_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) 
{ 
if (this->dragging) 
{ 
Point currentScreenPos = PointToScreen(e->Location); 
Location = Point(currentScreenPos.X - this->offset.X, currentScreenPos.Y - this->offset.Y); 
} 

} 

private: System::Void Form1_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) 
{ 
this->dragging = false; 
} 

這對我不起作用。誰能幫忙?

+2

展開「不起作用」。 –

+0

您在MouseMove事件中未使用「e」。 –

回答

2

而不是手動跟蹤鼠標,讓操作系統爲你做。攔截WM_NCHITTEST並返回HTCAPTION。或者,只使用MouseDown事件,向窗口發送特殊的WM_SYSCOMMAND/SC_DRAG消息。 MSDN上有大量有關拖動無邊框和/或無字幕窗口的信息。

+0

我只有13歲。我不知道如何去做你剛剛說的話。 – Lolsdasfgnhg

+0

在MSDN上查找'WM_NCHITTEST'。你可以讓你的winform覆蓋它的虛擬'WndProc()'方法來捕獲消息。至於'SC_DRAG',它沒有正式記錄,但它是'SC_MOVE + 2'。 'SC_MOVE'是'WM_SYSCOMMAND'消息的可用參數,可以使用'SendMessage()'函數發送給你的winform。 –