Running control.exe as process does not WaitForExit() - waitforexit

I am running control.exe as a process. (Windows 7 OS, .NET 3.5, C#). It does not stop at WaitForExit() as expected. It immediately "exits" the process even though the control.exe window is still open. I have tried the process.Exited event also and that is triggered before the application exits as well.
Here is my code:
Process process = new Process();
process.StartInfo.FileName = #"c:\windows\system32\control.exe";
process.StartInfo.Arguments = #"userpasswords";
process.Start();
process.WaitForExit();

Send from phone (please correct my formatting, would take hours by phone)
Try to check if window is visible or task is running like:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace yournamespace
{
class FormCheck
{
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, StringBuilder lParam);
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
public static bool IsRunning(string window, bool exactfit)
{ return Check(window,exactfit); }
public static bool IsRunning(string window)
{ return Check(window); }
public static bool Check(string Processname)
{
Process[] pname = Process.GetProcessesByName(Processname);
if (pname.Length <= 1) { return false; } else { return true; }
}
public static bool Check(string WindowTitle, bool exactfit)
{
bool response = false;
string[] strWindowsTitles = EnumerateOpenedWindows.GetDesktopWindowsTitles();
string strResponse = "";
foreach (string strTitle in strWindowsTitles)
{
if (strTitle.Contains(WindowTitle))
{
if (exactfit)
{
if (strTitle == WindowTitle)
{
strResponse = strTitle;
break;
}
}
else
{
if (strTitle.Contains(WindowTitle))
{
strResponse = strTitle;
break;
}
}
}
}
string pid = string.Empty;
if (strResponse != "")
{
response = true;
}
else { response = false; }
return response;
}
}
public class EnumerateOpenedWindows
{
const int MAXTITLE = 255;
private static List<string> lstTitles;
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop,
EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int _GetWindowText(IntPtr hWnd,
StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
{
string strTitle = GetWindowText(hWnd);
if (strTitle != "" & IsWindowVisible(hWnd)) //
{
lstTitles.Add(strTitle);
}
return true;
}
/// <summary>
/// Return the window title of handle
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
public static string GetWindowText(IntPtr hWnd)
{
StringBuilder strbTitle = new StringBuilder(MAXTITLE);
int nLength = _GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
strbTitle.Length = nLength;
return strbTitle.ToString();
}
/// <summary>
/// Return titles of all visible windows on desktop
/// </summary>
/// <returns>List of titles in type of string</returns>
public static string[] GetDesktopWindowsTitles()
{
lstTitles = new List<string>();
EnumDelegate delEnumfunc = new EnumDelegate(EnumWindowsProc);
bool bSuccessful = EnumDesktopWindows(IntPtr.Zero, delEnumfunc, IntPtr.Zero); //for current desktop
if (bSuccessful)
{
return lstTitles.ToArray();
}
else
{
// Get the last Win32 error code
int nErrorCode = Marshal.GetLastWin32Error();
string strErrMsg = String.Format("EnumDesktopWindows failed with code {0}.", nErrorCode);
throw new Exception(strErrMsg);
}
}
}
}
By using one of the methods of "isrunning" like:
int waiter = 0;
while (isRunning("Control")
{
//add 1 to an value and set to zero after x counts
waiter++;
if (waiter == 1000) { waiter=0; }
}
Your program will stuck until control shut down. Escaping sequences should inserted if control would never shut down again ;)

Related

Bring Window To Foreground When MainWindowHandle Is 0

The following code brings the window to the foreground, if the MainWindowHandle is not 0.
How can I bring a window to the front that has MainWindowHandle = 0?
This is for the Microsoft Excel - Compatibility Checker window that shows a GUI but does not have an icon in the task bar and has MainWindowHandle = 0.
I have no other instances of Excel running.
Add-Type #"
using System;
using System.Runtime.InteropServices;
public class Tricks {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"#
$excel = (Get-Process | Where-Object { $_.ProcessName -eq 'EXCEL' }).MainWindowHandle
[void] [Tricks]::SetForegroundWindow($excel)
In Windows Task Manager, I can right click on 'Microsoft Excel - Compatibility Checker' and click on "Bring To Front" and that works. How can I mimic this functionality in Powershell?
Thanks to IInspectable for pointing me in the right direction.
This code gets the real MainWindowHandle value:
$TypeDef2 = #"
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Api
{
public class WinStruct
{
public string WinTitle {get; set; }
public int MainWindowHandle { get; set; }
}
public class ApiDef
{
private delegate bool CallBackPtr(int hwnd, int lParam);
private static CallBackPtr callBackPtr = Callback;
private static List<WinStruct> _WinStructList = new List<WinStruct>();
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(CallBackPtr lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
private static bool Callback(int hWnd, int lparam)
{
StringBuilder sb = new StringBuilder(256);
int res = GetWindowText((IntPtr)hWnd, sb, 256);
_WinStructList.Add(new WinStruct { MainWindowHandle = hWnd, WinTitle = sb.ToString() });
return true;
}
public static List<WinStruct> GetWindows()
{
_WinStructList = new List<WinStruct>();
EnumWindows(callBackPtr, IntPtr.Zero);
return _WinStructList;
}
}
}
"#
Add-Type -TypeDefinition $TypeDef2 -Language CSharpVersion3
$excelInstance = [Api.Apidef]::GetWindows() | Where-Object { $_.WinTitle.ToUpper() -eq "Microsoft Excel - Compatibility Checker".ToUpper() }
So now using this correct value, I can call the SetForegroundWindow() function:
Add-Type #"
using System;
using System.Runtime.InteropServices;
public class Tricks {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"#
[void] [Tricks]::SetForegroundWindow($excelInstance.MainWindowHandle)
I wrote a detailed blog about this on my website.
I've put up a full example on GitHub of how to create an Excel file, edit it and run the above code in a different thread which you have to do because the Excel popup blocks the main thread.

XGrabKey not working

I'm currently working on a media key recognition for my application AudioCuesheetEditor. The application uses mono and gtk#. So I made wrapper for direct access to XGrabKey on linux (on windows I use the SetWindowsHookEx). But now the filter function doesn't get called, and I have no idea, why?!
class code:
public class HotkeyListener
{
#if BUILD_FOR_WINDOWS
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
public const int VK_MEDIA_STOP = 0xB2;
private LowLevelKeyboardProc _proc;
private static IntPtr _hookID = IntPtr.Zero;
#endif
#if BUILD_FOR_LINUX
private const int KeyPress = 2;
private const int GrabModeAsync = 1;
public const int VK_MEDIA_PLAY_PAUSE = 172;
public const int VK_MEDIA_NEXT_TRACK = 153;
public const int VK_MEDIA_PREV_TRACK = 144;
public const int VK_MEDIA_STOP = 164;
#endif
private static readonly Logfile log = Logfile.getLogfile(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private int keyCode;
public HotkeyListener(int vkCode)
{
#if BUILD_FOR_WINDOWS
_proc = HookCallback;
#endif
this.keyCode = vkCode;
}
public event HotKeyEvent HotkeyPressed;
public void Register()
{
#if BUILD_FOR_WINDOWS
_hookID = SetHook(_proc);
#endif
#if BUILD_FOR_LINUX
Gdk.Window rootWin = Gdk.Global.DefaultRootWindow;
IntPtr xDisplay = GetXDisplay(rootWin);
XGrabKey(xDisplay,this.keyCode,(uint)Gdk.ModifierType.ModifierMask,GetXWindow(rootWin),false,GrabModeAsync,GrabModeAsync);
#endif
}
public void Unregister()
{
#if BUILD_FOR_WINDOWS
UnhookWindowsHookEx(_hookID);
#endif
#if BUILD_FOR_LINUX
Gdk.Window rootWin = Gdk.Global.DefaultRootWindow;
IntPtr xDisplay = GetXDisplay(rootWin);
XUngrabKey(xDisplay,this.keyCode,(uint)Gdk.ModifierType.ModifierMask,GetXWindow(rootWin));
#endif
}
public delegate void HotKeyEvent();
#if BUILD_FOR_WINDOWS
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
log.debug("vk = " + vkCode);
if ((this.HotkeyPressed != null) && (this.keyCode == vkCode))
{
this.HotkeyPressed();
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endif
#if BUILD_FOR_LINUX
private Gdk.FilterReturn FilterFunction(IntPtr xEvent, Gdk.Event evnt)
{
XKeyEvent xKeyEvent = (XKeyEvent)Marshal.PtrToStructure(xEvent, typeof(XKeyEvent));
if (xKeyEvent.type == KeyPress)
{
log.debug("vk = " + xKeyEvent.keycode);
if (xKeyEvent.keycode == this.keyCode)
{
this.HotkeyPressed();
}
}
return Gdk.FilterReturn.Continue;
}
private static IntPtr GetXWindow(Gdk.Window window)
{
return gdk_x11_drawable_get_xid(window.Handle);
}
private static IntPtr GetXDisplay(Gdk.Window window)
{
return gdk_x11_drawable_get_xdisplay(gdk_x11_window_get_drawable_impl(window.Handle));
}
[DllImport("libgtk-x11-2.0")]
private static extern IntPtr gdk_x11_drawable_get_xid(IntPtr gdkWindow);
[DllImport("libgtk-x11-2.0")]
private static extern IntPtr gdk_x11_drawable_get_xdisplay(IntPtr gdkDrawable);
[DllImport("libgtk-x11-2.0")]
private static extern IntPtr gdk_x11_window_get_drawable_impl(IntPtr gdkWindow);
[DllImport("libX11")]
private static extern int XGrabKey(IntPtr display, int keycode, uint modifiers, IntPtr grab_window, bool owner_events, int pointer_mode, int keyboard_mode);
[DllImport("libX11")]
private static extern int XUngrabKey(IntPtr display, int keycode, uint modifiers, IntPtr grab_window);
[StructLayout(LayoutKind.Sequential)]
internal struct XKeyEvent
{
public int type;
public ulong serial;
public int send_event;
public IntPtr display;
public ulong window;
public ulong root;
public ulong subwindow;
public ulong time;
public int x, y;
public int x_root, y_root;
public uint state;
public uint keycode;
public int same_screen;
}
#endif
}
I made a breakpoint inside FilterFunction, but the system didn't break :(.
Usage for hotkeylistener:
this.hotkeyMediaPlayListener = new HotkeyListener(HotkeyListener.VK_MEDIA_PLAY_PAUSE);
this.hotkeyMediaPlayListener.HotkeyPressed += delegate {
if (this.objProgram.getObjOption().getBMediakeyboardHotkeyRecognition() == true)
{
if (this.objProgram.getAudioManager().PlayStatus != AudioCuesheetEditor.AudioBackend.PlayState.Playing)
{
this.objProgram.getAudioManager().play();
}
else
{
this.objProgram.getAudioManager().pause();
}
}
};
this.hotkeyMediaPlayListener.Register();
The full project code can be viewed here: http://sourceforge.net/p/audiocuesheet/code/HEAD/tree/
Thanks for everybody helping me.
Solved the problem myself, I didn't attach the filter function to the root window in constructor. Here the code, for all:
public class HotkeyListener
{
#if BUILD_FOR_WINDOWS
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
public const int VK_MEDIA_STOP = 0xB2;
private LowLevelKeyboardProc _proc;
private static IntPtr _hookID = IntPtr.Zero;
#endif
#if BUILD_FOR_LINUX
private const int KeyPress = 2;
private const int GrabModeAsync = 1;
public const int VK_MEDIA_PLAY_PAUSE = 172;
public const int VK_MEDIA_NEXT_TRACK = 153;
public const int VK_MEDIA_PREV_TRACK = 144;
public const int VK_MEDIA_STOP = 164;
#endif
private static readonly Logfile log = Logfile.getLogfile(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private int keyCode;
public HotkeyListener(int vkCode)
{
#if BUILD_FOR_WINDOWS
_proc = HookCallback;
#endif
Gdk.Window rootWin = Gdk.Global.DefaultRootWindow;
this.keyCode = vkCode;
rootWin.AddFilter(new Gdk.FilterFunc(FilterFunction));
}
public event HotKeyEvent HotkeyPressed;
public void Register()
{
#if BUILD_FOR_WINDOWS
_hookID = SetHook(_proc);
#endif
#if BUILD_FOR_LINUX
Gdk.Window rootWin = Gdk.Global.DefaultRootWindow;
IntPtr xDisplay = GetXDisplay(rootWin);
foreach(Gdk.ModifierType mod in Enum.GetValues(typeof(Gdk.ModifierType)))
{
XGrabKey(xDisplay,this.keyCode,(uint)mod,GetXWindow(rootWin),false,GrabModeAsync,GrabModeAsync);
}
#endif
}
public void Unregister()
{
#if BUILD_FOR_WINDOWS
UnhookWindowsHookEx(_hookID);
#endif
#if BUILD_FOR_LINUX
Gdk.Window rootWin = Gdk.Global.DefaultRootWindow;
IntPtr xDisplay = GetXDisplay(rootWin);
foreach(Gdk.ModifierType mod in Enum.GetValues(typeof(Gdk.ModifierType)))
{
XUngrabKey(xDisplay,this.keyCode,(uint)mod,GetXWindow(rootWin));
}
#endif
}
public delegate void HotKeyEvent();
#if BUILD_FOR_WINDOWS
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
log.debug("vk = " + vkCode);
if ((this.HotkeyPressed != null) && (this.keyCode == vkCode))
{
this.HotkeyPressed();
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endif
#if BUILD_FOR_LINUX
private Gdk.FilterReturn FilterFunction(IntPtr xEvent, Gdk.Event evnt)
{
XKeyEvent xKeyEvent = (XKeyEvent)Marshal.PtrToStructure(xEvent, typeof(XKeyEvent));
if (xKeyEvent.type == KeyPress)
{
log.debug("vk = " + xKeyEvent.keycode);
if ((this.HotkeyPressed != null) && (this.keyCode == xKeyEvent.keycode))
{
if (this.HotkeyPressed != null)
this.HotkeyPressed();
}
}
return Gdk.FilterReturn.Continue;
}
private static IntPtr GetXWindow(Gdk.Window window)
{
return gdk_x11_drawable_get_xid(window.Handle);
}
private static IntPtr GetXDisplay(Gdk.Window window)
{
return gdk_x11_drawable_get_xdisplay(gdk_x11_window_get_drawable_impl(window.Handle));
}
[DllImport("libgtk-x11-2.0")]
private static extern IntPtr gdk_x11_drawable_get_xid(IntPtr gdkWindow);
[DllImport("libgtk-x11-2.0")]
private static extern IntPtr gdk_x11_drawable_get_xdisplay(IntPtr gdkDrawable);
[DllImport("libgtk-x11-2.0")]
private static extern IntPtr gdk_x11_window_get_drawable_impl(IntPtr gdkWindow);
[DllImport("libX11")]
private static extern int XGrabKey(IntPtr display, int keycode, uint modifiers, IntPtr grab_window, bool owner_events, int pointer_mode, int keyboard_mode);
[DllImport("libX11")]
private static extern int XUngrabKey(IntPtr display, int keycode, uint modifiers, IntPtr grab_window);
[StructLayout(LayoutKind.Sequential)]
internal struct XKeyEvent
{
public int type;
public ulong serial;
public int send_event;
public IntPtr display;
public ulong window;
public ulong root;
public ulong subwindow;
public ulong time;
public int x, y;
public int x_root, y_root;
public uint state;
public uint keycode;
public int same_screen;
}
#endif
}

How to create extension for VS2012 using VS Package on File Save Event

I need to create an extension to do some custom activity, on File Save Event in visual studio. How to achieve this using VS Package.
I am struck with below code, any advise?
#region Package Members
FileEventsListener listener = null;
protected override void Initialize()
{
Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
base.Initialize();
listener = new FileEventsListener();
}
#endregion
}
public class FileEventsListener : IPersistFileFormat, IDisposable
{
private IVsSolution solution;
private uint solutionEventsCookie;
private bool isDirty;
private string fileName;
private const uint MyFormat = 0;
private const string MyExtension = ".edmx";
public FileEventsListener()
{
//solution = Package.GetGlobalService(typeof(SPersistFileFormat)) as IVsSolution;
//if (solution != null)
//{
// solution.AdviseSolutionEvents(this, out solutionEventsCookie);
//}
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
public int GetClassID(out Guid pClassID)
{
ErrorHandler.ThrowOnFailure(((IPersist)this).GetClassID(out pClassID));
return VSConstants.S_OK;
}
public int GetCurFile(out string ppszFilename, out uint pnFormatIndex)
{
pnFormatIndex = MyFormat;
ppszFilename = fileName;
return VSConstants.S_OK;
}
public int GetFormatList(out string ppszFormatList)
{
char Endline = (char)'\n';
string FormatList = string.Format(CultureInfo.InvariantCulture, "My Editor (*{0}){1}*{0}{1}{1}", MyExtension, Endline);
ppszFormatList = FormatList;
return VSConstants.S_OK;
}
public int InitNew(uint nFormatIndex)
{
if (nFormatIndex != MyFormat)
{
return VSConstants.E_INVALIDARG;
}
// until someone change the file, we can consider it not dirty as
// the user would be annoyed if we prompt him to save an empty file
isDirty = false;
return VSConstants.S_OK;
}
public int IsDirty(out int pfIsDirty)
{
pfIsDirty = 0;
return VSConstants.S_OK;
}
public int Load(string pszFilename, uint grfMode, int fReadOnly)
{
fileName = pszFilename;
return VSConstants.S_OK;
}
public int Save(string pszFilename, int fRemember, uint nFormatIndex)
{
return VSConstants.S_OK;
}
public int SaveCompleted(string pszFilename)
{
return VSConstants.S_OK;
}
}
}
You can subscribe to the DTE.Events.DocumentEvents.DocumentSaved event.

How to specify individual List item's Font color in VC++ WTL

I have list box CListBox in which i need to specify the color of individual list item according to some condition. How can i achieve this.
Im running VS2005.
The application is a WTL Dialog based app.
You can create your own listbox(Ex:CColorListBox)
ColorListBox.h
class CColorListBox : public CListBox
{
// Construction
public:
CColorListBox();
// Attributes
public:
// Operations
public:
int AddString( LPCTSTR lpszItem, COLORREF rgb);
int InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CColorListBox)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CColorListBox();
// Generated message map functions
protected:
//{{AFX_MSG(CColorListBox)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
ColorListBox.cpp
Here is an idea not exact code............
int CColorListBox::AddString( LPCTSTR lpszItem,COLORREF rgb )
{
int item = AddString(lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;
}
int CColorListBox::InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb)
{
int item = ((CListBox*)this)->InsertString(nIndex,lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;
}
void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpdis)
{
}
This is what i did to implement the same functionality in ListViewCtrl.
I wrote a class to extend the CListViewCtrl.
class CListViewCtrlEx: public CWindowImpl<CListViewCtrlEx, CListViewCtrl>, public CCustomDraw<CListViewCtrlEx>
{
public:
BEGIN_MSG_MAP(CListViewCtrlEx)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
CHAIN_MSG_MAP_ALT(CCustomDraw<CListViewCtrlEx>, 1)
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL bHandled);
DWORD OnPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW lpNMCustomDraw);
DWORD OnItemPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW lpNMCustomDraw);
void ForceMeasureItemMessage();
void DeleteItem(LPDELETEITEMSTRUCT /*lpDeleteItemStruct*/);
BOOL DeleteItem(int nItem);
void GetCellRect(int header_column, const CRect& item_rect, CRect& cell_rect);
};
The complete code is HERE.

Switching to another application (previously active) on button click in .net

Hi I want to do something like on screen keyboard. I want user to click a button on inactive application and then key press will be sent to active application while keeping active application active. I wrote the code for hover event of the button in inactive application and it is working. But what I want is to do it in click event. It is not working because inactive application becomes active. The code is below for hover event. Thank you.
private void button1_MouseHover(object sender, EventArgs e)
{
SendKeys.Send("{TAB}");
}
Finally I could figure out a way to do it.
Refer the code below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Timers;
using tmr = System.Timers;
using System.Threading;
namespace KeyPressTest
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern int GetForegroundWindow();
[DllImport("user32")]
private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
tmr.Timer tm = new tmr.Timer();
Int32 hwnd = 0;
private Int32 GetWindowProcessID(Int32 hwnd)
{
Int32 pid = 1;
GetWindowThreadProcessId(hwnd, out pid);
return pid;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tm.Elapsed += Timer_Elapsed;
tm.Interval = 100;
tm.Start();
}
private void button1_Click(object sender, EventArgs e)
{
SetForegroundWindow((IntPtr)hwnd);
Thread.Sleep(40);
SendKeys.Send("{TAB}");
}
private void Timer_Elapsed(object sender, System.EventArgs args)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate
{
if (this.Handle.ToInt32() != GetForegroundWindow())
{
hwnd = GetForegroundWindow();
}
}));
}
else
{
if (this.Handle.ToInt32() != GetForegroundWindow())
{
hwnd = GetForegroundWindow();
}
}
if (hwnd == 0)
{
return;
}
string appProcessName = "";
string appExePath = "";
string appExeName = "";
try
{
appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
appExeName = appExePath.Substring(appExePath.LastIndexOf(#"\") + 1);
}
catch (Exception ex)
{
}
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate
{
textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
}));
}
else
{
textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
}
}
}
}
I have never done something similar, but here is what I found in this forum:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern int GetForegroundWindow();
[DllImport("user32")]
private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);
private int teller = 0;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (teller == 1)
{
setTextje();
}
teller++;
}
private Int32 GetWindowProcessID(Int32 hwnd)
{
Int32 pid = 1;
GetWindowThreadProcessId(hwnd, out pid);
return pid;
}
private void setTextje()
{
Int32 hwnd = 0;
hwnd = GetForegroundWindow();
string appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
string appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
string appExeName = appExePath.Substring(appExePath.LastIndexOf(#"\") + 1);
textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
}
}
}
It doesn't answer exactly to your question but it will give you a hint. You need to DllImport "User32.dll". After that you can get the id of the foreground window and play with that.
There is also a very interesting article about application switch tracking written in C#

Resources