• 締切済み

C# タスクバーを隠したフォーム固定の方法

前略 ・C#の初心者です。 ・先日このMSN相談箱でtsukasa-12rさんからタスクバーを非表示する下記のプログラムをおしえていだきました。 このプログラムでウィンドウフォームを固定しようとして this.FormBorderStyle = FormBorderStyle.FixedSingle; のコードを1行だけ追加しましたがウィンドウフォームを固定できませんでした。  タスクバーを隠して かつウィンドウフォームを固定したいのですが tsukasa-12rさん以外の方でもかまいませんのでどなたか教えてください。  以上、よろしくお願いします //----------------------------------- using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, IntPtr lpWindowName); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); private const int SW_HIDE = 0; private const int SW_NORMAL = 1; [StructLayout(LayoutKind.Sequential)] struct APPBARDATA { public int cbSize; public IntPtr hwnd; public uint uCallbackMessage; public uint uEdge; public Rectangle rc; public int lParam; }; private const int ABM_SETSTATE = 10; private const int ABS_AUTOHIDE = 1; private const int ABS_ALWAYSONTOP = 2; [DllImport("shell32.dll")] static extern int SHAppBarMessage(int msg, ref APPBARDATA pbd ); public Form1() { InitializeComponent(); // 「タスクバーを自動的に隠す」 APPBARDATA abd = new APPBARDATA(); abd.cbSize = Marshal.SizeOf(abd); abd.lParam = ABS_AUTOHIDE; SHAppBarMessage(ABM_SETSTATE, ref abd); // タスクバーを非表示 ShowWindow(FindWindow("Shell_TrayWnd", IntPtr.Zero), SW_HIDE); // コントロールボックスを非表示 this.ControlBox = false; // 最大表示 this.WindowState = FormWindowState.Maximized; this.FormBorderStyle = FormBorderStyle.FixedSingle;// ← ウィンドウサイズを固定しようとして追加 → 固定できず } private void button1_Click(object sender, EventArgs e) { // タスクバーを常に表示 APPBARDATA abd = new APPBARDATA(); abd.cbSize = Marshal.SizeOf(abd); abd.lParam = ABS_ALWAYSONTOP; SHAppBarMessage(ABM_SETSTATE, ref abd); // タスクバーを表示 ShowWindow(FindWindow("Shell_TrayWnd", IntPtr.Zero), SW_NORMAL); // フォームを閉じる this.Close(); } } }

noname#154688
noname#154688

みんなの回答

回答No.2

位置とサイズを固定できたとして、Alt + Tab でアプリケーションを切り替えたり、デスクトップに切り替えてそこからアプリケーションを起動できたりするのは構わないのですか? 「要求仕様」という言葉は仕事で使うのは別に問題ないと思いますが、こういった場では少し感じが悪いように思います。(受け取り方によっては、回答してくれる人に対して「こういう仕様で作りなさい」という要求を突きつけているように取れてしまう。)

回答No.1

とりあえず、 private const int WM_NCLBUTTONDBLCLK = 0x00A3; protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_NCLBUTTONDBLCLK: break; default: base.WndProc(ref m); break; } } を追加すると、ウィンドウタイトルをダブルクリックしても元のサイズに戻らなくなります。 しかし、Windows7 のエアロスナップが有効の場合、ウィンドウタイトルをドラッグすると元のサイズに戻ってしまいます。 レジストリの HKEY_CURRENT_USER\Control Panel\Desktop\DockMoving では再起動、もしくはログオフしなければ設定の変更がシステムに反映されませんが、コントロールパネルでは再起動、ログオフせずに反映できているので、何か方法があるのかもしれません。

参考URL:
http://weekly.ascii.jp/elem/000/000/022/22691/
noname#154688
質問者

お礼

