I've been using a windows CE application that produces a text in its textbox when its button was clicked. I need this text for my C# app. How can I trigger its button and copy its text to my application.
private void button1_Click(object sender, EventArgs e)
{
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "\\Program Files\\kangkong\\Barcode2005.exe";
myProcess.Start();
// minimize Barcode2005.exe and the code goes here...
}
catch
{
throw;
}
}
Anyway, I'm using C# in Windows CE
Thanks :)
This is similar to your other question c# how to get handle over a specific mainmenu using coredll.dll
You need to define some p/invokes to be able to use the native FindWindow, GetWindowText functions.
First use FindWindow with the class name and/or title of the foreign window to get a handle to the main window. You may also use the processID (pID) to identify the foreign window.
Then enumerate all child windows of the main window until you find the window of the textbox (in native API an TextBox is using the EDIT class and is just a window like everything else you see). Then you can use GetWindowText using the found handle of the EDIT window.
If the foreign window is a dialog, you have to add GetDlgItem to your p/invoke list. Then you can use the EDIT dlg item's control ID to identify the input field and then use GetWindowText using this handle. AFAIR the control IDs are available in the resources of an windows exe file (use ResHacker to get the IDs).
BTW: where comes barcode2005.exe come from? Can you upload a copy for analysis? Is this a barcode graphic generator or decoder? If so, there are easier ways to get barcode graphics or decodes in C#.
Related
I want to use a winform inside MT4, which I know is possible. Metatrader4 has its own c++ flavored language, which can import .DLL-s, but we can only use stdcall CallingConvention.
I am using C# in Visual Studio 2015 to create the .DLL
My approach here is this:
From Metatrader 4 Terminal, I call a function in the .DLL to start the winform, I also pass the window handle as use it as an ID to track what chart just called the .DLL
I am storing the panel objects using a key, value dictionary.
Below is the process which loads the form.
// key,value dictionary holding forms
private static Dictionary<int, TradePanelForm> panels = new Dictionary<int, TradePanelForm>();
// start panel assosicated by window handle
[DllExport("OpenPanel", CallingConvention.StdCall)]
public static void OpenPanel(int inMT4hwnd)
{
if (!panels.ContainsKey(inMT4hwnd))
{
// add the panel object to the dictionary list
panels.Add(inMT4hwnd, new TradePanelForm(inMT4hwnd));
}
Application.Run(panels[inMT4hwnd]);
// panels[inMT4hwnd].Show();
}
If I use
Application.Run(panels[inMT4hwnd]);
the form will open, but it seems to be 'in focus' and Metatrader 4 Terminal seems to become unresponsive until I close the panel.
If I load the panel this way
panels[inMT4hwnd].Show();
the panel partially loads, with the buttons whited out, and the both the panel, and Metatrader 4 Terminal hangs.
Q1: What is the correct way to be doing this?
Q2: Is there some special winAPI stuff I need to dance around to make the form work properly within the metatrader process?
I've looked up threads, tasks, async stuff. Could somebody usher me towards the right direction here.
It would be nice if I could get the form to load 'inside' Metatrader's window as like a child window.
I have an ActiveX control written in C# which operates a scanner from the browser using WIA. Everything works fine except the WIA CommonDialog pops under the browser window. How can I get it to show up on top of the browser?
wiaDialog = new WIA.CommonDialog();
wiaImage = wiaDialog.ShowAcquireImage(WiaDeviceType.ScannerDeviceType, WiaImageIntent.UnspecifiedIntent, WiaImageBias.MaximizeQuality, wiaFormatJPEG, false, false, false);
[Edit]
Thanks very much to Noseratio for putting me onto the right track. The suggestion to use BringWindowToTop invoked via a timer before popping up the dialog does not quite work. Instead the function to use is SetForegroundWindow. The code is as follows (invoked from a System.Timer.Timer prior to opening the scan dialog):
public static void scanDialogToTop(Object caller, EventArgs theArgs) {
scanner.theTimer.Stop();
foreach (Process p in Process.GetProcesses()) {
if (p.MainWindowTitle.StartsWith("Scan using")) {
SetForegroundWindow(p.MainWindowHandle);
break;
}
}
}
See this article for a more complete discussion.
It does not look like you can specify a parent window for ShowAcquireImage. If the caption of the popup window is static, you could use FindWindow to find the popup's handle. If ShowAcquireImage is a blocking call (doesn't return until the popup window is closed), before calling it you'd need to setup a timer and call FindWindow upon a timer event. I also suspect the WIA popup is created on a different thread (you could check that with Spy++). If that's the case, you could use the following hack to give the WIA popup window focus. Otherwise you just do BringWindowToTop.
I'm working on a .Net (WPF/C#) application, the application displays notifications (similar to Growl notifications on OS X) at different times, I would like the notifications to display above all other windows, including when there is a full screened app (like a PowerPoint presentation).
Is there anyway to display a Window over a full screened app?
Did you try to set TopMost = true parameter of this window?
also check this thread:
Form top most?
or you can use Popup instead, it will on top of any window or control.
WPF.
private void Window_Deactivated(object sender, EventArgs e)
{
Thread.Sleep(2000);
this.Topmost = true;
}
But this is not the best option.
In full screen mode there is exclusive access to the display, and you can only display on top of this if you are allowed to implement it if it's a game in OpenGL, DirectX and BackBuffer to impose your data.
I am working on winforms application in C#. What I want to achieve is to get a file from user for which I am using the following code:
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string sFileName = dlg.FileName;
//my code goes here
}
Now, everything is working fine but I want to put 3 radio buttons in the same dialog box, meaning I would now get two things from this dialog box
string sFileName = dlg.FileName; //same as in case of traditional dialog box
//some thing like this which tells which radio button is selected:
dlg.rbTypes.Selected
How do I achieve this?
Yes, that's possible, I did the same kind of customization with SaveFileDialog successfully and it's pretty interesting.
Follow the following links:
http://www.codeproject.com/KB/dialog/OpenFileDialogEx.aspx
http://www.codeproject.com/KB/cs/getsavefilename.aspx
http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx
Also my own questions too will help you:
Change default arrangement of Save and Cancel buttons in SaveFileDialog
How to stop overwriteprompt when creating SaveFileDialog using GetSaveFileName
You have to use the WinAPI for this and you need to write the ShowDialog method in your own calling the GetOpenFileName windows function inside it, instead of calling .net's OpenFileDialog. The GetOpenFileName will create the windows OpenFileDialog. (Refer to http://msdn.microsoft.com/en-us/library/ms646927%28v=vs.85%29.aspx). This together with writing the HookProc procedure and catching events such as WM_INITDIALOG, CDN_INITDONE inside it will help you do what you want.
To add radio buttons etc., you have to call the windows functions such as CreateWindowEx and SendMessage....
The 2nd link has the exact direction to the customization...
Ask for any clarifications...
On XP you need to use the hook procedure method and the GetOpenFileName API. On Vista and later this will result in a horrid looking file dialog with limited utility, e.g. no search. On Vista you should use IFileDialog and to customise the dialog you need the IFileDialogCustomize interface. Because the new Vista dialogs are exposed as COM interfaces they are quite easy to consume in .net.
I have lots of old Windows Forms applications that will eventually be ported to WPF (it is a large application so it can't be done in one sprint), and I have started the process by creating a main menu in WPF. The Windows Forms applications are separate windows opened from this menu.
The Windows Forms applications are opening and working without any problems except the issues I am having with the shortcut and Tab keys. The tab key is not moving focus to the next control, and the Alt key to trigger the &Search button no longer works.
What am I doing wrong?
A partial solution I discovered is to call this from your WPF constructor:
System.Windows.Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop();
(You need to reference the dll WindowsFormsIntegration.dll)
I say partial because not all key strokes function as expected. Eg, seems to work okay for simple forms.
See this:
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.enablewindowsformsinterop(v=vs.100).aspx
I finally managed to fix the issue by hosting the winform inside a WindowsFormsHost control inside a WPF form.
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
Form winform = new Form();
// to embed a winform using windowsFormsHost, you need to explicitly
// tell the form it is not the top level control or you will get
// a runtime error.
winform.TopLevel = false;
// hide border because it will already have the WPF window border
winform.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.windowsFormsHost.Child = winform;
}
}
Please note that you may also need to hook up the winform close event if you have a button to close the form.
This is by design. Shortcut keys are handled at the message loop level, detected before the Windows message gets dispatched to the window with the focus. That's the reason those keys can work regardless of the focus.
Problem is, you don't have the Winforms message loop pumping the messages. Application.Run() is implemented by WPF in your program, not Winforms. So any of the code in Winforms that processes keyboard messages to implement shortcut keystrokes won't run.
There's no good solution for this, it is pretty fundamentally the "can't get a little pregnant" problem. This code in Winforms is locked up heavily since it would allow CAS bypass. The only workaround is to display a Form derived class that contain Winforms controls with its ShowDialog() method. That method pumps a modal message loop, the Winforms one, good enough to revive the shortcut keystroke handling code. Restructure your approach by converting the main windows first, dialogs last.
Another solution I found to handle focus on the Tab key is to override OnKeyDown like this:
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
HandleFocus(this, ActiveControl);
}
else
{
base.OnKeyDown(e);
}
}
internal static void HandleFocus(Control parent, Control current)
{
Keyboard keyboard = new Keyboard();
// Move to the first control that can receive focus, taking into account
// the possibility that the user pressed <Shift>+<Tab>, in which case we
// need to start at the end and work backwards.
System.Windows.Forms.Control ctl = parent.GetNextControl(current, !keyboard.ShiftKeyDown);
while (null != ctl)
{
if (ctl.Enabled && ctl.CanSelect)
{
ctl.Focus();
break;
}
else
{
ctl = parent.GetNextControl(ctl, !keyboard.ShiftKeyDown);
}
}
}
The advantage of this solution is that it doesn't require neither a WindowsFormsHost nor a message pump which can be a hassle to implement. But I don't know if it is possible to handle shortcuts keys like this because I didn't need it.
Check if IsTabStop="True" and TabIndex is assigned. For Alt + Key shortcut, try using the underscore (_) character instead of the ampersand (&).