-
Notifications
You must be signed in to change notification settings - Fork 19
/
Program.cs
88 lines (73 loc) · 3.73 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Drawing;
using WInterop.Gdi;
using WInterop.Windows;
namespace Windows101;
internal class Program
{
// To create a Windows Application in .NET Core you must do the following things:
//
// 1. Create a .NET Core Console App.
// 2. Double click on the project and change <OutputType> to WinExe.
// 3. Mark the Main method as [STAThread].
// Optional- to make things look better, add an Application Manifest file and
// change it to match the one included in this project.
[STAThread]
private static void Main()
{
// You can just show message boxes to interact
Windows.MessageBox("Hello World.", caption: "Hello");
// Or create actual Window classes and run them. A Window Class in Windows
// includes a few basic things:
//
// 1. Appearance settings (border, icon, background, etc.)
// 2. A callback pointer for messages (mouse, keyboard, etc.)
// 3. An optional menu
//
// The Window Class is a template that actual Window instances are created
// from. WInterop wraps the registration and callbacks in "WindowClass"
// that you can derive from. "CreateMainWindowAndRun" will create an
// instance of the Window for the given WindowClass and loop processing
// messages until the Window is closed.
Windows.CreateMainWindowAndRun(new WindowClass(), windowTitle: "So Simple");
// To display a message in a Window you have to draw it yourself in response
// to a message to draw the window contents.
Windows.CreateMainWindowAndRun(new HelloWindow(), windowTitle: "Hello!");
}
private class HelloWindow : WindowClass
{
// Overriding the callback method will allow us to provide our own custom behavior
protected override LResult WindowProcedure(WindowHandle window, MessageType message, WParam wParam, LParam lParam)
{
switch (message)
{
// The Paint message is sent when the Window contents need drawn.
case MessageType.Paint:
// Drawing is done in a Device Context by calling BeginPaint(). When the
// DeviceContext is disposed it will call EndPaint().
using (DeviceContext dc = window.BeginPaint())
{
Rectangle client = window.GetClientRectangle();
// The default font is really small on modern PCs, so we'll take the extra step
// to select a font into our device context before drawing the text.
using FontHandle font = Gdi.CreateFont(
height: client.Height / 5,
family: FontFamilyType.Swiss);
dc.SelectObject(font);
// Draw the given text in the middle of the client area of the Window.
dc.DrawText(
"Hello, .NET Core!",
bounds: client,
TextFormat.SingleLine | TextFormat.Center | TextFormat.VerticallyCenter);
// Put the system font back as we're going to dispose our font
dc.SelectObject(StockFont.System);
}
// Return 0 to indicate we've handled the message
return 0;
}
// Let the base class handle any other messages
return base.WindowProcedure(window, message, wParam, lParam);
}
}
}