Possible to control other Programs/Windows [duplicate] - c#

This question already has answers here:
How do you programmatically resize and move windows with the Windows API?
(5 answers)
Closed 9 years ago.
I was wondering whether it is possible to resize programs other than the actual application itself. IE, I want to resize and move Word and my application to fill 70% and 30% of the screen respectively.
Private Sub MinimiseButton_Copy_Click(sender As Object, e As RoutedEventArgs) Handles MinimiseButton_Copy.Click
Me.Left = SystemParameters.PrimaryScreenWidth - Me.Width + 14
Me.Top = -14
Me.Height = SystemParameters.PrimaryScreenHeight
Dim parry As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("winword")
Dim word As System.Diagnostics.Process = parry(0)
SetWindowPos(word.Handle, 0, 0, 0, SystemParameters.PrimaryScreenWidth - Me.Width, SystemParameters.PrimaryScreenHeight - 28, &H10)
End Sub
<DllImport("user32.dll", CharSet:=CharSet.Auto)> Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, W As Integer, H As Integer, uFlags As UInteger) As Boolean
End Function

That is indeed possible, try this function:
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref Rectangle rectangle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int WPARAM, int LPARAM);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[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);
public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_NEXTWINDOW = 0xF040;
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
public static readonly IntPtr HWND_TOP = new IntPtr(0);
public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
public const UInt32 TOPMOST_FLAGS = 0x0002 | 0x0001;
Public void resisezeWindow(String procesname, int Width, int Height, Boolean bringtofront)
{
foreach (Process proc in Process.GetProcesses())
{
IntPtr id = proc.MainWindowHandle;
Rectangle rect = new Rectangle();
GetWindowRect(id, ref rect);
if (proc.MainWindowTitle.Contains(procesname))
{
PostMessage(proc.Handle, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
MoveWindow(id, 0, 0, Width, Height, true);
if(bringtofront) SetWindowPos(id, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
proc.Refresh();
}
}
}
If there is a problem, please notify me!

Related

How to make a window always stay on top in .Net?(Task View)

I have a security program for my schools. What i need,
I keep my Windows form up until I'm allowed to.
I can partially do with the following codes..
<code>
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const 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);
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);</code>
But
the above code.
Does not work in task view(windows 10)
preview

What makes this click through?

I'm trying to figure out how this code works but I just can not figure out what makes it click through.
Yes, this code is not mine since i'm trying to learn/understand it.
Assume I want the tranparancy but not the click through what needs to be changed and why?
I have been over the Windows styles pages over and over and still can not get my head around the click through part.
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class TransparentWindow : MonoBehaviour
{
[SerializeField]
private Material m_Material;
private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
[DllImport("user32.dll")]
static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern int SetWindowPos(IntPtr hwnd, int hwndInsertAfter, int x, int y, int cx, int cy, int uFlags);
[DllImport("Dwmapi.dll")]
private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
const int GWL_STYLE = -16;
const uint WS_POPUP = 0x80000000;
const uint WS_VISIBLE = 0x10000000;
const int HWND_TOPMOST = -1;
void Start()
{
#if !UNITY_EDITOR // You really don't want to enable this in the editor..
int fWidth = Screen.width;
int fHeight = Screen.height;
var margins = new MARGINS() { cxLeftWidth = -1 };
var hwnd = GetActiveWindow();
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
// Transparent windows with click through
SetWindowLong(hwnd, -20, 524288 | 32);//GWL_EXSTYLE=-20; WS_EX_LAYERED=524288=&h80000, WS_EX_TRANSPARENT=32=0x00000020L
SetLayeredWindowAttributes(hwnd, 0, 255, 2);// Transparency=51=20%, LWA_ALPHA=2
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, 32 | 64); //SWP_FRAMECHANGED = 0x0020 (32); //SWP_SHOWWINDOW = 0x0040 (64)
DwmExtendFrameIntoClientArea(hwnd, ref margins);
#endif
}
void OnRenderImage(RenderTexture from, RenderTexture to)
{
Graphics.Blit(from, to, m_Material);
}
}
This function:
SetWindowLong(hwnd, -20, 524288 | 32);
does the trick. Windows implements the rule that Mircosoft made which is that a window that is transparent to the user must be transparent to the mouse.
With the transparency bit set WS_EX_TRANSPARENT the window becomes transparent to mouse too and click passes to the painted layer behind the transparent window.
You need not understand but make use of this 'OS feature' which was probably implemented to cover for something else.
Read this article about the subject and this answer that explains the parameters

Cefsharp change the parameter passed to the site the width and height of the screen c#

