Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
Hummel009 committed Jan 18, 2024
1 parent 7d5b0fc commit 150f549
Show file tree
Hide file tree
Showing 4 changed files with 2,590 additions and 34 deletions.
1 change: 1 addition & 0 deletions appCs/appCs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<RootNamespace>appCs</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

</Project>
21 changes: 11 additions & 10 deletions appCs/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,21 @@ public class Program
public const int DEFAULT_CAPACITY = 100;
private static readonly IntPtr field;
private static readonly List<string>? data;
private static WndProc? delegWndProc;
private static WinAPI.WndProc? delegWndProc;

private static readonly int[] FACTORIAL = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600];

public static void Main(string[] args)
{
var className = "HummelCalculator";
var windowTitle = "WinAPI";
delegWndProc = MyWndProc;
delegWndProc = ParentWndProc;

var windowClass = new WinAPI.WNDCLASSEX
{
cbSize = Marshal.SizeOf<WinAPI.WNDCLASSEX>(),
style = 0,
lpfnWndProc = Marshal.GetFunctionPointerForDelegate(delegWndProc!),
lpfnWndProc = delegWndProc,
cbClsExtra = 0,
cbWndExtra = 0,
hInstance = 0,
Expand All @@ -62,28 +63,28 @@ public static void Main(string[] args)
lpszClassName = className
};

WinAPI.RegisterClassEx(ref windowClass);
var atom = WinAPI.RegisterClassEx(ref windowClass);

var screenWidth = WinAPI.GetSystemMetrics(WinAPI.SystemMetrics.SM_CXSCREEN);
var screenHeight = WinAPI.GetSystemMetrics(WinAPI.SystemMetrics.SM_CYSCREEN);
var screenWidth = WinAPI.GetSystemMetrics(WinAPI.SystemMetric.SM_CXSCREEN);
var screenHeight = WinAPI.GetSystemMetrics(WinAPI.SystemMetric.SM_CYSCREEN);

var windowWidth = 260;
var windowHeight = 458;

var windowX = Math.Max(0, (screenWidth - windowWidth) / 2);
var windowY = Math.Max(0, (screenHeight - windowHeight) / 2);

var hwnd = WinAPI.CreateWindowEx(0, className, windowTitle, WS_VISIBLE | WS_CAPTION | WS_SYSMENU, 0, 0, windowWidth, windowHeight, 0, 0, 0, 0);
WinAPI.CreateWindowEx(0, atom, windowTitle, WS_VISIBLE | WS_CAPTION | WS_SYSMENU, windowX, windowY, windowWidth, windowHeight, 0, 0, 0, 0);

var msg = new WinAPI.MSG();
while (WinAPI.GetMessage(out msg, 0, 0, 0))
while (WinAPI.GetMessage(out msg, 0, 0, 0) != 0)
{
WinAPI.TranslateMessage(ref msg);
WinAPI.DispatchMessage(ref msg);
}
}
public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
private static IntPtr MyWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)

private static IntPtr ParentWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
return WinAPI.DefWindowProc(hWnd, msg, wParam, lParam);
}
Expand Down
49 changes: 25 additions & 24 deletions appCs/src/Wrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,52 @@ namespace Hummel
{
public class WinAPI
{
public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class WNDCLASSEX
public struct WNDCLASSEX
{
public uint cbSize;
public uint style;
public IntPtr lpfnWndProc;
public int cbSize;
public int style;
public WndProc lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string? lpszMenuName;
public string? lpszClassName;
public string lpszMenuName;
public string lpszClassName;
public IntPtr hIconSm;
}


[DllImport("user32.dll")]
public static extern IntPtr RegisterClassEx(ref WNDCLASSEX lpwcx);
public static extern ushort RegisterClassEx(ref WNDCLASSEX lpwcx);

public enum SystemMetrics : int
public enum SystemMetric : int
{
SM_CXSCREEN = 0,
SM_CYSCREEN = 1,
}

[DllImport("user32.dll")]
public static extern int GetSystemMetrics(SystemMetrics nIndex);
public static extern int GetSystemMetrics(SystemMetric smIndex);

[DllImport("user32.dll")]
public static extern IntPtr CreateWindowEx(
uint dwExStyle,
string lpClassName,
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam
);
int dwExStyle,
uint lpClassName,
string? lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);

[StructLayout(LayoutKind.Sequential)]
public struct MSG
Expand All @@ -70,14 +71,14 @@ public struct POINT
}

[DllImport("user32.dll")]
public static extern bool GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
public static extern int GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);

[DllImport("user32.dll")]
public static extern bool TranslateMessage(ref MSG lpMsg);

[DllImport("user32.dll")]
public static extern IntPtr DispatchMessage(ref MSG lpMsg);

[DllImport("user32.dll")]
public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

Expand Down
Loading

0 comments on commit 150f549

Please sign in to comment.