Howdy all and anyone who cares to read this. I was hoping you 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. Then send 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 showwindows(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

Why not just use its handle?

string pname="notepad";

foreach (Process theprocess in Myprocess)
{
if (theprocess.ProcessName == pname)
{
postmessage( theprocess.Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
break;
}
}

Thanks jerry for your niffty response....but It seems that the value is still being passed as (short or not HEX) it will only pass a int value.... the value (theprocess.Handle) outputs a small in (1140) this is not eh Real HAndle name it should be 003B0510..any idea to convert the theprocess.handle to a hex and passit or add 0x0 value to the start...?

Well, you can see what happens when you turn the int into a hex.

int x = 123;
string hex = "0x" + x.ToString("X");

Well, you can see what happens when you turn the int into a hex.

int x = 123;
string hex = "0x" + x.ToString("X");

Thankyou so much for replying...I didn't have much faith in the old forums run for help scenario...

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

Yes, because you must use the window handle of the Application:

theprocess.MainWindowHandle

use that one...

Jerry thankvery much for spending the time to try and work this crap out...I really do appreciate it....Hope to do business again...lol..

_r0ckbaer - THANKS - THANKS SO MUCH FOR typing such a quick response and solving it in one statement I can't believe i spent a whole day trying everything including writing about 10 differnt version...and never even noticed the MainWindowHandle.....ARGGGG...IS all can Say....But ONCE Karma +10 to all.. and R

BTW R0ckbaer Nice Tounge...You could rest a Beer can on that handy....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.