|
-
May 4th, 2008, 06:03 AM
#1
If Process found by Name then Send Minimize Window Command.
Howdy all and anyone who cares to read this. I was hoping somoeone could help me with one small issue I am having. OK to cut a long story short...
I need to be able to search through all running process.id and then sendmessage(hwnd, blah,blah, MIN_WINDOW);
In other words I need find notepad by name in the running processes and then get the Handle for it window. Then send a message to that handle to minimize the window or Set the Window Position.
Now my problem is that when I get the Handle using:
string pname="notepad";
foreach (Process theprocess in Myprocess)
{
if (theprocess.ProcessName == pname)
{
textBox1.Text = theprocess.Id.ToString();
//_________________________________//
AS you can see that the textbox1.text will show that the handle value(process.id) is an int. Now when I pass it to the showwindow(hwnd.......). It's not the Hexadecimal value so it doesn’t exist...It seem that when it's pulled using the Process[] method it converts it to a short type of Int. Please is there away to collect the Process.Id and the pass it to Handle name as Hex...
int handle = 0x00D7A;
postmessage(handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
Now this would work if a window with that PID exist...Which It did AS I used WinSpy to get the value for notepad then check it and it worked...but soon as I pass the handle via the above collected method it only pass a short version of the handle. I need to convert it to HEX to pass it. Please can you point me in the right direction...?
Cheers
Sound
PS. Quick Update. Basiclly need to Collect the Handle from the Process(Say Notepad.exe(say Handle 0005130E) then pass it a MinWIndow Command. Problem the method use to get Handle form process name is only returns a small PID(Int(3500) when it should be 0x130E <- that is the vaule i need to pass which is just 0005130E then i have knocked off the first 4(?) chars and manully enterd "0x" on the start of Handle. So how can it get the HEx of a Handle and use it to sendmessge(HANDLE(PID)) - <as example
-
May 4th, 2008, 07:31 AM
#2
Re: If Process found by Name then Send Minimize Window Command.
 Originally Posted by Soundgarden
...In other words I need find notepad by name in the running processes and then get the Handle for it window....
If you oly want to open a notepad why not using the simple Shell32 api call ?
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 4th, 2008, 07:50 AM
#3
Re: If Process found by Name then Send Minimize Window Command.
Cheers Jonny for your quick response.
I was just using the Notepad as an example...The idea is that if a program(Variable) is running.... then on click send message to Minimize. In other words. I can search for the process and make sure it's running then get is PID(Handle) and send it a 0xF020(sc_minimize).
Now the problem i have seems to be convertsing intprt to int....I think...when i use the process.process.id as int and then try and pass it to showwindow(hwnd) it onley gets a decminal version of the process.id not the Hex(0x)
Here ther basic Code for what I need....The basic Premiss is that when the prog is run, it checks for given program names as varible in running process list then gets the Handle using this method
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(
int hWnd, // window handle
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags); // window positioning flags
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
int clicked = 0;
string pname = "notepad";
int whandle;
string whandled;
void checkproc(string pname)
{
int SW_SHOWNOACTIVATE = 4;
int HWND_TOPMOST = -1;
uint SWP_NOACTIVATE = 0x0010;
clicked++;
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
if (theprocess.ProcessName == pname)
{
// int dothiswin=0x0104A;
ShowWindow(theprocess.Handle, SW_SHOWNOACTIVATE);
MessageBox.Show(theprocess.Handle.ToString());
SetWindowPos(theprocess.Handle.ToInt32(), HWND_TOPMOST, 100, 100, 100, 100, SWP_NOACTIVATE);
break;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = whandle.ToString();
checkproc(pname);
}
this is a quick idea of what i want to happen but setWindowPos(THEPROCESS.HANDLE..... is the problem.....Any Idea....Thanks for your time......BTW
Last edited by Soundgarden; May 4th, 2008 at 01:43 PM.
-
May 4th, 2008, 02:08 PM
#4
Re: If Process found by Name then Send Minimize Window Command.
 Originally Posted by Soundgarden
...Now the problem i have seems to be convertsing intprt to int....I think...when i use the process.process.id as int and then try and pass it to showwindow(hwnd) it onley gets a decminal version of the process.id not the Hex(0x)
IMHO you have some basic misunderstoods of an IntPtr and an integer and what hex means.
Hex is nothing then a notation form.
in computing all vlues are basically bits and bytes and you can notate a given number in binary, hex, decimal, octal and it still will have the same bit pattern.
'int' is an alias for Int32 which has a size of 4 Bytes in C# An IntPtr instead is system depending and is a pointer to an memory adress.
IMHO your pointer simplemaybe isn't correctly converted and this all has nothing to do with hex or not hex.
I would use the debugger and looking step by step what happens to your adress pointer.
Whats the sense behind all this-? You want to miimize applictions not started by the same proram ?
And - Please use code tags. ! ( look into the bottom of my post how this is done.) Otherwise your code losts format and is too difficult to read.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 4th, 2008, 02:42 PM
#5
Re: If Process found by Name then Send Minimize Window Command.
Thanks Johnny for input....Very handy...I realised that i had my wording very wrong....but you seem to get the message....anyway It turned out that i needed to use the MainWindowHandle instead of theprocess.Handle....
Yes it's part of a bigger picture to Min-Hide whatever a process that is running...Solved...now...I think....Cross legs and Ear Lobes...
Thanks for you response..
-
May 4th, 2008, 03:12 PM
#6
Re: If Process found by Name then Send Minimize Window Command.
 Originally Posted by Soundgarden
Thanks Johnny for input....Very handy...I realised that i had my wording very wrong....but you seem to get the message....anyway It turned out that i needed to use the MainWindowHandle instead of theprocess.Handle....
Yes it's part of a bigger picture to Min-Hide whatever a process that is running...Solved...now...I think....Cross legs and Ear Lobes...
Thanks for you response..
Yes you are correc and here is the code
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsApplication1 {
public partial class Form1 : Form {
private int _clicked = 0;
private string _pName = "notepad";
//private int _wHandle;
//private string _wHandled;
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
//textBox1.Text = _wHandle.ToString();
if (textBox1.Text != string.Empty) {
checkProc(textBox1.Text);
}
}
void checkProc(string pname) {
int SW_SHOWNOACTIVATE = 4;
int HWND_TOPMOST = -1;
uint SWP_NOACTIVATE = 0x0010;
_clicked++;
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist) {
if (theprocess.ProcessName == pname) {
// int dothiswin=0x0104A;
API.ShowWindow(theprocess.MainWindowHandle, SW_SHOWNOACTIVATE);
MessageBox.Show(theprocess.MainWindowHandle.ToString());
API.SetWindowPos(theprocess.MainWindowHandle, HWND_TOPMOST, 100, 100, 100, 100, SWP_NOACTIVATE);
break;
}
}
}
}
}
And I did the API calls in a separated class
Code:
namespace WindowsApplication1 {
public class API {
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(
IntPtr hWnd, // window handle
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags); // window positioning flags
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
}
This works ( tested) in this case you write the applictions name into the textbox and then press button click. BTW look at the SetWindowPos. Simple use an IntPtr there too !
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 4th, 2008, 03:55 PM
#7
Re: If Process found by Name then Send Minimize Window Command.
An additional hint:
If your messagebox should show you adresses in hex then simple use
Code:
MessageBox.Show(theprocess.MainWindowHandle.ToString("X"));
Another additional point to know is that lots of modern applications doesn't have The Applications MainWindow on the screen. For example try your application with word 2003 and you will see it will not work.
Winword doesn't find it.
Last edited by JonnyPoet; May 4th, 2008 at 04:37 PM.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 4th, 2008, 04:37 PM
#8
Re: If Process found by Name then Send Minimize Window Command.
Dude I Just Finished the Code....and then Had a Look Back here to keep in touch.....@@#%$@# You needed not go to so much Effort...Bloody hell....I really didn;t expect this much help.....Very many Karma points....Heading your way...and a SIX pack of VB (Victoria Bitter - Aus- Beer)...lol..
I'm l on my way with the code and should have an app put to the(open souce) public...hopefully tomorrow...I should be a handy...to some....A lot of my friends CO worker have been bugging me to compelete this project proggy...You know what it's like....Thats very much for the tips....Lovely
-
May 4th, 2008, 04:43 PM
#9
Re: If Process found by Name then Send Minimize Window Command.
 Originally Posted by Soundgarden
You needed not go to so much Effort...Bloody hell....I really didn;t expect this much help.....
Wasn't too much work was 10 minutes debugging your code. But obviously bit too late as I was offline some time and havn't looked to the CG.
Look to the additional notes !!
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 4th, 2008, 04:52 PM
#10
Re: If Process found by Name then Send Minimize Window Command.
Its funny how great minds..Well maybe yours...
Last edited by Soundgarden; May 4th, 2008 at 04:58 PM.
-
May 4th, 2008, 05:40 PM
#11
Re: If Process found by Name then Send Minimize Window Command.
 Originally Posted by Soundgarden
Its funny how great minds..Well maybe yours...
Thx for the flowers.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|