How to Customize Title Bar and Window of Desktop Application

How to customize title bar and window of desktop application

#########################################################
## customize Title bar
## dotpy.ir
## iraj.jelo@gmail.com
#########################################################
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import Qt

class TitleBar(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setWindowFlags(Qt.FramelessWindowHint);
css = """
QWidget{
Background: #AA00AA;
color:white;
font:12px bold;
font-weight:bold;
border-radius: 1px;
height: 11px;
}
QDialog{
Background-image:url('img/titlebar bg.png');
font-size:12px;
color: black;

}
QToolButton{
Background:#AA00AA;
font-size:11px;
}
QToolButton:hover{
Background: #FF00FF;
font-size:11px;
}
"""
self.setAutoFillBackground(True)
self.setBackgroundRole(QtGui.QPalette.Highlight)
self.setStyleSheet(css)
self.minimize=QtGui.QToolButton(self);
self.minimize.setIcon(QtGui.QIcon('img/min.png'));
self.maximize=QtGui.QToolButton(self);
self.maximize.setIcon(QtGui.QIcon('img/max.png'));
close=QtGui.QToolButton(self);
close.setIcon(QtGui.QIcon('img/close.png'));
self.minimize.setMinimumHeight(10);
close.setMinimumHeight(10);
self.maximize.setMinimumHeight(10);
label=QtGui.QLabel(self);
label.setText("Window Title");
self.setWindowTitle("Window Title");
hbox=QtGui.QHBoxLayout(self);
hbox.addWidget(label);
hbox.addWidget(self.minimize);
hbox.addWidget(self.maximize);
hbox.addWidget(close);
hbox.insertStretch(1,500);
hbox.setSpacing(0);
self.setSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Fixed);
self.maxNormal=False;
close.clicked.connect(self.close);
self.minimize.clicked.connect(self.showSmall);
self.maximize.clicked.connect(self.showMaxRestore);

def showSmall(self):
box.showMinimized();

def showMaxRestore(self):
if(self.maxNormal):
box.showNormal();
self.maxNormal= False;
self.maximize.setIcon(QtGui.QIcon('img/max.png'));
print '1'
else:
box.showMaximized();
self.maxNormal= True;
print '2'
self.maximize.setIcon(QtGui.QIcon('img/max2.png'));

def close(self):
box.close()

def mousePressEvent(self,event):
if event.button() == Qt.LeftButton:
box.moving = True; box.offset = event.pos()

def mouseMoveEvent(self,event):
if box.moving: box.move(event.globalPos()-box.offset)

class Frame(QtGui.QFrame):
def __init__(self, parent=None):
QtGui.QFrame.__init__(self, parent)
self.m_mouse_down= False;
self.setFrameShape(QtGui.QFrame.StyledPanel)
css = """
QFrame{
Background: #D700D7;
color:white;
font:13px ;
font-weight:bold;
}
"""
self.setStyleSheet(css)
self.setWindowFlags(Qt.FramelessWindowHint);
self.setMouseTracking(True);
self.m_titleBar= TitleBar(self);
self.m_content= QtGui.QWidget(self);
vbox=QtGui.QVBoxLayout(self);
vbox.addWidget(self.m_titleBar);
vbox.setMargin(0);
vbox.setSpacing(0);
layout=QtGui.QVBoxLayout(self);
layout.addWidget(self.m_content);
layout.setMargin(5);
layout.setSpacing(0);
vbox.addLayout(layout);
# Allows you to access the content area of the frame
# where widgets and layouts can be added

def contentWidget(self):
return self.m_content

def titleBar(self):
return self.m_titleBar

def mousePressEvent(self,event):
self.m_old_pos = event.pos();
self.m_mouse_down = event.button()== Qt.LeftButton;

def mouseMoveEvent(self,event):
x=event.x();
y=event.y();

def mouseReleaseEvent(self,event):
m_mouse_down=False;

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv);
box = Frame()
box.move(60,60);
l=QtGui.QVBoxLayout(box.contentWidget());
l.setMargin(0);
edit=QtGui.QLabel("""I would've did anything for you to show you how much I adored you
But it's over now, it's too late to save our loveJust promise me you'll think of me
Every time you look up in the sky and see a star 'cuz I'm your star.""");
l.addWidget(edit)
box.show()
app.exec_()

Custom title bar color on Windows 10 desktop app

In WPF (and I assume WinForms), it is easier to create a borderless window and create the title bar yourself. Here is WPF code to do it,,,

First the C#:

public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
}

public event PropertyChangedEventHandler PropertyChanged;
private void Notify(params string[] PropertyNames)
{
foreach (string PropertyName in PropertyNames)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}

public Visibility CanMaximize { get { return WindowState == WindowState.Normal ? Visibility.Visible : Visibility.Collapsed; } }
public Visibility CanRestore { get { return WindowState == WindowState.Maximized ? Visibility.Visible : Visibility.Collapsed; } }

private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
{
Application.Current.Shutdown();
}

private void CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}

private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}

private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e)
{
this.WindowState = WindowState.Maximized;
Notify("CanMaximize", "CanRestore");
}

private void RestoreWindow(object sender, ExecutedRoutedEventArgs e)
{
this.WindowState = WindowState.Normal;
Notify("CanMaximize", "CanRestore");
}

private void TitleBar_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
if (TitleBar.IsMouseDirectlyOver)
DragMove();
}
}

}

now the XAML:

