質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

2回答

7181閲覧

C#でwindowのiconサイズを変えたい

picko

総合スコア52

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2016/06/28 07:04

こんにちは。
Buttonを押したときに特定のExprolerのwindowのiconの表示サイズを変更したいです。

アイコンを小さくするコマンドメッセージは0x702aだということなので、エクスプローラのウィンドウに WM_COMMAND, 0x702A, 0をPostMessageしてみました。

前半でactivewindowにし、そのhWndはとれているようです。
ウィンドウがactiveになるので。

しかし、PostMessageがうまくいかないです。

併せて、アイコンを最大化するコマンドメッセージとか、メッセージ一覧もいただけたら嬉しいです。

Windows10, Visual Studio 2013 communityです。

private const int WM_COMMAND = 0x0111; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr FindWindow( string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] public static extern int GetWindowThreadProcessId( IntPtr hWnd, out int lpdwProcessId); private const Int32 WM_USER = 0x400; private const Int32 WM_IPC_MESSAGE = WM_USER + 1; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); private void Button_Click(object sender, RoutedEventArgs e){ IntPtr hWnd = activeWindow("titleOfWindow"); PostMessage(hWnd, WM_COMMAND, new IntPtr(0x702A), IntPtr.Zero); } //以下の参照を追加 //・com/Microsoft Shell Controls And Automation //・com/Microsoft Internet Control //web.LocationNameがエクスプローラが選択しているフォルダ //web.LocationURLがするフルパス //※コード中ではLocationNameのみ使用しています [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); public IntPtr activeWindow(string foldername) { IntPtr hWnd = (IntPtr)0; Shell shell = new Shell(); ShellWindows win = shell.Windows(); foreach (IWebBrowser2 web in win) { if (Path.GetFileName(web.FullName).ToUpper() == "EXPLORER.EXE") { // この部分をアクティブにしたいエクスプローラのタイトル(選択フォルダ)に変更してください if (web.LocationName == foldername) { // 指定のエクスプローラをアクティブにする hWnd = (IntPtr)web.HWND; SetForegroundWindow(hWnd); break; } } } return hWnd; } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

using System;
using System.Runtime.InteropServices;

public static class ConsoleHelper
{
private const int FixedWidthTrueType = 54;
private const int StandardOutputHandle = -11;

[DllImport("kernel32.dll", SetLastError = true)] internal static extern IntPtr GetStdHandle(int nStdHandle); [return: MarshalAs(UnmanagedType.Bool)] [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx); [return: MarshalAs(UnmanagedType.Bool)] [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx); private static readonly IntPtr ConsoleOutputHandle = GetStdHandle(StandardOutputHandle); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct FontInfo { internal int cbSize; internal int FontIndex; internal short FontWidth; public short FontSize; public int FontFamily; public int FontWeight; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] //[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.wc, SizeConst = 32)] public string FontName; } public static FontInfo[] SetCurrentFont(string font, short fontSize = 0) { Console.WriteLine("Set Current Font: " + font); FontInfo before = new FontInfo { cbSize = Marshal.SizeOf<FontInfo>() }; if (GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref before)) { FontInfo set = new FontInfo { cbSize = Marshal.SizeOf<FontInfo>(), FontIndex = 0, FontFamily = FixedWidthTrueType, FontName = font, FontWeight = 400, FontSize = fontSize > 0 ? fontSize : before.FontSize }; // Get some settings from current font. if (!SetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref set)) { var ex = Marshal.GetLastWin32Error(); Console.WriteLine("Set error " + ex); throw new System.ComponentModel.Win32Exception(ex); } FontInfo after = new FontInfo { cbSize = Marshal.SizeOf<FontInfo>() }; GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref after); return new[] { before, set, after }; } else { var er = Marshal.GetLastWin32Error(); Console.WriteLine("Get error " + er); throw new System.ComponentModel.Win32Exception(er); } }

}
Hope this will help you and you can check here(http://blogs.microsoft.co.il/blogs/pavely/archive/2009/07/23/changing-console-fonts.aspx)

投稿2020/10/13 15:03

gulahsnnegi

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

COMの操作はあまり詳しくないので別の方法で恐縮ですが。

取得したIWebBrowserDocumentに対して操作することで
今回やりたいことは実現可能です。

private enum FolderViewMode { ICON = 1, LIST = 3, DETAILS = 4, TILE = 6, CONTENTS = 8 } private void Button_Click(object sender, RoutedEventArgs e) { SetFolderViewMode("titleOfWindow", FolderViewMode.ICON); } private void SetFolderViewMode(string foldername, FolderViewMode mode) { Shell shell = new Shell(); ShellWindows win = shell.Windows(); foreach (IWebBrowser2 web in win) { if (Path.GetFileName(web.FullName).ToUpper() == "EXPLORER.EXE") { if (web.LocationName == foldername) { ShellFolderView view = web.Document; view.CurrentViewMode = (uint)mode; break; } } } }

その他、
アイコンサイズはShellFolderView.IconSize
表示オプションはShellFolderView.ViewOptions
等で指定可能です。


追記

ViewOptionプロパティは読み取り専用でした。すみません。

今回は、アイコンの大きさを任意のサイズにしたいとのことですので、
ShellFolderView.IconSizeに値を設定してあげればよいです。
なお、IconSizeプロパティには、16~256までの整数値が設定可能です。

エクスプローラの「表示方法の変更」で指定できるアイコンサイズのそれぞれの規定値は、
特大:256
大 :96
中 :48
小 :16
です。

投稿2016/07/06 06:49

編集2016/07/11 23:36
wakuwaku

総合スコア386

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

picko

2016/07/11 06:47

ありがとうございます。iconやlistにできることを確認しました。 任意のサイズにしたいので、ShellFolderView.IconSizeとViewOptionsの使い方をもう少し詳しく補足していただけたら嬉しいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問