前略 ・返答ありがとうございます。返答大変うれしく思っております。 ・コード追加して、ウィンドウタイトルをダブルクリックしても元のサイズに戻らなくなることを確認しました。ありがとうございます。でもウィンドウタイトルをドラッグして移動できてしまいます。移動できないようにしたいのです。  私の要求仕様が明確でなかったようです。もう一度整理して箇条書きにしたいとおもいます。 <要求仕様> 下記のウィンドウフォームをC#でつくりたい。 (1) タスクバーを表示しない。 (2) タイトルバーは表示する。 (3) ウィンドウフォームは最大化され、モニター画面内を移動できないこと。 <目的>  パソコンの拡張スロットの拡張ボード( A/D、D/Aボード)をつかったソフトを作成中です。  作成中のソフトが動作中(アクチュエータ動作中)は他のアプリケーションソフトを起動できないようにしたいので上記のウィンドウフォームつくろうとしています。  尚、this.FormBorderStyle = FormBorderStyle.None;を追加するとタイトルバーが消えて<目的>は達成できるのですが メニューバー等を追加した時見栄えがよくないのでタイトルバーを追加したいと思っている次第です。 <質問> メニューバー付ウィンドウフォームが最大化され、モニター画面を移動できないようにする方法を教えてください。 以上、よろしくお願いします  

