Machsupport Forum
Mach Discussion => Mach SDK plugin questions and answers. => Topic started by: rdpdo on May 10, 2011, 05:01:22 PM
-
Hello,
I've try to add a menu whith the code below in myPostInitControl :
{
CFrameWnd *MachFrame = MachView->MachFrame; //Prblem here -> MachFrame = 0x00000
CMenu *menu = MachFrame->GetMenu();
HMENU hSubmenu = CreatePopupMenu();
int pos = FindMenuItem3(menu,"PlugIn Control");
//here we can add menu items to MAch3's menu..
int x = pos;
HMENU control = GetSubMenu( menu->m_hMenu, pos);
InsertMenu ( control, -1, MF_BYPOSITION, RangeStart , _T("kk") );
MachFrame->DrawMenuBar();*/
}
Compling is OK but when I start mach3, I've gort a error triged . After debugging the dll, I saw that MachFrame is equal to 0x00000 so there is a probleme somewhere.... Do you know where is the problem whith this code ?
I've use the "mach3 internal and plugin bible" http://www.google.fr/url?sa=t&source=web&cd=1&ved=0CB0QFjAA&url=http%3A%2F%2Fwww.cnczone.com%2Fforums%2Fattachment.php%3Fattachmentid%3D113858%26d%3D1283534276&rct=j&q=mach3%20plugin%20bible&ei=iafJTZ6aK8TMhAe1h-isDQ&usg=AFQjCNEpq4AGpFKRAqSJRPQAkVSXY1PcwQ&sig2=fiIfMq0sLSH2yqb4P_68Vg&cad=rja
Thanks a lot for your help.
PS : I can add a menu by searching the "Mach3 CNC" window and using his HWND, that works perfectly... but myNotify is not called when I click a menu item under mach3... :( So I dont know hox to do for handle Menu Item click in thsi case ...
-
Yes ! I got it running :)
This is what I've done :
On MyInitControl I initialise RangeStar :
void myInitControl()
{
RangeStart = GetMenuRange( 1 );
}
On MyPostInit I call a thread that will search for MAch3 windows while mach3 is loading. When mach3 is found, it add the menu item :
void myPostInitControl()
{
AfxBeginThread(ProcessFindMach3, NULL);
}
The code for my thred is :
UINT ProcessFindMach3(LPVOID param)
{
do {
EnumWindows(EnumWindowsProc2, NULL);
}while(mach3Wnd==NULL);
mach3CWnd.Attach(mach3Wnd);
dlg = new CProbeDlg;
dlg->Create(IDD_DIALOG_CONFIG,&mach3CWnd);
static bool menued = false;
if( !menued)
{
//" Startup of Menu handler"
menued = true;
if (mach3Wnd != NULL)
{
CMenu *menu = mach3CWnd.GetMenu();
HMENU hSubmenu = CreatePopupMenu();
int pos = FindMenuItem3(menu,"PlugIn Control");
//here we can add menu items to MAch3's menu..
int x = pos;
HMENU control = GetSubMenu( menu->m_hMenu, pos);
InsertMenu ( control, -1, MF_BYPOSITION, RangeStart , _T("Probe") );
mach3CWnd.DrawMenuBar();
}
}
return 1;
}
My Callback routine :
BOOL CALLBACK EnumWindowsProc2(HWND hWnd, LPARAM lParam)
{
char String[255];
if (!hWnd)
return TRUE; // Not a window
if (!::IsWindowVisible(hWnd))
return TRUE; // Not visible
if (!SendMessage(hWnd, WM_GETTEXT, sizeof(String), (LPARAM)String))
return TRUE; // No window title
GetWindowText(hWnd,String,0);
CString toto(String);
if (toto.Find("Mach3 CNC",0) !=-1)
{
mach3Wnd=hWnd;
return 1;
}
return TRUE;
}
And FinWindow :
int FindMenuItem3(CMenu* Menu, LPCTSTR MenuString)
{
ASSERT(Menu);
ASSERT(::IsMenu(Menu->GetSafeHmenu()));
int count = Menu->GetMenuItemCount();
for (int i = 0; i < count; i++)
{
CString str;
if (Menu->GetMenuString(i, str, MF_BYPOSITION) &&
(strcmp(str, MenuString) == 0))
return i;
}
return -1;
}
I have also Add the notify routine ion my .cpp:
extern void myNotify(int ID);
extern "C" __declspec(dllexport) void Notify(int ID)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
myNotify(ID);
//Called when reset is pressed, at end of actual reset commend in Mach3.
//Check the Engine.Estop variable to see if we have reset or not..
}
And in MyNotify I got the message ID when I click on menu item, so I call My Plugin :
void myNotify(int ID)
{
if (ID == RangeStar)
{
....
}
}
-
Sorry, it is not correct because we cannot create a Dlg in a thread... So I must create the dlg outside the thread...
THere is no notify when mach3 is completly loaded ??
-
Ok, I do this and it works :
UINT ProcessFindMach3(LPVOID param)
{
do {
EnumWindows(EnumWindowsProc2, NULL);
}while(mach3Wnd==NULL);
MachWindowFound = true;
return 0;
}
And I create the Dlg in MyUpdate :
void myUpdate()
{
// DbgMsg(("myUpdate entry"));
if (MachWindowFound == true)
{
MachWindowFound = false;
CreateDlgOutside();
}
// DbgMsg(("myUpdate exit"));
}
void CreateDlgOutside(void)
{
mach3CWnd.Attach(mach3Wnd);
dlg = new CProbeDlg;
dlg->Create(IDD_DIALOG_CONFIG,&mach3CWnd);
static bool menued = false;
if( !menued)
{
//" Startup of Menu handler"
menued = true;
if (mach3Wnd != NULL)
{
CMenu *menu = mach3CWnd.GetMenu();
HMENU hSubmenu = CreatePopupMenu();
int pos = FindMenuItem3(menu,"PlugIn Control");
//here we can add menu items to MAch3's menu..
int x = pos;
HMENU control = GetSubMenu( menu->m_hMenu, pos);
InsertMenu ( control, -1, MF_BYPOSITION, RangeStart , _T("Probe") );
mach3CWnd.DrawMenuBar();
}
}
}