Solved

Enable other programs button/menu from your own program.

Posted on 2003-11-10
13
455 Views
Last Modified: 2010-04-05
Hi, I'm trying to make a Button Enabler in Delphi, I need some help on how I could enable/disable button's and menu's in other programs.

could anyone show me how I could do this.

thxs
0
Comment
Question by:SaLz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 6

Expert Comment

by:DaFox
ID: 9719224
Hi,

that depends on what buttons you want to disable, e.g. this sample disables the close button close in notepad's caption bar and system menu.

procedure TForm1.Button2Click(Sender: TObject);
var
  wnd: hWnd;
  menu: hMenu;
begin
  wnd := FindWindow('notepad', nil);
  menu := GetSystemMenu(wnd, false);
  if (menu <> 0) then EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND or MF_GRAYED);
end;

But this doesn't work for normal buttons or toolbuttons, of course.

Markus
0
 
LVL 2

Author Comment

by:SaLz
ID: 9719245
how would I be able to enable a menu in another program like

File|View|Grayed|Help

how could I get the menu ungrayed and enabled so I can view the dropdown menu
0
 
LVL 6

Expert Comment

by:DaFox
ID: 9719383
Hi SaLz,

procedure TForm1.Button2Click(Sender: TObject);
var
  wnd: hWnd;
  menu: hMenu;
begin
  wnd := FindWindow('notepad', nil);
  menu := GetMenu(wnd);
  // uIDEnableItem = 2, since "grayed" is the third entry in the menu (zero-based)
  if (menu <> 0) then EnableMenuItem(menu, 2, MF_BYPOSITION or MF_GRAYED);
  // this would gray the first item in "grayed" menu
  if (menu <> 0) then EnableMenuItem(GetSubMenu(menu, 2), 0, MF_BYPOSITION or MF_GRAYED);
end;

To enable the items simply pass MF_ENABLED as param instead of MF_GRAYED. You may also want to take a look at ModifyMenu() API.

Markus
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:SaLz
ID: 9719480
wnd := FindWindow('notepad', nil);
how would I do it for another program?

0
 
LVL 6

Expert Comment

by:DaFox
ID: 9719559
From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindow.asp:

HWND FindWindow(
    LPCTSTR lpClassName,
    LPCTSTR lpWindowName
);

You either have to know the classname or the windowname of your target application. If you don't know it, try to get it with Spy++ or any other aquivalent tool (e.g. www.x-spy.net [it's freeware]).

So,

wnd := FindWindow('anotherClassName', nil);
wnd := FindWindow(nil, 'anotherWindowTitle');
wnd := FindWindow('anotherClassName', 'anotherWindowTitle');

would work as long as the classname or/and windowname is correct.

Markus
0
 
LVL 2

Author Comment

by:SaLz
ID: 9719718
ok am working on my form I've added a "MainMenu1" with these

File | View | Grayed | Help

The Window Class is "TForm1"
The Window Title is "Form1"

I've put this in.

procedure TForm1.Button2Click(Sender: TObject);
var
  wnd: hWnd;
  menu: hMenu;
begin
  wnd := FindWindow(PChar(Edit2.text), PChar(Edit1.text));
  menu := GetMenu(wnd);
  if (menu <> 0) then EnableMenuItem(menu, 2, MF_BYPOSITION or MF_ENABLED);
  if (menu <> 0) then EnableMenuItem(GetSubMenu(menu, 2), 0, MF_BYPOSITION or MF_ENABLED);
end;

I've made a program just now that gets the Window Title & Class name when I go over it with my mouse, I've put the Interval to 2 seconds so it gives me time to press Enable or Disable, I've tryed it on nodepad, wordpad, windows media player it works and on my form and it works.

it doesn't work on Internet Explorer, folders, Outlook Express or DC++ what could be the problem?

Class's
Internet Exploer = IEFrame
Windows Media Player = WMP Skin Host
Outlook Express = Outlook Express Browser Class
DC++ = DC++

what am I doing wrong here? I've put in the class, I've put in the title, but in some programs it doesn't work.
0
 
LVL 6

Accepted Solution

by:
DaFox earned 50 total points
ID: 9722588
>> Internet Exploer,Windows Media Player, Outlook Express, DC++

They're all working with Toolbars instead of normal main menus. Take a look at the child windows of e.g. Internet Explorer (WorkerW -> RebarWindow32 -> ...). As I said before, you have to adjust your code on the design of your target application. If it uses a normal main menu like notepad it's easy. If it uses toolbars it gets more complicated. And last not least you always have to know the classname (and classnames of the childwindows). For MS products they vary with different versions.

Markus
0
 
LVL 2

Author Comment

by:SaLz
ID: 9727303
ok, thxs
0
 
LVL 2

Author Comment

by:SaLz
ID: 9732074
I have another question on this, once I have enabled the menu now do I enable the submenu if they are disabled as well in form1
0
 
LVL 2

Author Comment

by:SaLz
ID: 9732135
like
File | View | Grayed | Help
                 Menu 1
                 Menu 1
                 Grayed
                 Menu 1
                 Grayed

How could I get the drop down menu enabled as above? so its not just 1 there all enabled



0
 
LVL 2

Author Comment

by:SaLz
ID: 9732175
nm, I've sorted it :) I luv ya :)
0
 
LVL 2

Author Comment

by:SaLz
ID: 9732293
Would you know how to enable Buttons? in the same manner?

asume that I have the class and title how would I do this?

I have made a program that will get the class and title just how to enable a button or other things like that?
0
 
LVL 6

Expert Comment

by:DaFox
ID: 9732640
Assuming you are talking about "normal" buttons, just use EnableWindow() API.

Markus
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question