Original: C # handle use
Call the API function SendMessage to send the WM_CLOSE message.
DllImport ("User32.dll", entrypoint= "SendMessage")]
private static extern int SendMessage ( inthwnd,int msg,int wparam,int lParam);
const int wm_close = 0x10;
SendMessage (the window handle of the program, WM_CLOSE, 0, 0);
You can replace the int data type with INTPTR
How to implement a window anywhere in Visual C # 2005 Moving a window
First, you need to use a WIMDOWSAPI function, so you must introduce
Using System.Runtime.InteropServices;
namespaces;
Then, there are two ways to use the API, one that does not need to override the WndProc window procedure in a way that does not require an API function. Another method requires two:
SendMessage send message as specified window procedure
ReleaseCapture Release mouse capture
Finally, there are some necessary constant declarations that can be found in the MSDN or CPP header files:
Private Const INT WM_SYSCOMMAND = 0x0112;//System Information when you click the icon in the upper-left corner of the window
Private Const int sc_move = 0xf010;//Mobile Information
Private Const INT htcaption = 0x0002;//indicates system information when the mouse is in the window title bar
Private Const int Wm_nchittest =0x84;//A message that is sent when the mouse is in the form client area (except for the title bar and part of the border)
Private Const int htclient = 0x1;//represents the mouse in the window client area of the system message
Private Const int sc_maximize = 0xf030;//maximized information
Private Const int sc_minimize = 0xf020;//minimized information
Here we go.
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Runtime.InteropServices;
Namespace WindowsapplicationtestnoBoarderandmove
{
Publicpartial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
[DllImport ("User32.dll")]
public static extern bool ReleaseCapture ();
[DllImport ("User32.dll")]
public static extern bool SendMessage (INTPTR hwnd, int wmsg, intwparam, int lParam);
Private Const INT WM_SYSCOMMAND = 0x0112;//System Information when you click the icon in the upper-left corner of the window
Private Const int sc_move = 0xf010;//Mobile Information
Private Const INT htcaption = 0x0002;//indicates system information when the mouse is in the window title bar
Private Const int Wm_nchittest =0x84;//A message that is sent when the mouse is in the form client area (except for the title bar and part of the border)
Private Const int htclient = 0x1;//represents the mouse in the window client area of the system message
Private Const int sc_maximize = 0xf030;//maximized information
Private Const int sc_minimize = 0xf020;//minimized information
private void Closetoolstripmenuitem_click (object sender, Eventargse)
{
Application.exit ();
}
The following code I use///commented, this is two ways to implement
private void Form1_mousedown (object sender, Mouseeventargse)
//{
//ReleaseCapture ();//First release the mouse focus capture so that no more wm_nchittest messages are emitted
//SendMessage (this. Handle, Wm_syscommand, Sc_move + htcaption,0);//Then send a message to the current form, the message is move + means the mouse is on the title bar
//}
If you use this method of rewriting, comment out the above section ...
protected override void WndProc (ref Message m)
{
Switch (m.msg)
{
Case Wm_syscommand:
if (M.wparam = = (INTPTR) sc_maximize)
{
M.wparam = (IntPtr) sc_minimize;
}
Break
Case wm_nchittest://If the mouse moves or clicks
Base. WndProc (ref m);//Call the Base class window procedure--wndproc method to process the message
if (M.result = = (INTPTR) htclient)//If the return is Htclient
{
M.result = (IntPtr) htcaption;//change it to htcaption
return;//Direct Return Exit method
}
Break
}
Base. WndProc (ref m); If you are not moving the mouse or clicking a message, call the base class's window procedure for processing
}
There is a problem with this rewrite method.
Guess what happens if you double-click the mouse on the form? Hehe, the window realizes maximization ...
}
}
C # handles use