Where are the Windows system menu icons stored? - c#

Where are the Windows system menu icons stored?
I'm talking about the icons on the system context menu.
I want to put them in my application.

You can get images using the LoadBitmap function.
Example (c#):
namespace GetBitmap
{
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
internal static class Program
{
private const long OBM_RESTORED = 32744;
private static void Main()
{
var hBitmap = LoadBitmap(IntPtr.Zero, OBM_RESTORED);
var image = Image.FromHbitmap(hBitmap);
image.Save(#"d:\restored.png", ImageFormat.Png);
DeleteObject(hBitmap);
}
[DllImport("user32.dll")]
private static extern IntPtr LoadBitmap(IntPtr hInstance, long resourceId);
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteObject([In] IntPtr hObject);
}
}

Related

How to get the user's wallpaper

As the title
I would like the effect like UAC's background
Here is a code I found from web.
using System;
using System.Runtime.InteropServices;
namespace cleandesktop
{
internal static class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SystemParametersInfo(uint uAction, uint uParam, StringBuilder lpvParam, uint init);
const uint SPI_GETDESKWALLPAPER = 0x0073;
static void Main(string[]) args
{
StringBuilder wallPaperPath = new StringBuilder(200);
if (SystemParametersInfo(SPI_GETDESKWALLPAPER, 200, wallPaperPath, 0))
{
MessageBox.Show(wallPaperPath.ToString());
}
}
}
}
This code get the path of the wallpaper picture, but this code only works when users hadn't delete their wallpaper picture.
PaintDesktop
The PaintDesktop function fills the clipping region in the specified device context with the desktop pattern or wallpaper.

C# unmanaged code call not working: AddConsoleAlias

I have the following code which keeps returning FALSE with a value of 8 from the GetLastError() call.
8 apparently is ERROR_NOT_ENOUGH_MEMORY.
I of course have enough memory, but the process doesn't think so, can anyone enlighten me as to what could be going wrong?
The code below is all I have except for the Forms objects declarations of course, but I guess there is no need to see this as I have 2 text boxes and 1 button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AddConsoleAlias
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("kernel32", SetLastError = true)]
static extern bool AddConsoleAlias(string Source, string Target, string ExeName);
[DllImport("kernel32.dll")]
static extern uint GetLastError();
private void btnAddAlias_Click(object sender, EventArgs e)
{
if (AddConsoleAlias(txbSource.Text, txbTarget.Text, "cmd.exe"))
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show(String.Format("Problem occured - {0}", GetLastError()));
}
}
}
}
AddConsoleAlias defines console alias. You have Windows Forms application without opened console. Console should be allocated before AddConsoleAlias invoke. To do that you can use AllocConsole function.
C# binding for this function is:
[DllImport("kernel32.dll",
EntryPoint = "AllocConsole",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
Your modified code will look like:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("kernel32.dll",
EntryPoint = "AllocConsole",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
[DllImport("kernel32", SetLastError = true)]
static extern bool AddConsoleAlias(string Source, string Target, string ExeName);
[DllImport("kernel32.dll")]
static extern uint GetLastError();
private void btnAddAlias_Click(object sender, EventArgs e)
{
AllocConsole();
if (AddConsoleAlias(txbSource.Text, txbTarget.Text, "cmd.exe"))
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show(String.Format("Problem occured - {0}", GetLastError()));
}
}
}

Is it possible to open an application within a panel of a form , like Google chrome

I don't know its possible or not
I can open it outside individually by
System.Diagnostics.Process.Start("FileName");
I tried this too
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace wmpkhela
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process p = Process.Start(#"notepad.exe");
p.WaitForInputIdle();
SetParent(p.MainWindowHandle, panel1..Handle);
}![enter image description here][1]
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
but this doesn't work as I expected
I guess you are looking for this and probably it is a duplicate see this question
Use these two functions for placing any other process inside panels.
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

Getting infos out of an activex plugin in C#

I'm using the Sopcast activex plugin (sopocx.ocx) in one of my C# applications.
I would like to retrieve the player status ("Buffering the channel", "Playing the channel", "Channel Offline...") and the buffering percentage. Both of these informationss are displayed on the player (I tried to post a picture but I don't have enough reputation yet).
The problem is the Sopcast activex plugin doesn't provide any methods in order to retrieve these informations.
Does someone has any idea on how this could be done??
GetWindowText results in an empty string...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
IntPtr hwnd = sopcast.Handle;
StringBuilder lpString = new StringBuilder(256);
GetWindowText(hwnd, lpString, 256);
MessageBox.Show(lpString.ToString());
}
private void playToolStripMenuItem_Click(object sender, EventArgs e)
{
sopcast.SetSopAddress("sop://broker.sopcast.com:3912/123456789");
sopcast.SetChannelName("Channel");
sopcast.Play();
}
}
}
You can identify Id control and get text with api windows
here a code sample (replace notepad by your application name) the most important for you is to get from your application a way to get ID control of your ocx window
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Security;
namespace Application
{
public class Program
{
public static void Main ( )
{
IntPtr hwnd = UnsafeNativeMethods.FindWindow("Notepad", null);
StringBuilder stringBuilder = new StringBuilder(256);
UnsafeNativeMethods.GetWindowText(hwnd, stringBuilder, stringBuilder.Capacity);
Console.WriteLine(stringBuilder.ToString());
}
}
[SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern int GetWindowText ( IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount );
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow ( string lpClassName, string lpWindowName );
}
}
I download sopcast and try to get status using spy++:
As you see, caption is status not the channel...
so you can not get it easier
the handle you have catched is for the whole control
Sopcast draws the status text using DrawText (i found out using API Monitor http://www.rohitab.com/apimonitor). So there is no way of getting the text using conventional GetWindowText function or similar. I was able to obtain the text by hooking DrawText function. For .NET EasyHook will enable you to do this.
My scenario:
I have a winforms app that hosts the activex control and i want to obtain the status text.
public class hooklocal : EasyHook.IEntryPoint
{
[DllImport("user32.dll")]
static extern int DrawText(IntPtr hDC, string lpString, int nCount, IntPtr lpRect, uint uFormat);
[UnmanagedFunctionPointer(CallingConvention.StdCall,CharSet = CharSet.Auto,SetLastError = true)]
delegate int DDrawText(IntPtr hDC, string lpString, int nCount, IntPtr lpRect, uint uFormat);
int DrawTextH(IntPtr hDC, string lpString, int nCount, IntPtr lpRect, uint uFormat)
{
//lpString contains the status text
return DrawText(hDC, lpString, nCount, lpRect, uFormat);
}
public hooklocal()
{
try
{
var CreateHook = LocalHook.Create(
LocalHook.GetProcAddress("user32.dll", "DrawTextW"),
new DDrawText(DrawTextH),
this);
CreateHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
}
catch (Exception ExtInfo)
{
Debugger.Break();
}
}
}
To use, instantiate hooklocal class in a new thread at program startup.
EasyHook download
https://easyhook.github.io/downloads.html

Forms and Console

I'm Running a forms application and a console application in one application.
How can i run the forms application and keep the console closed until i click a button on the form?
You need to call a couple of win32app calls most specifically allocconsole. here is an msdn post with some sample code.
You'll need to do a little P/Invoke:
Add the appropriate methods:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StackOverflow.Extensions;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32.SafeHandles;
namespace StackOverflow
{
public partial class FormMain : Form
{
[DllImport("kernel32.dll",
EntryPoint = "GetStdHandle",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll",
EntryPoint = "AllocConsole",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
// Some constants
private const int STD_OUTPUT_HANDLE = -11;
private const int MY_CODE_PAGE = 437;
public FormMain()
{
InitializeComponent();
}
public void PrepareConsole()
{
AllocConsole();
IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
}
private void button1_Click(object sender, EventArgs e)
{
// Console was not visible before this button click
Console.WriteLine("This text is written to the console that just popped up.");
MessageBox.Show("But we're still in a Windows Form application.");
}
}
}

Categories