Solved

Enable other programs button/menu from your own program.

Posted on 2003-11-10
13
449 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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