<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="Segoe UI Symbol"
mc:Ignorable="d" x:Name="root"
Title="MainWindow" Height="450" Width="800" WindowStyle="None">
<Window.CommandBindings>
<CommandBinding Command="SystemCommands.CloseWindowCommand" Executed="CloseWindow" CanExecute="CanExecute" />
<CommandBinding Command="SystemCommands.MinimizeWindowCommand" Executed="MinimizeWindow" CanExecute="CanExecute" />
<CommandBinding Command="SystemCommands.MaximizeWindowCommand" Executed="MaximizeWindow" CanExecute="CanExecute" />
<CommandBinding Command="SystemCommands.RestoreWindowCommand" Executed="RestoreWindow" CanExecute="CanExecute" />
</Window.CommandBindings>
<DockPanel>
<DockPanel DockPanel.Dock="Top" Background="#FF000020" x:Name="TitleBar" MouseMove="TitleBar_MouseMove">
<Image DockPanel.Dock="Left" Source="{Binding ElementName=root, Path=Icon}" Stretch="Uniform" Height="25" Width="25" Margin="2,0"/>
<Button Command="SystemCommands.CloseWindowCommand" DockPanel.Dock="Right" Margin="0" Padding="0">
<Grid Width="25" Height="25" Background="{Binding ElementName=TitleBar, Path=Background}">
<TextBlock Text="" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
</Grid>
</Button>
<Button Command="SystemCommands.RestoreWindowCommand" DockPanel.Dock="Right" Visibility="{Binding ElementName=root, Path=CanRestore}">
<Grid Width="25" Height="25" Background="{Binding ElementName=TitleBar, Path=Background}">
<TextBlock Text="" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
</Grid>
</Button>
<Button Command="SystemCommands.MaximizeWindowCommand" DockPanel.Dock="Right" Visibility="{Binding ElementName=root, Path=CanMaximize}">
<Grid Width="25" Height="25" Background="{Binding ElementName=TitleBar, Path=Background}">
<TextBlock Text="𐰿" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
</Grid>
</Button>
<Button Command="SystemCommands.MinimizeWindowCommand" DockPanel.Dock="Right">
<Grid Width="25" Height="25" Background="{Binding ElementName=TitleBar, Path=Background}">
<TextBlock Text="" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="White" />
</Grid>
</Button>
<TextBlock Text="{Binding ElementName=root, Path=Title, FallbackValue='Application Title'}" Foreground="White" VerticalAlignment="Center"/>
</DockPanel>
<Grid>
<TextBlock Text="Content goes here." VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</DockPanel>

Change title bar color in flutter windows desktop

for this kind of customization, you will need to use the bitsdojo_windows community package, that allows you to customize the appearance of the window

How can I remove windows title bar or customize it in compose desktop?

To remove it you can use undecorated argument for Window:

fun main() = application {
Window(
onCloseRequest = ::exitApplication,
undecorated = true,
) {
App()
}
}

WPF Window Titlebar

People have been wanting to see how I got this to work

Put this under your class

[DllImport("DwmApi")] 
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
const int DWWMA_CAPTION_COLOR = 35;

in whatever function (main?)

get the window handle or hwnd like this

IntPtr hWnd = new WindowInteropHelper(this).EnsureHandle();

then define the color

int[] colorstr = new int[]{0xFF00FF};

the 0x string thing is formatted like this
0xRRGGBB
replace the letters to their corresponding values

then make it happen

DwmSetWindowAttribute(hWnd, DWWMA_CAPTION_COLOR, colorstr, 4);

note: this only works in windows 11


here is the complete version if your lazy

class MainWindow : Window
{

[DllImport("DwmApi")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
const int DWWMA_CAPTION_COLOR = 35;

public MainWindow()
{
IntPtr hWnd = new WindowInteropHelper(this).EnsureHandle();
int[] colorstr = new int[]{0xFF00FF};
DwmSetWindowAttribute(hWnd, DWWMA_CAPTION_COLOR, colorstr, 4);
}

}

oh yes and import these

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;

Edit: the color goes in the BGR format, so make sure it goes blue, green, red and not red, green blue

How to customize the window title bar of an Electron app?

Hide the default titlebar by creating a frameless window:

// main.js
window = new BrowserWindow({
titlebarStyle: 'hidden',
trafficLightPosition: {
x: 15,
y: 13, // macOS traffic lights seem to be 14px in diameter. If you want them vertically centered, set this to `titlebar_height / 2 - 7`.
},
})

Then create your own makeshift titlebar using HTML + CSS:

<!-- index.html -->
<body>
<header class="titlebar"></header>
...
</body>
/* styles.css */
.titlebar {
background-color: #f0f0f0;
height: 40px;
border-bottom: 1px solid #d0d0d0;
-webkit-app-region: drag; /* Allow user to drag the window using this titlebar */
-webkit-user-select: none; /* Prevent user from selecting things */
user-select: none;
}

The result so far:

Scroll bar overlaps titlebar

Notice that the titlebar appears under the scrollbar. It even moves when the user scrolls. We need to separate it from the scrollable content by wrapping everything below the titlebar in a <div class="main-content"> and then adding these styles:

.main-content {
height: calc(100vh - 40px); /* Force the content to take up the viewport height minus the titlebar height */
overflow: auto; /* Allow the main content to be scrollable */
}
body {
overflow: hidden; /* Make the HTML body be non-scrollable */
}

Final result:

Scrollbar appears below the titlebar

Now you can add whatever HTML content you want up there.



Related Topics



Leave a reply



Submit