Solved

Enable other programs button/menu from your own program.

Posted on 2003-11-10
13
445 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

17 Experts available now in Live!

Get 1:1 Help Now