c#: SetScrollPos (user32.dll) - c#

I want to have 2 (rich)texboxes (vc# 2k8) with same scrolling... so when I scroll tb1 the tb2 scrolls to the same position. I use this functions:
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
public static extern int GetScrollPos(IntPtr hwnd, int nBar);
That works fine but
int pos = GetScrollPos(tb1.Handle, 1);
SetScrollPos(tb2.Handle, 1, pos, true);
only sets the scrollbar to the same position but down update the text in there.
tb2.Update() oder Refresh won't work...
Please help. Ty

I found answer
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar,
int nPos, bool bRedraw);
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
public void ScrollTo(int Position) {
SetScrollPos((IntPtr)textBox1.Handle, 0x1, Position, true);
PostMessage((IntPtr)textBox1.Handle, 0x115, 4 + 0x10000 * Position, 0);
}

Related

Process frozen when I try to start it hooked to a panel

I need to launch an external .exe application within my windows forms app on a panel. If I start the process, without placing it on the panel, it's fine but if I place it on the panel the program freezes and does nothing.
I tried to google the problem but I did not find anything,can you help me?.
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr Handle, int x, int y, int w, int h, bool repaint);
[DllImport("USER32.DLL")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
private const int GWL_STYLE = (-16);
public static int WS_BORDER = 0x00800000;
public static int WS_CAPTION = WS_BORDER;
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
private const int SW_SHOWMAXIMIZED = 3;
string projectDirectory = "C:\\Users\\anten\\OneDrive\\Desktop\\together\\VisualSharp7\\bin\\Debug";
Visual_Sharp7.StartInfo.WorkingDirectory = projectDirectory;
Visual_Sharp7.StartInfo.FileName = "VisualSharp7.exe";
if (Visual_Sharp7.Start())
{
IntPtr ptr = IntPtr.Zero;
while ((ptr = Visual_Sharp7.MainWindowHandle) == IntPtr.Zero) ;
int WS_VISIBLE = GetWindowLong(Visual_Sharp7.MainWindowHandle, GWL_STYLE);
SetWindowLong(Visual_Sharp7.MainWindowHandle, GWL_STYLE, (WS_VISIBLE & ~WS_CAPTION));
SetParent(Visual_Sharp7.MainWindowHandle, panelMain.Handle);
ShowWindow(Visual_Sharp7, SW_SHOWMAXIMIZED);
}

C# Turn off selected monitor

I'm wondering if someone could help me I have been trying to find a way of turning off specific monitors in a basic application, there are a couple of posts on here about turning off all monitors and this works fine. The only topic I can see if this one here:
C# selectively turn off monitor
This does explain how to go about doing this but I'm still learning and not really sure how to expand the example into whats needed.
Turn off all monitors:
public void MonitorOff(IntPtr handle)
{
SendMessage(handle, WmSyscommand, (IntPtr)ScMonitorpower, (IntPtr)MonitorShutoff);
}
private void MonitorOn()
{
mouse_event(MouseeventfMove, 0, 1, 0, UIntPtr.Zero);
Thread.Sleep(40);
mouse_event(MouseeventfMove, 0, -1, 0, UIntPtr.Zero);
}
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);
private const int WmSyscommand = 0x0112;
private const int ScMonitorpower = 0xF170;
private const int MonitorShutoff = 2;
private const int MouseeventfMove = 0x0001;
[DllImport("user32")]
private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lpRect, MonitorEnumProc callback, int dwData);
private delegate bool MonitorEnumProc(IntPtr hDesktop, IntPtr hdc, ref Rect pRect, int dwData);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
Count Monitors:
public void GetMonitors()
{
int monCount = 0;
//Rect r = new Rect();
MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) => ++monCount > 0;
if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0))
MessageBox.Show("You have " + monCount.ToString() + " monitors", "Windows Hook");
else
MessageBox.Show("An error occured while enumerating monitors");
}
Is anyone able to enplane this or show me the right route to take?, Thank you.

restore window to specific locaiton on screen c# (console application)

I am wondering how can I restore application window to specific location on the screen.
I did managed to write some code, but somehow I do have a feeling that there must be and easier and more efficient way to do it programaticaly... as my current version is restoring window first and then moving it to required location. I would like to restore it without being forced to move it afterwards.
my code:
public void Send()
{
if (Process.GetProcessesByName("report").Any())
{
Process proc = Process.GetProcessesByName("report").First();
if (proc != null)
{
IntPtr pointer = proc.MainWindowHandle;
SetForegroundWindow(pointer);
SendMessage(pointer, WM_SYSCOMMAND, SC_RESTORE, 0);
Program.MoveWindow(pointer, 0, 0, 100, 100, true);
}
}
// do something
}
of course this is followed with :
[DllImport("user32.dll")]
static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
static Int32 WM_SYSCOMMAND = 0x0112;
static Int32 SC_RESTORE = 0xF120;
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
I would be very grateful for further solutions and explanation.

Why C# PostMessage does not send VK_Return iff window is focused?

I've been doing a program to send a text and enter key on a background running program , the sendmessage (string) works fine but after I call postmessage(VK_ENTER) it works only if the window is focused
here is code in C# (Visual Studio 2016)
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
private static string SetText(IntPtr handle)
{
const int VK_RETURN = 0x0D;
const int WM_KEYDOWN = 0x100;
SendMessage(handle, 0x000C, 0, textSend);
PostMessage(handle, WM_KEYDOWN, VK_RETURN, IntPtr.Zero);
}
Also I've tried with Sendmessage and it has the same result (the VK_RETURN isn't sent only if the window is not focused).

Set transparency to a control in windows form

I am new in programming. I can’t find a way to set transparency to a control. Please help. Something like, Form.transparency or some thing else.
The most simple way to make a form transparent is just to set the Form.Opacity property to a value.
Or, you can try, API calls for doing that,
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
public const int GWL_EXSTYLE = -20;
public const int WS_EX_LAYERED = 0x80000;
public const int LWA_ALPHA = 0x2;
public const int LWA_COLORKEY = 0x1;
For Calling,
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);

Categories