can you help, how you can change the screen size, which cefsharp transmits ? I go to ipleak.net and saw
Your screen: 1920 x 1080
Available screen: 1920 x 1040
Is there any way to change it ?
I tried that, but it didnt work
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
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
protected static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
protected static extern bool IsWindowVisible(IntPtr hWnd);
public static IntPtr m_hwndForm;
protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
{
int size = GetWindowTextLength(hWnd);
if (size++ > 0 && IsWindowVisible(hWnd))
{
StringBuilder sb = new StringBuilder(size);
GetWindowText(hWnd, sb, size);
string strName = sb.ToString();
if (strName.Contains("Form111111"))
m_hwndForm = hWnd;
}
return true;
}
...
EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero);
SetWindowPos(m_hwndForm.ToInt32(), 0, 10, 20, 600, 800, 0x0040);
InitializeChromium();
or
settings.CefCommandLineArgs.Add("Screen-width", "800");
settings.CefCommandLineArgs.Add("Screen-height", "600");
I've found, that's possible through chrome DevTools protocol using setDeviceMetrics method. Many thanks to amaitland!
var client = browser.GetDevToolsClient();
await client.Emulation.SetDeviceMetricsOverrideAsync(1000, 800, 1, true);
You also need to fully initialize CefSharp browser before use this method.

Tessnet 2 return wrong result

I trying to run tessnet on a bitmap returned from a screenshot created with getwindow function but the result is bad. I tried to run on a bmp file saved in paint. This image is same as image created with getwindow and for this the tessnet work. This is the image Any idea?
public const int SRCCOPY = 13369376;
public const int WM_CLICK = 0x00F5;
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "GetDC")]
internal extern static IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
internal extern static IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
internal extern static IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
internal extern static IntPtr DeleteDC(IntPtr hDc);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
internal extern static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
internal extern static bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, int RasterOp);
[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
internal extern static IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
internal extern static IntPtr DeleteObject(IntPtr hDc);
[DllImport("user32.dll")]
public static extern int SendMessage(
int hWnd, // handle to destination window
uint Msg, // message
long wParam, // first message parameter
long lParam // second message parameter
);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
public static Bitmap createBitmapFromWindow(string windowClass,string windowTitle,Point sarok1,Point sarok2)
{
IntPtr hWnd = FindWindow(windowClass, windowTitle);
Bitmap bmp = null;
IntPtr hdcFrom = GetDC(hWnd);
IntPtr hdcTo = CreateCompatibleDC(hdcFrom);
RECT windowSize;
GetWindowRect(hWnd, out windowSize);
int height = windowSize.Bottom;
int width = windowSize.Right;
IntPtr hBitmap = CreateCompatibleBitmap(hdcFrom, width, height);
if (hBitmap != IntPtr.Zero)
{
// adjust and copy
IntPtr hLocalBitmap = SelectObject(hdcTo, hBitmap);
int posx, posy;
if (sarok1.X > sarok2.X)
{
posx = sarok2.X;
}
else
{
posx = sarok1.X;
}
if (sarok1.Y > sarok2.Y)
{
posy = sarok2.Y;
}
else
{
posy = sarok1.Y;
}
BitBlt(hdcTo, 0, 0, Math.Abs(sarok1.X-sarok2.X), Math.Abs(sarok1.Y-sarok2.Y),
hdcFrom, posx, posy, SRCCOPY);
SelectObject(hdcTo, hLocalBitmap);
//We delete the memory device context.
DeleteDC(hdcTo);
//We release the screen device context.
ReleaseDC(hWnd, hdcFrom);
//Image is created by Image bitmap handle and assigned to Bitmap variable.
bmp = System.Drawing.Image.FromHbitmap(hBitmap);
DeleteObject(hBitmap);
}
return bmp;
}
public static void main()
{
Bitmap b1 = new Bitmap(createBitmapFromWindow(null, "window title", new Point(557, 460), new Point(670, 500)));
Bitmap b = b1.Clone(new Rectangle(new Point(0, 0), new Size(110, 29)),PixelFormat.Format24bppRgb);
var ocr = new Tesseract();
ocr.Init(#"path", "eng", false);
b.SetResolution(300, 300);
List<Word> l = ocr.DoOCR(b, Rectangle.Empty);
}
Tesseract expects black font on white background. An invert, a histogram equalization and a conversion to grayscale will help.
Looks like your images will consists of numbers only. You can hint tesseract to look for a numbers (and the point only). But I don't know how to do that with Tessnet.
Tesseract doesn't like different sized fonts. If the result is still not good it might be necessary to split the image in two different images and feed them separately.

Why Can This Window Still Be Resized, Minimized, and Maximized?

I have a window in an application, and the Resize mode is set to NoResize, but if you click the top-left of the window, the menu items of "Maximize", "Minimize", and "Size" are still enabled. This only happens to my windows that has no icon but still has the close button. Meaning this problem isn't happening to my windows that do have an icon.
This is the code that is removing the icon but still keeps the red close button:
protected override void OnSourceInitialized(EventArgs e) {
IconHelper.RemoveIcon(this);
}
public static class IconHelper {
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
const int GWL_EXSTYLE = -20;
const int WS_EX_DLGMODALFRAME = 0x0001;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOMOVE = 0x0002;
const int SWP_NOZORDER = 0x0004;
const int SWP_FRAMECHANGED = 0x0020;
const uint WM_SETICON = 0x0080;
public static void RemoveIcon(Window window) {
IntPtr hwnd = new WindowInteropHelper(window).Handle;
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
I would like to know why this is happening and would appreciate it if some one can fix this problem. I am using WPF and C#.

Categories

Resources