Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

"Shapes", sample C-hook program from Developer's Guide


Recommended Posts

Hello, all.

 

Since my last post on 04/22/11, I've been working on another C-hook sample programs, "revctr" and "Shapes"

from Developer's Guide, Ver. 9.0 (Beta 2).

"revctr" was not so difficult because no GUI (dialog) was used.

 

However, "Shapes" was really a tough one for me, I had to spend so many hours to make it work.

On top of that, I'm new to MFC, so I had to learn basics of MFC concurrently.

I'm reading a book "Introductions to MFC Programming with Visual C++", kind of old, targeted

VC++6.0,but good for understanding the basics.

 

It was just yesterday, "Shapes" finally started to work.

No pain no gain, indeed...

 

I have a couple of questions.

 

(1) How do I use "m_notify" function in my C-hook program?

In this case, I'm using repaint" and" fit", whenever I move my mouse a messagebox is displayed and asks to hit OK button.

I know it is made so, but I even cannot do my operation because messagebox appears one after another, kind of pesky...

If a message box goes away by itself in a second or two, maybe OK.

Besides that, when a C-hook is done, come back to Mastercam, it seems "m_notify" still in memory, and keeps reacting.

I could be wrong though...

 

Here is the code snippet.

extern "C" __declspec(dllexport) int m_notify (int notify_code)
{

MC_RETURN result = MC_NOERROR;
switch (notify_code)
	{
  case MCEVENT_REPAINT:
	//	result = HandleRepaint();
	//  ::MessageBox (NULL, (LPCTSTR)"Repaint", (LPCTSTR)"M_repaint", MB_OK | MB_ICONINFORMATION);
	break;

  case MCEVENT_FIT:
	//	result = HandleFit();
	//  ::MessageBox (NULL, (LPCTSTR)"Fit screen", (LPCTSTR)"M_fit_screen", MB_OK | MB_ICONINFORMATION);
	break;
	}

return MC_NOERROR;
}

 

(2) In "Shapes" dialog, icons are used. The original background color is green, but when my C-hook starts, it's black.

I couldn't see the shape, so I inverted the color for the shape, but the background color stays black.

Maybe, I just don't know how to edit color for background.

 

I tried to insert image files, *.png, but not successful.

 

 

--------------------------------------------------------------------------------------------------------------------

Roger,

Hello.

 

I would appreciate if you could give me some pieces of advice because "shapes.txt" file is not used anymore,

so I modified the program my own way.

I know you are busy, but if you could check/verify my "Shape" program, I can send it to you by e-mail,

now I know which files I have to send, no more fat e-mail attachment(s). :)

-------------------------------------------------------------------------------------------------------------------

 

Any advice will be appreciated.

Thank you.

Link to comment
Share on other sites
How do I use "m_notify" function in my C-hook program?

Notify is rarely needed, but you can certainly use it – if your CHook needs to.

The messagebox(es) were just for demonstration that the specific notify callback occurred.

Of course you would not have these in a production CHook.

 

Besides that, when a C-hook is done, come back to Mastercam, it seems "m_notify" still in memory, and keeps reacting.

Correct, when you “run” a CHook, Mastercam loads it to memory and does not unload it unless specifically requested to.

Meaning it is still loaded and will receive m_notify callbacks.

So whatever you are doing in a callback notification, make sure that it happens fast!

You can “ask” Mastercam tounload your DLL by the return value to Mastercam ->

return MC_NOERROR | MC_UNLOADAPP;

*Note that I said “ask”, because if your CHook was loaded using an FT file, it will not be unloaded until Mastercam shuts down.

 

In "Shapes" dialog, icons are used.

Yes, but you could use BMPs. That project is using a CImageList control to hold the images.

How you “create” the imagelist will determine how the images look.

Yes – It is very confusing, but that’s Microsoft Windows and MFC. :unsure:

 

 

And check your mail...

Link to comment
Share on other sites

Roger,

 

Hello. Thanks for the e-mail and attached *.zip file.

 

Wow, I was more than I expected that you would send me a whole C-hook project file.

Unzipped, compiled/linked, everything is working just fine. It made me smile.

 

Now, I can compare your program and mine, and I will see the difference, also learn

ideal C-hook programming using MFC.

 

Although I haven't checked in detail yet, I noticed right away that your were using "String Table."

I was using string arrays and pointers, nothing fancy, typical "C" (not even C++) programming style.

 

Again, thank you very much for your help every time, appreciated.

Link to comment
Share on other sites

Roger,

 

Hello.

 

Since you have to use MFC, you may as well use (some of) the tools it provides, like CString.

 

Right, I will try to.

 

A couple of things I would like to bring up.

 

(1) I've been comparing your C-hook program and mine, and found why I had a problem with

icons, (background color was black, so I had to invert color for shape), in List control.

After modified the program, icons were displayed correctly. Problem has been solved.

 

//	m_shape_image.Create (32, 32, 0, 0, 8);  <-- Original code from old Developer's Guide
//	c_shape_image.Create(32, 32, ILC_COLOR16 | ILC_MASK, 4, 1); <-- your original code.
m_shape_image.Create(32, 32, ILC_COLOR16 | ILC_MASK, 4, 1); <-- renamed for my program

   icon[0] = AfxGetApp()->LoadIcon(IDI_RECTANGLE);
   icon[1] = AfxGetApp()->LoadIcon(IDI_OBROUND);
   icon[2] = AfxGetApp()->LoadIcon(IDI_SINGLE_D);
   icon[3] = AfxGetApp()->LoadIcon(IDI_DOUBLE_D);

   for (i = 0; i < N_SHAPES; i++)
       m_shape_image.Add (icon[i]);

 

(2) A question about using "OK",IDOK(Button Control) and "Cancel",IDCANCEL(Button Control) in dialog.

 

I'm trying to switch to OK, check mark in green, and Cancel buttons, cross in red, to Mastercam

default bitmaps, but for some reason, it doesn't show bitmaps when executed.

I'm sure I changed OK & Cancel button settings in each property page "Bitmap" to "True", and

checked "Owner Draw" was set to "false."

 

For a test, I made a C-hook program generated by C-hook Wizard, using Dialog sample program,

there is no problem at all. I see Mastercam OK and Cancel bitmaps.

 

I also made a C-hook program without a dialog, then manually added a dialog, and changed

settings for OK and Cancel buttons, it works fine.

 

I looked a *.h, *.rc and *.cpp files, but no luck...

Am I still missing something?

 

Thanks for your help and advice.

Link to comment
Share on other sites
For a test, I made a C-hook program generated by C-hook Wizard, using Dialog sample program,

there is no problem at all. I see Mastercam OK and Cancel bitmaps.

 

I also made a C-hook program without a dialog, then manually added a dialog, and changed

settings for OK and Cancel buttons, it works fine.

The OK, Cancel, Apply and Help buttons are "special" on an MCDialog.

More correctly, I should really say that these "IDs" are special;

IDOK

IDCANCEL

ID_APPLY_NOW

IDHELP

 

They auto-magically get the appropriate BMP image assigned to them.

 

These IDs would not be defined in the project's resource.h file,

as all of them are already defined by Windows.

All except for ID_APPLY_NOW in WinUser.h, which is defined in afxres.h

 

In the .RC file, the buttons would bedefined as such ->

 

IDD_MyDialog DIALOGEX 0, 0, 216, 101
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Sans Serif", 0, 0, 0x0
BEGIN
   LTEXT           "MCReal 1",IDC_STATIC,21,15,36,8
   EDITTEXT        IDC_W_EDIT,159,12,50,14,ES_AUTOHSCROLL
   LTEXT           "MCReal 2",IDC_STATIC,21,33,32,8
   EDITTEXT        IDC_H_EDIT,159,30,50,14,ES_AUTOHSCROLL
   PUSHBUTTON      "OK",IDOK,37,76,36,18,BS_BITMAP
   PUSHBUTTON      "Cancel",IDCANCEL,117,76,36,18,BS_BITMAP
   PUSHBUTTON      "Apply",ID_APPLY_NOW,76,76,36,18,BS_BITMAP
   PUSHBUTTON      "Help",IDHELP,157,77,32,17,BS_BITMAP
END

*** One thing to look out for which can bite you... :o

Buttons with these IDS on an MCDialog MUST be set to this size -> 36,18

If one is not the "correct" size, it will work OK in a Release DLL,

but when you run a DEBUG build of your CHook, you'll get an "Debug Assertion Failed"

when this line is executed -> MCDialog::OnInitDialog();

Yes, it's a bit harsh, but if you want the magic, you have to play by the rules.

This to make sure that the buttons are the correct and consistent size.

 

*Note the X,Y size of the IDHELP button in the code sample - it's not correct.

You will get the the correct BMP on the button and it will work in Release mode,

but you will get an Assertion running in Debug mode.

 

 

As for your missing Bitmap issue...

If the button definitions in you RC file look correct and you not tried to re-define those IDs,

is your Dialog really an MCDialog ?

 

In the .h file for your dialog, check the declaration ->

class CMyDialog : public MCDialog

Link to comment
Share on other sites

Roger,

 

Hello. Thanks for your prompt reply.

 

PUSHBUTTON      "OK",IDOK,..,..,36,18,BS_BITMAP
PUSHBUTTON      "Cancel",IDCANCEL,..,..,36,18,BS_BITMAP

 

I checked these two lines ,"...36,18,BS_BITMAP", in *.rc file, but they were OK.

Problem still persists...

 

is your Dialog really an MCDialog ?

 

In the .h file for your dialog, check the declaration ->

class CMyDialog : public MCDialog

 

class Cshapedlg : public MCDialog

 

I checked "shapedlg.h", it was OK.

 

BOOL Cshapedlg::OnInitDialog() 
{
...  <-- skipped some lines

   //CDialog::OnInitDialog();
   MCDialog::OnInitDialog();
 Matching lines: 13    Matching files: 2    Total files searched: 12

 

Then, I searched a string "CDialog" in "shapedlg.cpp", and found it.

Changed from "CDialog" to "MCDialog" and rebuild the program and tested.

I saw both OK and Cancel Mastercam bitmaps!

Problem solved.

 

Find all "CDialog", Subfolders, Keep modified files open, Find Results 1, "Entire Solution"

 F:\Mastercam_C-hook_related2\shapes\shapedlg.cpp(66)://    CDialog::DoDataExchange(pDX);
 F:\Mastercam_C-hook_related2\shapes\shapedlg.cpp(67):    MCDialog::DoDataExchange(pDX);
 F:\Mastercam_C-hook_related2\shapes\shapedlg.cpp(179):    //CDialog::OnInitDialog();
 F:\Mastercam_C-hook_related2\shapes\shapedlg.cpp(180):    MCDialog::OnInitDialog();

 ... <-- skipped some lines

 Matching lines: 13    Matching files: 2    Total files searched: 12

 

To make sure whether or not there were some more "CDialog" exist in my entire solution,

searched again. Yes, there were bunches of them.

 

Well, I learned a lot this time, too.

There is a will, there is a way.

 

Thanks for your advice, really appreciated.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...