Solved

Enable other programs button/menu from your own program.

Posted on 2003-11-10
13
447 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now