using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Interop;
using System;
namespace PreTranslate
{
public class Commands
{
// Keys
const int MK_SHIFT = 4;
const int MK_CONTROL = 8;
// Keyboard messages
const int WM_KEYDOWN = 256;
const int WM_KEYUP = 257;
const int WM_CHAR = 258;
const int WM_SYSKEYDOWN = 260;
const int WM_SYSKEYUP = 261;
// Mouse messages
const int WM_MOUSEMOVE = 512;
const int WM_LBUTTONDOWN = 513;
const int WM_LBUTTONUP = 514;
static long MakeLong(int LoWord, int HiWord)
{
return (HiWord << 16) | (LoWord & 0xffff);
}
static IntPtr MakeLParam(int LoWord, int HiWord)
{
return (IntPtr)MakeLong(LoWord,HiWord);
}
static int HiWord(int Number)
{
return (Number >> 16) & 0xffff;
}
static int LoWord(int Number)
{
return Number & 0xffff;
}
// State used by the VhmouseHandler to filter on
// the vertical or the horizontal
bool vMode;
bool hMode;
int ptx;
int pty;
// Commands to add/remove message handlers
[CommandMethod("caps")]
public void Caps()
{
Application.PreTranslateMessage +=
new PreTranslateMessageEventHandler(CapsHandler);
}
[CommandMethod("uncaps")]
public void UnCaps()
{
Application.PreTranslateMessage -=
new PreTranslateMessageEventHandler(CapsHandler);
}
[CommandMethod("vhmouse")]
public void Vhmouse()
{
Application.PreTranslateMessage +=
new PreTranslateMessageEventHandler(VhmouseHandler);
}
[CommandMethod("unvhmouse")]
public void UnVhmouse()
{
Application.PreTranslateMessage -=
new PreTranslateMessageEventHandler(VhmouseHandler);
}
[CommandMethod("watchCC")]
public void WatchCC()
{
Application.PreTranslateMessage +=
new PreTranslateMessageEventHandler(WatchCCHandler);
}
[CommandMethod("unwatchCC")]
public void UnWatchCC()
{
Application.PreTranslateMessage -=
new PreTranslateMessageEventHandler(WatchCCHandler);
}
[CommandMethod("noX")]
public void NoX()
{
Application.PreTranslateMessage +=
new PreTranslateMessageEventHandler(NoXHandler);
}
[CommandMethod("yes")]
public void YesX()
{
Application.PreTranslateMessage -=
new PreTranslateMessageEventHandler(NoXHandler);
}
// The event handlers themselves...
// Force alphabetic character entry to uppercase
void CapsHandler(
object sender,
PreTranslateMessageEventArgs e
)
{
// For every lowercase character message,
// reduce it my 32 (which forces it to
// uppercase in ASCII)
if (e.Message.message == WM_CHAR &&
(e.Message.wParam.ToInt32() >= 97 &&
e.Message.wParam.ToInt32() <= 122))
{
MSG msg = e.Message;
msg.wParam =
(IntPtr)(e.Message.wParam.ToInt32() - 32);
e.Message = msg;
}
}
// Force mouse movement to either horizontal or
// vertical
void VhmouseHandler(
object sender,
PreTranslateMessageEventArgs e
)
{
// Only look at mouse messages
if (e.Message.message == WM_MOUSEMOVE ||
e.Message.message == WM_LBUTTONDOWN ||
e.Message.message == WM_LBUTTONUP)
{
// If the left mousebutton is pressed and we are
// filtering horizontal or vertical movement,
// make the position the one we're storing
if ((e.Message.message == WM_LBUTTONDOWN ||
e.Message.message == WM_LBUTTONUP)
&& (vMode || hMode))
{
MSG msg = e.Message;
msg.lParam = MakeLParam(ptx, pty);
e.Message = msg;
return;
}
// If the Control key is pressed
if (e.Message.wParam.ToInt32() == MK_CONTROL)
{
// If we're already in "vertical" mode,
// set the horizontal component of our location
// to the one we've stored
// Otherwise we set the internal "x" value
// (as this is the first time through)
if (vMode)
{
MSG msg = e.Message;
msg.lParam =
MakeLParam(
ptx,
HiWord(e.Message.lParam.ToInt32())
);
e.Message = msg;
pty = HiWord(e.Message.lParam.ToInt32());
}
else
ptx = LoWord(e.Message.lParam.ToInt32());
vMode = true;
hMode = false;
}
// If the Shift key is pressed
else if (e.Message.wParam.ToInt32() == MK_SHIFT)
{
// If we're already in "horizontal" mode,
// set the vertical component of our location
// to the one we've stored
// Otherwise we set the internal "y" value
// (as this is the first time through)
if (hMode)
{
MSG msg = e.Message;
msg.lParam =
MakeLParam(
LoWord(e.Message.lParam.ToInt32()),
pty
);
e.Message = msg;
ptx = LoWord(e.Message.lParam.ToInt32());
}
else
pty = HiWord(e.Message.lParam.ToInt32());
hMode = true;
vMode = false;
}
else
// Something else was pressed,
// so cancel our filtering
vMode = hMode = false;
}
}
// Watch for Ctrl-C, and display a message
void WatchCCHandler(
object sender,
PreTranslateMessageEventArgs e
)
{
// Check for the Ctrl-C Windows message
if (e.Message.message == WM_CHAR &&
e.Message.wParam.ToInt32() == 3)
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
doc.Editor.WriteMessage(
"\nCtrl-C is pressed"
);
}
}
// Filter out use of the letter x/X
void NoXHandler(
object sender,
PreTranslateMessageEventArgs e
)
{
// If lowercase or uppercase x is pressed,
// filter the message by setting the
// Handled property to true
if (e.Message.message == WM_CHAR &&
(e.Message.wParam.ToInt32() == 120 ||
e.Message.wParam.ToInt32() == 88))
e.Handled = true;
}
}
}