

C# 纯控制台创建一个全屏窗口
source link: https://lindexi.gitee.io/post/C-%E7%BA%AF%E6%8E%A7%E5%88%B6%E5%8F%B0%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%85%A8%E5%B1%8F%E7%AA%97%E5%8F%A3.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本文告诉大家如何使用 win32 方法创建一个全屏的窗口
使用 user32.dll 的 CreateWindowExW 方法就能创建窗口,代码请看
internal class Program
{
private static void Main(string[] args)
{
var thread = new Thread(() =>
{
var customWindow = new Window("sdf");
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Console.ReadLine();
}
}
internal class Window : IDisposable
{
public Window(string className)
{
var windClass = new WNDCLASS
{
lpszClassName = className
};
_wndProc = CustomWndProc;
windClass.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_wndProc);
RegisterClassW(ref windClass);
// Create window
_mHwnd = CreateWindowExW
(
0,
className,
string.Empty,
0,
0,
0,
0,
0,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero
);
const int SW_MAXIMIZE = 3;
ShowWindow(_mHwnd, SW_MAXIMIZE);
int ret;
while ((ret = GetMessage(out var msg, _mHwnd, 0, 0)) != 0)
{
if (ret == -1)
{
//-1 indicates an error
}
else
{
TranslateMessage(ref msg);
DispatchMessage(ref msg);
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
[DllImport("user32.dll", SetLastError = true)]
private static extern ushort RegisterClassW([In] ref WNDCLASS lpWndClass);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr CreateWindowExW
(
uint dwExStyle,
[MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPWStr)] string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam
);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr DefWindowProcW
(
IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam
);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool DestroyWindow(IntPtr hWnd);
private void Dispose(bool disposing)
{
if (!_mDisposed)
{
if (disposing)
{
// Dispose managed resources
}
// Dispose unmanaged resources
if (_mHwnd != IntPtr.Zero)
{
DestroyWindow(_mHwnd);
_mHwnd = IntPtr.Zero;
}
}
}
[DllImport("user32.dll")]
private static extern int GetMessage(out int lpMsg, IntPtr hWnd, uint wMsgFilterMin,
uint wMsgFilterMax);
[DllImport("user32.dll")]
private static extern bool TranslateMessage([In] ref int lpMsg);
[DllImport("user32.dll")]
private static extern IntPtr DispatchMessage([In] ref int lpmsg);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private static IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
return DefWindowProcW(hWnd, msg, wParam, lParam);
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WNDCLASS
{
public readonly uint style;
public IntPtr lpfnWndProc;
public readonly int cbClsExtra;
public readonly int cbWndExtra;
public readonly IntPtr hInstance;
public readonly IntPtr hIcon;
public readonly IntPtr hCursor;
public readonly IntPtr hbrBackground;
[MarshalAs(UnmanagedType.LPWStr)] public readonly string lpszMenuName;
[MarshalAs(UnmanagedType.LPWStr)] public string lpszClassName;
}
private delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
private readonly WndProc _wndProc;
private bool _mDisposed;
private IntPtr _mHwnd;
}
本文代码放在github 欢迎小伙伴访问
本文会经常更新,请阅读原文: https://blog.lindexi.com/post/C-%E7%BA%AF%E6%8E%A7%E5%88%B6%E5%8F%B0%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%85%A8%E5%B1%8F%E7%AA%97%E5%8F%A3.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。
如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 。
无盈利,不卖课,做纯粹的技术博客
以下是广告时间
推荐关注 Edi.Wang 的公众号
欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利
Recommend
-
4
C# 从零开始写 SharpDx 应用 控制台创建 Sharpdx 窗口本文告诉大家如何在控制台使用 SharpDx 创建窗口,这是一个底层的博客,我会用很多博客告诉大家如何从控制台创建一个高性能渲染程序 如果想看更多关于底层渲染的博客,请点击
-
5
在 Electron 中还存在一种创建窗口的方式,就是使用 HTML 5 的 API 创建窗口。在 HTML 5 中提供了 window.open 方法用于打开一个子窗口,该方法返回一个 BrowserWindowProxy 对象,并且打开了一个功能受限的窗口。 window.open 方法的原型如下。
-
47
MAGPIE Magpie可以将任意窗口放大至全屏,支持多种高级缩放算法,包括Lanczos、Anime4K、FSR、
-
9
WPF 稳定的全屏化窗口方法本文来告诉大家在 WPF 中,设置窗口全屏化的一个稳定的设置方法。在设置窗口全屏的时候,经常遇到的问题就是应用程序虽然设置最大化加无边框,但是此方式经常会有任务栏冒出来,或者说窗口没有贴屏幕的边。本文的方法是基于 Win32...
-
21
使用 Silk.NET 创建 OpenGL 空窗口项目例子 本文告诉大家如何使用 Silk.NET 创建 OpenGL 空窗口项目。在 dotnet 基金会下,开源维护 Silk.NET 仓库,此仓库提供了渲染相关的封装逻辑,包括 DX 和 OpenGL 等等的封装,利用此封装可以用来代替原有的...
-
4
MFC系列(一)创建空白窗口 Published at: 2013-08-26 | Reading: 1503 words ~3min | PV/UV: 7/7 ...
-
8
WPF 全屏窗口将让 Chrome 97 视频停止播放 无论是使用 WPF 全屏窗口,还是高性能全屏透明窗口,都会在 Chrome 97 以及使用 chromium 对应版本内核的应用的视频停止播放。这是 chromium 的一个优化,因为 chromium 认为,如果有全屏窗口盖在上面,...
-
6
V2EX › macOS MacOS 窗口全屏如何固定在当前桌面 ...
-
10
吕毅 发表于 7 小时前 在深入了解 WPF Dispatcher 的工作原理(Invoke/InvokeAsync 部分)中,我提到 Dis...
-
5
如何在控制台程序中监听 Windows 前台窗口的变化 吕毅 发表于 3 天前 前一段时间总会时不时发现当前正在打字的窗口突然失去了焦点,于是很希望有个工具能实时监听前台窗口的变化,并实时输出出来。本文会介绍两...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK