main.cpp

 #ifndef MAIN_CPP
#define MAIN_CPP

/*
DOCUMENTATION:

Program: USE MENUS WITH NO USER INPUT

Written by: Daniel Vandersluis
E-Mail: dvandersluis@sympatico.ca
ICQ:    57887382
Version: 1.00
Date: 08 June 2002

Included Files: main.cpp - main source file
menu.h - header file containing menu control functions
readme.txt - documentation file

Description:
============

This is a demo program I wrote for myself in order to use a menu item without having to click on
it. This allows the usage of menus in other programs without having to switch to them and using
the menu. This is going to be used in the future as a part of my Autosave program. This is a
program designed to automatically save whatever documents you are working on so that in case of a
crash your work would be saved, even if you forgot to do so manually. Originally written in
Visual Basic, I switched over to C++ (starting with Version 2.00), and therefore had to rewrite
the entire program. In Visual Basic, I had used the SendKeys command to use the menus of other
programs to activate the Save menu item. As this function is not considered a safe way of sending
to different programs, as other programs can accidentally intercept the send, I needed to find a
better way to do it. By directly using the menus of the other programs, this problem is overcome.
If you are interested in Autosave, Version 1.0 and 1.2 can be found on Planet Source Code. I wrote
up to Version 1.4 in Visual Basic, and then switched to C++. The current version is 2.02, and is
not yet available.


Explanation:
============

This program does not include a Graphical or Console user interface, as I did not have a need for
one. In order to use the program, make sure that you have Windows Notepad, on an Untitled
document. On running the program (without modification), an Open File dialog box will open up in
Notepad. You will have to switch to Notepad to see it, the program does not switch windows. To
change what Menu Item you want to use, just change "open" in the FindMenuItem() call to the
caption of whatever menu item you want. This program can easily be modified to include an
interface, or to access menus of other programs; to do that, you just have to get the HWND of the
program you want.


Programming:
============

This program uses advanced API calls, and is documented and commented in full. Redundant comments
are omitted, and refered to previous functions. Programming concepts covered in this program
include message handling, Windows menu API, and memory and string manipulation.
This program was programmed using Microsoft Visual C++ 6 on WindowsXP.


Distribution:
=============

I don't mind if you use part or all of this code within your own programs, however, I ask that
you do not distribute it as your own program and give credit where credit is deserved - Please
either link to my email or to my PSC submission page if you use this code. As well, all PSC
distribution rules apply.


Feedback:
=========

I'd very much appreciate feedback, comments, and/or suggestions you might have. Either log them
through PSC, email them to me, or instant message me and I'll respond as soon as I can.


Special Thanks To:
==================

cryptyk, of the IRC channel #c++ on DALnet for helping me figure out the problem I was having with
the MENUITEMINFO API structure.

*/

#include "menu.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{

// Get the handle to Notepad's window:
HWND hwnd = FindWindow("Notepad", "Untitled - Notepad");

// Create a Menu structure, and fill it with the position of the menu item to be found:
Menu ret = FindMenuItem("open", // caption of the menu item to find
hwnd); // handle to the window

if (ret.sub != -1) // If a menu item was found (if it was -1, none was found)
// Send a WM_COMMAND message to the menu item. This acts as if you had clicked on it with
// the mouse, and will activate any code that should run in that case.

PostMessage(hwnd, // handle to the window
WM_COMMAND, // command to send
MAKELONG(ConvertToID(ret, hwnd), 0),
// WM_COMMAND's WPARAM: a 32 bit variable containing a hiword (the menu
// item's position) and a loword (0, meaning that the command comes from
// a menu item)
NULL);
// WM_COMMAND's LPARAM: not used

return true;
}

#endif

Project Homepage: