動作環境
C++ Builder XE4
概要
-
以下の実装に変更する
- ボタン押下時にタイトルバー非表示
- ボタン押下時にタイトルバー表示
情報
- Show/hide titlebar of a form @ Delphi Magic
- Unityでデスクトップマスコットのためにタイトルバー非表示した by @gatosyocora 様
情報感謝です。
実装
Unit1.h
//---------------------------------------------------------------------------
# ifndef Unit1H
# define Unit1H
//---------------------------------------------------------------------------
# include <System.Classes.hpp>
# include <Vcl.Controls.hpp>
# include <Vcl.StdCtrls.hpp>
# include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE で管理されるコンポーネント
TButton *B_hide;
TButton *B_show;
void __fastcall B_hideClick(TObject *Sender);
void __fastcall B_showClick(TObject *Sender);
private: // ユーザー宣言
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
# endif
Unit1.cpp
//---------------------------------------------------------------------------
# include <vcl.h>
# pragma hdrstop
# include "Unit1.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
this->Position = poScreenCenter; // 画面中央に表示
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_hideClick(TObject *Sender)
{
DWORD astyle;
astyle = GetWindowLong(this->Handle, GWL_STYLE);
SetWindowLong(this->Handle, GWL_STYLE, astyle & ~WS_CAPTION);
// SetWindowLongの変更適用
SetWindowPos(this->Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_showClick(TObject *Sender)
{
DWORD astyle;
// this->BorderStyle = bsSingle;
astyle = GetWindowLong(this->Handle, GWL_STYLE);
SetWindowLong(this->Handle, GWL_STYLE, astyle | WS_CAPTION);
// SetWindowLongの変更適用
SetWindowPos(this->Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);
}
//---------------------------------------------------------------------------
備考
SetWindowPos()をコールすることでSetWindowLong()の内容が適用されるというのは @gatosyocora さんの記事で知りました。
情報感謝です。
SetWindowPos()
SetWindowPos()の各種パラメータについては以下に詳しい。
- SetWindowPos @ MSDN
動作例
タイトルバーないときー (B_hide押下後)
タイトルバーあるときー (B_show押下後)
備考 > Windows 10: 表示位置のずれ
動作環境: Windows 10 Pro (64bit) バージョン 1803 (April 2018 Update)
- タイトルバー非表示からタイトルバー表示に戻った時、ウィンドウの位置が下にずれる
- 他のウィンドウをクリックすると、ずれていたウィンドウが元の位置に戻る
Windows 10の処理でおかしな部分があるのだろう。
(対処は検討が必要)
モニタの解像度によっても症状の有無が異なるようだ。
- FullHD: ずれは起きない
- 4K UHD: ずれが起きる
ずれの原因は下記に記載した。