I am trying to open a compiled exe inside a panel of my code and that this program stays only inside this panel, in other words, if i move the window or even if I close the window, it would act as the same program.
It's working to some programs for example, "notepad.exe", but some others it doesn't (calc.exe for example it doesn't), they just insist to open outside the panel, so that's when I need help.
That's my code so far:
public partial class Form1 : Form
{
[DllImport("USER32.DLL")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("USER32.dll")]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process process = Process.Start("calc.exe");
process.WaitForInputIdle();
SetParent(process.MainWindowHandle, this.panel1.Handle);
MoveWindow(process.MainWindowHandle, 0, 0, this.Width - 90, this.Height, true);
}
}
I am a true newbie on C# and the progress I got so far was from some videos from youtube and even here, but it doesn't work exactly as I need, so I don't mind to reformulate the whole code, as long it works as only 1 program.
Related
I created a c# winform application. It opens a new window (without borders, like a popup if this matters) which should always stay on top of the main form. The window is opened by a thread:
Task.Run(() => ...
in the constructor of the new form I tried to add :
this.TopMost = true;
Sometimes the window goes in the background of the main form. And sometimes i can't even open it from the taskbar. I click on it and nothing happens.
There are no other applications with a window open
I tried it on Windows 7 and 10 (different pc's)
I tried to call following method from form.load (which to be honest I have no idea what it does exactly) :
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public static UInt32 SWP_NOSIZE = 0x0001;
public static UInt32 SWP_NOMOVE = 0x0002;
public static UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
Nothing helped. Does someone have an idea?
i am developing a wpf app, but i have a problem
when i activate my app from another application it shows totally black(demo on the bottom)
this code of activating my window work great but the window shows black
class WinApi2
{
public const int SW_SHOWNORMAL = 5;
[DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static void ShowToFront(string windowName)
{
IntPtr firstInstance = FindWindow(null, windowName);
ShowWindow(firstInstance, SW_SHOWNORMAL);
SetForegroundWindow(firstInstance);
}
}
// Activating my window by calling:
ShowToFront(".");
The result :
i found a solution by calling This.Show(); on the Activated Event of my wpf window
public void Window_Activated(object sender, EventArgs e)
{
this.Show();
}
but when doing that the black window still there it shows for a quick time and goes like flash that give a bad locking at the app showing.
i tried the same code to show another app(created with c plus plus) no problem, but the problem is just with wpf apps i think Winforms too ! who can i solver this problem ??
I was wondering if it is possible to remove the ability to close OTHER windows using C#?
I know that you can override your windows' close() method, but is that also possible for other processes? And what about changing the window style of another process to fixed___ so it cannot be resized?
So far I have gotten the main window handle of the application and I have removed all buttons and menus, but I still need to figure out how to make it uncloseable and unresizeable.
Here's what I've got:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ThirdTest
{
class Program
{
#region Constants
//Finds a window by class name
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
//Sets a window to be a child window of another window
[DllImport("USER32.DLL")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
//Sets window attributes
[DllImport("USER32.DLL")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
//Gets window attributes
[DllImport("USER32.DLL")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll")]
static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
static extern bool DrawMenuBar(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
//assorted constants needed
public static uint MF_BYPOSITION = 0x400;
public static uint MF_REMOVE = 0x1000;
public static int GWL_STYLE = -16;
public static int WS_CHILD = 0x40000000; //child window
public static int WS_BORDER = 0x00800000; //window with border
public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar
public static int WS_SYSMENU = 0x00080000; //window menu
#endregion
public static void WindowsReStyle()
{
Process[] Procs = Process.GetProcesses();
foreach (Process proc in Procs)
{
Console.WriteLine("Found process: " + proc.ProcessName.ToString());
if (proc.ProcessName.StartsWith("notepad"))
{
IntPtr pFoundWindow = proc.MainWindowHandle;
int style = GetWindowLong(pFoundWindow, GWL_STYLE);
//get menu
IntPtr HMENU = GetMenu(proc.MainWindowHandle);
//get item count
int count = GetMenuItemCount(HMENU);
//loop & remove
for (int i = 0; i < count; i++)
RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE));
//force a redraw
DrawMenuBar(proc.MainWindowHandle);
SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_SYSMENU));
SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_CAPTION));
}
}
}
static void Main(string[] args)
{
WindowsReStyle();
}
}
}
Any ideas? (:
As I've put in the comments, here are some more details on the issue:
I need two applications to be side-by-side on the monitor.
None of them can be closeable or resizeable. One is a browser, the other is an application called "Z-tree".
I have already fixed the issue with Z-tree as it, by default, runs with no closebutton and no resizing and you can specify the size and position of it in the command line.
Here's another idea, create a winforms project and set the window so it cannot be resized. Then embed a single WebBrowser control in the form and navigate to your page in the form load:
private void Form1_Load(object sender, EventArgs e)
{
//catch form closing event to prevent form being closed using alt-f4
FormClosing += Form1_FormClosing;
//remove close button from toolbar and remove window border to prevent
//moving and resizing
this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//set position to half of the screen
this.Left = Screen.PrimaryScreen.Bounds.Width / 2;
this.Top = 0;
this.Width = Screen.PrimaryScreen.Bounds.Width / 2;
this.Height = Screen.PrimaryScreen.Bounds.Height;
//mark the window as a top level window, reducing users ability to alt-tab away
TopMost = true;
webBrowser1.Navigate("www.google.com");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//prevent form being closed
e.Cancel = true;
}
//the only way to close the form
void DoExit()
{
//remove the closing handler first or it won't close
FormClosing -= Form1_FormClosing;
Close();
}
you can force internet explorer into an "unexitable" fullscreen mode if you start it with:
iexplore -k www.google.com
This is how shops and stuff get it to run so no-one can close it. Of course you can close it through task manager, but it just makes it difficult for most users to close it.
(CTRL-W will close it <--- secret key!)
http://support.microsoft.com/kb/154780
I'm working with a printer SDK for a label printer in C#, details of which can be found here:
https://stackoverflow.com/questions/18083309/getting-a-printer-api-to-work-with-c-sharp
Taking the advice given there, I used PInvoke to get the functions in the DLLs to work, and to my surprise, it all has began to come together...mostly.
A function SlpDrawTextXY() is supposed to be able to take an argument for a font of the type Hfont. This can be created by a function called SlpCreateFont(). (details of these methods on pages 21 and 19 respectively of the documentation).
Now, my quest to discover what Hfont actually is has went poorly. MSDN mentions it a bit, but doesn't really tell me what it is exactly. The articles provided aren't really useful if you are going in blind and are definitely more suited to someone who is already half way there. Other documentation about it is really slim and I'm left guessing at what the hell is supposed to be happening.
I have a block of code that looks like this:
public partial class Form1 : Form
{
[DllImport("SlpApi7x32.dll")]
static extern void SlpDebugMode(int nMode);
[DllImport("SlpApi7x32.dll")]
static extern int SlpOpenPrinter(String strPrinterName, int nID, bool fPortrait);
[DllImport("SlpApi7x32.dll")]
static extern void SlpClosePrinter();
[DllImport("SlpApi7x32.dll")]
static extern bool SlpStartLabel();
[DllImport("SlpApi7x32.dll")]
static extern void SlpDrawTextXY(int x, int y, Font iFont, String lpText);
[DllImport("SlpApi7x32.dll")]
static extern bool SlpEndLabel();
[DllImport("SlpApi7x32.dll")]
static extern Font SlpCreateFont(String lpName, int nPoints, int nAttributes);
[DllImport("GDI32.dll")]
public static extern bool DeleteObject(IntPtr objectHandle);
public Form1()
{
InitializeComponent();
}
private void print_Click(object sender, EventArgs e)
{
//Font myFont = new Font("Arial", 12);
//IntPtr hFont = myFont.ToHfont();
SlpDebugMode(2);
SlpOpenPrinter("Smart Label Printer 440", 1, false);
{
SlpStartLabel();
//Font font = SlpCreateFont("Courier", 12, 0);
SlpDrawTextXY(30, 30, null, "Hello World!");
SlpEndLabel();
}
SlpClosePrinter();
}
}
There are some remnants of my toying around that have been commented out. If it's commented out, it doesn't work.
This code will actually go to the printer and will 'print' a blank label, so it does seem like I'm really close. The third argument in SlpDrawTextXY is where the font is supposed to be though and I have it set as 'null' just to see if I can get past it successfully. This code is based on the sample C code in the documentation on page 12. I would like to be able to transform this code into something that actually prints text.
[DllImport("SlpApi7x32.dll")]
static extern Font SlpCreateFont(...)
Using Font is not correct here. SlpCreateFont() returns a HFONT, a "handle to font". It is the way you manipulate a font when you create one in unmanaged code. And is the exact same kind of animal you get back from the Font.ToHfont() method. So you must declare it the way ToHfont() returns it, it must be IntPtr in your declarations. Update the other declarations accordingly.
Do note that you'll have some decent odds that you can use Font.ToHfont() instead of SlpCreateFont(). The rules are the same however, you must be sure to call DeleteObject() when you are done using the font or you'll leak GDI objects that eventually will crash your code.
Hans was absolutely right. For future reference, below is working 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.Runtime.InteropServices;
namespace SSLP
{
public partial class Form1 : Form
{
[DllImport("SlpApi7x32.dll")]
static extern void SlpDebugMode(int nMode);
[DllImport("SlpApi7x32.dll")]
static extern int SlpOpenPrinter(String strPrinterName, int nID, bool fPortrait);
[DllImport("SlpApi7x32.dll")]
static extern void SlpClosePrinter();
[DllImport("SlpApi7x32.dll")]
static extern bool SlpStartLabel();
[DllImport("SlpApi7x32.dll")]
static extern void SlpDrawTextXY(int x, int y, IntPtr iFont, String lpText);
[DllImport("SlpApi7x32.dll")]
static extern bool SlpEndLabel();
[DllImport("SlpApi7x32.dll")]
static extern IntPtr SlpCreateFont(String lpName, int nPoints, int nAttributes);
[DllImport("GDI32.dll")]
public static extern bool DeleteObject(IntPtr objectHandle);
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
IntPtr font = SlpCreateFont("Arial", 10, 0);
SlpDebugMode(2);
//The second parameter defines the type of label per the documentation.
SlpOpenPrinter("Smart Label Printer 440", 3, false);
{
SlpStartLabel();
//Draw as much as you want with these!
SlpDrawTextXY(0, 0, font, "Hello World");
SlpEndLabel();
}
SlpClosePrinter();
DeleteObject(font);
}
}
}
I have a windows application develop using C# .net 4.0 version.
I need to remove close button from one of my popup windows. I can do it by setting Control Box property as false. But in that case it will remove my icon as well. Else I can disable the close button. But is there any way to remove close button only (leaving the icon in place)?
This is a bit of a cleaner solution :-)
Original post
winuser.h
public partial class Form1 : Form
{
private const int CS_NOCLOSE = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams mdiCp = base.CreateParams;
mdiCp.ClassStyle = mdiCp.ClassStyle | CS_NOCLOSE;
return mdiCp;
}
}
public Form1()
{
InitializeComponent();
}
}
According to constants in the winuser.h there are no flags to get rid of the closed button completely. (Unless you want to find a way to draw over the top of the button by copying a section to the left of it - yuk.)
This code will disable your close button but will show the icon.
In your form class:
Import:
using System.Runtime.InteropServices;
In the main class:
const int MF_BYPOSITION = 0x400;
[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);
Event:
private void Form1_Load(object sender, EventArgs e)
{
IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
}