関連するQ&A

  • C#でウィンドウのアイコン取得する方法

    タスクマネージャのアプリケーションタブに 起動しているアプリケーション名とアイコンが一覧で表示されている部分のように 小さいアイコンを取得したいんですがなかなかうまくいきません。 ファイルに紐付くアイコンと解釈していただいても構いません。 とりあえず、下記のWin32 APIの処理を利用してみましたが SHGetFileInfoだけしかうまくいきませんでした。 しかし、SHGetFileInfoも、アイコンは取得できますがこれはファイルに紐付くアイコンではなく 実行体(EXE)に紐付くアイコンで表示されてしまいました。 開いているウィンドウ情報からだと実行プロセスのEXEのパスしか分からないからです。 ★質問★: ファイルに紐付く小さいアイコンの取得方法を教えてください!できればサンプルソース込みだと助かります。 #region 小さいアイコンを取得するためのWin32 API(ファイルパスから) // SHGetFileInfo関数 [DllImport("shell32.dll")] private static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); // SHGetFileInfo関数で使用するフラグ private const uint SHGFI_ICON = 0x100; // アイコン・リソースの取得 private const uint SHGFI_LARGEICON = 0x0; // 大きいアイコン private const uint SHGFI_SMALLICON = 0x1; // 小さいアイコン private const uint SHGFI_TYPENAME = 0x400;//ファイルの種類 // SHGetFileInfo関数で使用する構造体 private struct SHFILEINFO { public IntPtr hIcon; public IntPtr iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; }; #endregion #region 小さいアイコンを取得するためのWin32 API(ウィンドウハンドルからその1) [DllImport("user32.dll", EntryPoint = "GetClassLong")] public static extern IntPtr GetClassLongPtr32(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "GetClassLongPtr")] public static extern IntPtr GetClassLongPtr64(IntPtr hWnd, int nIndex); const int GCL_HICON = (-14); const int GCL_HICONSM = (-34); #endregion #region 小さいアイコンを取得するためのWin32 API(ウィンドウハンドルからその2) [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); public const int WM_GETICON = 0x7F; public const int WM_SETICON = 0x80; public const int ICON_SMALL = 0; public const int ICON_BIG = 1; #endregion

  • C# 他のアプリケーションから複数行取得

    C#で外部のアプリケーションのリッチテキストコントロールの複数行の文字列を取得したいです。 自分は下のコードを書いたのですが、これだと1行しか取得できませんでした。 どこが悪いでしょうか。 ここでは対象のアプリケーションをA、リッチテキストのコントロールをRichEditとします。 button1をクリックしたら、textbox1にRichEditの内容が表示されるようなものを作ろうとしています。 //ここから [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, StringBuilder lParam); private const int WM_GETTEXT = 0x000D; private void button1_Click(object sender, EventArgs e) { IntPtr hWnd = FindWindow("A", ""); if (hWnd != null) { IntPtr hWndChild = FindWindowEx(hWnd, IntPtr.Zero, "RichEdit", ""); StringBuilder strBuilder = new StringBuilder(); SendMessage(hWndChild, WM_GETTEXT, strBuilder.Capacity, strBuilder); StringReader strReader = new StringReader(strBuilder.ToString()); string stResult = string.Empty; while (strReader.Peek() >= 0) { string stBuffer = strReader.ReadLine(); stResult += stBuffer + System.Environment.NewLine; } strReader.Close(); textbox1.Text = stResult; } }

  • 【C#】FindWindowExの使い方を教えてください

    はじめまして Visual Studio 2005を使用しています。 C#.NETは、いじり初めて1週間の超初心者です。 C#.NETでのFindWindowExの使い方を教えてください。 まずはじめに、vb.netで作ったアプリAの"Form1"があり、その中にテキストボックス"TextBox1"があります。 "TextBox1"のテキスト(キャプション?)には同じく"TextBox1"と入力されています。 そこで、C#側のアプリBでVBのアプリAの"Form1"のハンドルをFindWindowで取得します。 ここまでは出来ました。 次に、FindWindowExを使って"TextBox1"のハンドルを取得したいのですが、どうしてもうまく取得できません(0が返ってきます) 以下、C#のソースです。 (textBox1のMultilineはTrueです) ================================================================== 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 WindowsApplication1 { public partial class Form1 : Form { [DllImport("user32.dll")] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] static extern IntPtr FindWindowEx(IntPtr hWnd, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); public IntPtr hWnd = (IntPtr)0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { IntPtr hWnd; IntPtr hWndTest; string sClassName = null; string sWindowText = "AppA"; // アプリAのウインドウハンドルを取得 hWnd = FindWindow(sClassName, sWindowText); textBox1.Text = "ウインドウのハンドル " + hWnd + "\r\n"; // アプリAのウインドウ内のTextBox1のハンドルを取得 hWndTest = hWnd; sClassName = null; sWindowText = "TextBox1"; hWnd = FindWindowEx(hWndTest, IntPtr.Zero, sClassName, sWindowText); textBox1.Text += "テキストボックスのハンドル " + hWnd; } } } ================================================================== Spy++で覗くとテキストボックスにもハンドルが割り与えられているので取得できるはずだと思っているのですが、どうしてもいまくいきません。 どうか、よろしくご享受願います。 ちなみに、アプリAのテキストボックスのクラス名が”WindowsForms10.EDIT.app.0.378734a”となっているのですが、これはどの環境でビルド(コンパイル)しても不変なのでしょうか? 不変だとしたら、クラス名を使えば悩まずに取得できると思うのですが・・・(実験済み)

  • C# タスク非表示でタイトルバーは表示する

    前略 ・C#の初心者です。 ・タスクバーは下記のプログラムで非表示とすることができますが、同時にタイトルバーも消えてしまいます。タスクバーは非表示で、右端に閉じる・最大化・最小化アイコン("X"、 "□"、"_")のないタイトルバーを表示したいのですが、その方法をおしえてください。 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; namespace noTaskbar { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.FormBorderStyle = FormBorderStyle.None;//タイトルバー&タスクバー非表示 this.WindowState = FormWindowState.Maximized;// } } } 以上、よろしくお願いします

  • C++でタスクマネージャーを最小化

    C++Win32コンソールアプリケーションを作成しています。 タスクマネージャーを最小化させるプログラムを考えているのですが、上手くできません。 #include<iostream> #include<windows.h> using namespace std; int main() { for (;;) { HWND hwnd = FindWindow("TaskManagerWindow", NULL); if (hwnd != NULL) { cout << "成功" << endl; ShowWindow(hwnd,SW_MINIMIZE); } else cout << "失敗" << endl; Sleep(300); } return 0; } のようにしているのですが上手くできません。 Notepadのようなファイルは最小化できるのですが、タスクマネージャーに限ってできないのです。 どのようにすればよいでしょうか。 又、一時的に見えなくすればいいので終了や透明化等の他の方法でも構いません。 ご教授お願いします。

  • C#の配列の定数

    BCCを使用していたのですが、最近VS2013でC#を勉強し始めました フォームの指定した位置にいくつかのボタンを設置し、あらかじめ定義しておいた テーブルからボタンのtextを表示したいと思い、以下のようにコーディングしたのですが エラーが出てしまい思ったような結果に持って行くことができません C#ならこうした方がいいという指摘も含めて どうやるのがいいのか ご教授ください 足りない情報があれば追加します ちなみにまだテスト段階だから2列定義して1列しか表示するようにしかなってません ----------------------------------------------------------------------- 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; namespace haneda { public partial class Form1 : Form { private Button[] buttons; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { int i; //ボタンコントロール配列の作成 this.buttons = new Button[table.MAX_R]; for (i = 0; i < buttons.Length; i++) { //ボタンコントロールのインスタンス作成 this.buttons[i] = new Button(); //プロパティ設定 this.buttons[i].Name = "btn" + i.ToString(); this.buttons[i].Text = table.roomNo[1, i]; <=ここがエラー this.buttons[i].Top = table.roomX01[i]; <=ここがエラー this.buttons[i].Left = table.roomY01[i]; <=ここがエラー this.buttons[i].Click += new System.EventHandler(button_Click); //コントロールをフォームに追加 this.Controls.Add(this.buttons[i]); } } private void button_Click(object sender, System.EventArgs e) { Button btn = (Button)sender; MessageBox.Show(btn.Name); } } public class define { public const bool IGNORE = false; //非表示 public const bool EFFECT = true; //表示 /* 最大値 */ public const int MAX_F = 2; //フロア数 public const int MAX_R = 3; //ルーム数 } public class table { /* ナンバー */ public readonly string[,] roomNo = new string[define.MAX_F, define.MAX_R] { { "101", "102", "103"}, { "201", "202", "203"} } /* 表示・非表示 */ public readonly bool[,] effectRoom = new bool[define.MAX_F, define.MAX_R] { {EFFECT, IGNORE, EFFECT}, {EFFECT, EFFECT, EFFECT} } public readonly int[] roomX01 = new int[define.MAX_R] { 5, 70, 135 } public readonly int[] roomY01 = new int[define.MAX_R] { 100, 100, 100 } } }

  • C#でのWNetAddConnection3の使い方を教えてください

    現在、C#(VisualStudio2005)で共有フォルダの認証を行い、 そのフォルダ内のファイルを読み込む処理を行ないたいと考えています。 ■動作------------------------------------------------------ 1.ボタン押下によってフォルダ(\\aaa)をユーザ(user)、パスワード(pass)で認証を行なう 2.ファイル(bbb.txt)の内容をメッセージボックスに出力する 3.認証を切る ■現在の実装状況-------------------------------------------- private void btnGetShareFileData_Click(object sender, EventArgs e) { NETRESOURCE netresource = new NETRESOURCE(); netresource.dwType = 1; netresource.lpLocalName = null; netresource.lpRemoteName = "\\\\aaa"; netresource.lpProvider = null; try { int returnNo1 = WNetAddConnection3(IntPtr.Zero, ref netresource, "user", "pass", 0); System.IO.StreamReader streamReader = new System.IO.StreamReader("\\\\aaa\\bbb.txt", System.Text.Encoding.GetEncoding(932)); MessageBox.Show(streamReader.ReadToEnd()); int returnNo2 = WNetCancelConnection2("\\\\aaa", "0", true); } catch (Exception ex) { MessageBox.Show(ex.Message); } } [StructLayout(LayoutKind.Sequential)] public struct NETRESOURCE { public int dwType; public string lpLocalName; public string lpRemoteName; public string lpProvider; } [DllImport("mpr.dll")] private static extern int WNetAddConnection3( System.IntPtr hWndOwner, ref NETRESOURCE lpNetResource, string lpPassword, string lpUserName, int dwFlags ); [DllImport("mpr.dll")] private static extern int WNetCancelConnection2( string lpName, string dwFlags, bool fForce ); [DllImport("mpr.dll")] private static extern int WNetAddConnection2( ref NETRESOURCE lpNetResource, string lpPassword, string lpUsername, int dwFlags ); ■結果------------------------------------------------------ WNetAddConnection3の戻り値が「487」で System.IO.StreamReader streamReaderを実行されると 「ログオン失敗:ユーザ名を認識できないかまたはパスワードが間違っています。」 とエラーになってします。 WNetAddConnection3でのログオン自体がうまくできていないのでしょうですが、 どうすればよいのかがわかりません。 (ちなみにWNetAddConnection2でやってみても同様です) どなたか教えていただけないでしょうか? いろいろなページは参考にさせていただいているのですが、 知識が足らず読み解くことができないので、 できればサンプルコード等を教えていただけると助かります。 よろしくお願いいたします。

  • 【.NET】 VScrollBarクラスを追加して,VScrollbarの色を変更したいのですが...

     こんばんは,いつも勉強させていただいております. VB.NET初心者に少し毛が生えた程度のレベルでございます. 質問させていただきます.どうぞよろしくお願いいたします.  VScrollBarの色を変えたく,下のようなコードを追記いたしました. ビルド後,ツールボックスの一番上に現れた「VScrollBar」(歯車のアイコン) をForm上に置いてみましたところ,何故か横向きのHSCrollBarが現れます... Public Class VScrollBar   Inherits ScrollBar   Dim VScrollBar1 As VScrollBar   Private Const WM_CREATE As Int32 = &H1   Private Const WM_DESTROY As Int32 = &H2   Private Const WM_CTLCOLORSCROLLBAR As Int32 = &H137   Private Const WM_PAINT As Int32 = &HF   <System.Runtime.InteropServices.DllImport("Gdi32.dll")> _   Private Shared Function CreateSolidBrush(ByVal crColor As Int32) As IntPtr   End Function   <System.Runtime.InteropServices.DllImport("Gdi32.dll")> _   Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean   End Function   Private hBrush As IntPtr   Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)     Select Case m.Msg       Case WM_CREATE         hBrush = CreateSolidBrush(&HFF0000)       Case WM_DESTROY         DeleteObject(hBrush)       Case WM_CTLCOLORSCROLLBAR         If m.LParam.Equals(VScrollBar1.Handle) Then           m.Result = hBrush           Return         End If     End Select     MyBase.WndProc(m)   End Sub End Class  Inheritsの後が「VScrollBar」になるのかな?などと考えたりしましたが, http://msdn.microsoft.com/ja-jp/library/system.windows.forms.vscrollbar.aspx を見る限り,「Inherits ScrollBar」となっております..  目的の「Case WM_CTLCOLORSCROLLBAR」の中にも入ってきておらず, 悪戦苦闘し続けております.お詳しい方がいらっしゃいましたら, 是非ともアドバイスいただきたくお願い申し上げます.  何卒よろしくお願いいたします.

  • 特定座標のRGB値取得について

    kimarioと申します。 実行中の、あるアプリケーションの任意座標位置のRGB値を取得し、その結果を メッセージボックスにて表示させたいと考えております。 しかし下記のプログラムを実行すると、どの座標位置でもすべて(-1)で返ってき てしまいます。 getDCの戻り値が(0)ではないことを確認しているので、デバイスコンテキストの ハンドルは取得できているものと思われます。 API初心者です。よろしくお願いいたします。 補足) アプリケーションのウィンドウタイトルが「XXYYZZ」 色を特定したい座標を(60, 60)としています。 ############################################################################ public partial class Form1 : Form { [DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr hWnd); [DllImport( "user32.dll" )] static extern int ReleaseDC( IntPtr hWnd, IntPtr hDC ); [DllImport( "gdi32.dll" )] static extern int GetPixel( IntPtr hDC, int x, int y ); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { foreach (Process p in Process.GetProcesses()) { if (p.MainWindowHandle != IntPtr.Zero) { if (p.MainWindowTitle == "XXYYZZ") { IntPtr dc = GetDC(p.MainWindowHandle); MessageBox.Show(GetPixel(dc, 60, 60).ToString()); } } } } }

  • C# でパネルのマウスイベントが取得できない

    OpenNI(キネクトセンサー)のサンプルプログラムは、C#のパネルにbitmapを描画し続けるのですが、 描画中、そのパネルのマウスイベントが生じない(無視されている?)ようです。マウスの位置から 補助線を引いたりしたいのですが、描画中にマウスイベントが取得できる方法をご存知の方、お願いします。以下サンプルソースそのままです。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Text; using System.Windows.Forms; using OpenNI; using System.Threading; using System.Drawing.Imaging; namespace SimpleViewer.net { private unsafe void ReaderThread() { DepthMetaData depthMD = new DepthMetaData(); while (this.shouldRun) { try { this.context.WaitOneUpdateAll(this.depth); } catch (Exception) { } this.depth.GetMetaData(depthMD); lock (this) { Rectangle rect = new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height); BitmapData data = this.bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); ushort* pDepth = (ushort*)this.depth.DepthMapPtr.ToPointer(); // set pixels for (int y = 0; y < depthMD.YRes; ++y) { byte* pDest = (byte*)data.Scan0.ToPointer() + y * data.Stride; for (int x = 0; x < depthMD.XRes; ++x, ++pDepth, pDest += 3) { byte pixel = (byte)this.histogram[*pDepth]; pDest[0] = 0; pDest[1] = pixel; pDest[2] = pixel; } } this.bitmap.UnlockBits(data); } this.Invalidate(); } } private readonly string SAMPLE_XML_FILE = @"../../../Data/SamplesConfig.xml"; private Context context; private ScriptNode scriptNode; private DepthGenerator depth; private Thread readerThread; private bool shouldRun; private Bitmap bitmap; private int[] histogram; } }

専門家に質問してみよう