您的位置:首页 > 编程语言 > C#

一个简单的 C# 语言编写的 WIN32 程序

2009-08-19 21:06 831 查看
刚k始学习 C / C++ / windows 编程时接触的经典 demo 程序, 今日拿来用 C# 玩耍了一下.



其实用 C# 编写 win32 窗口和控件的小型 wrapper 类库也无不可.



Delphi 的 VCL 可以拿来做参照.



代码备忘:





using System;
using System.Runtime.InteropServices;
/**
 * zYg - Dobzhansky 2009-8-19 20:58
 */
namespace demo
{
  class program
  {
    static void Main()
    {
    	// 初始化窗口类结构
      WNDCLASS wc = new WNDCLASS();
      wc.style = 0;
      wc.lpfnWndProc = SplashWindowProcedure;
      wc.hInstance = GetModuleHandle(null);
      wc.hbrBackground = (IntPtr)6;
      wc.lpszClassName = "CSharpWindow";
      wc.cbClsExtra = 0;
      wc.cbWndExtra = 0;
      wc.hIcon = IntPtr.Zero;
      wc.hCursor = IntPtr.Zero;
      wc.lpszMenuName = null;
      // 注册窗口类
      RegisterClass(wc);
      // 创建并显示窗口
      IntPtr hwnd;
      hwnd = CreateWindowEx(0,
        "CSharpWindow",
        "欢迎你来搞",
        (int)WS_STYLE.WS_OVERLAPPEDWINDOW,
        -1, -1, 400, 300,
        IntPtr.Zero, IntPtr.Zero, GetModuleHandle(null), null);
      ShowWindow(hwnd, 1);
      UpdateWindow(hwnd);
      // 进入消息循环
      MSG msg = new MSG();
      while (GetMessage(ref msg, IntPtr.Zero, 0, 0))
      {
        TranslateMessage(ref msg);
        DispatchMessage(ref msg);
      }
    }
    // 窗口过程
    static int SplashWindowProcedure(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)
    {
      switch (msg)
      {
        case WM_DESTROY:
          PostQuitMessage(0);
          break;
      }
      return DefWindowProc(hwnd, msg, wParam, lParam);
    }
  	[Flags]
  	public enum WS_STYLE:long
  	{
			WS_OVERLAPPED   =    0x00000000L,
			WS_CAPTION      =    0x00C00000L ,    /* WS_BORDER | WS_DLGFRAME  */
			WS_SYSMENU      =    0x00080000L,
			WS_THICKFRAME   =    0x00040000L,
			
			WS_MINIMIZEBOX  =    0x00020000L,
			WS_MAXIMIZEBOX  =    0x00010000L,
			
			WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED     | 
                             WS_CAPTION        | 
                             WS_SYSMENU        | 
                             WS_THICKFRAME     | 
                             WS_MINIMIZEBOX    | 
                             WS_MAXIMIZEBOX),
    }
    public const int WM_DESTROY = 2;
    public const int WM_CLOSE = 0x10;
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetModuleHandle(string moduleName);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr RegisterClass(WNDCLASS wc);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr CreateWindowEx(int dwExStyle, string lpszClassName, string lpszWindowName, int style, int x, int y, int width, int height, IntPtr hWndParent, IntPtr hMenu, IntPtr hInst, [MarshalAs(UnmanagedType.AsAny)] object pvParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool UpdateWindow(IntPtr hWnd);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool GetMessage(ref MSG msg, IntPtr hwnd, int minFilter, int maxFilter);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool TranslateMessage(ref MSG msg);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int DispatchMessage(ref MSG msg);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern void PostQuitMessage(int nExitCode);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class WNDCLASS
    {
      public int style = 0;
      public WNDPROC lpfnWndProc = null;
      public int cbClsExtra = 0;
      public int cbWndExtra = 0;
      public IntPtr hInstance = IntPtr.Zero;
      public IntPtr hIcon = IntPtr.Zero;
      public IntPtr hCursor = IntPtr.Zero;
      public IntPtr hbrBackground = IntPtr.Zero;
      public string lpszMenuName = null;
      public string lpszClassName = null;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct MSG
    {
      public IntPtr hwnd;
      public int message;
      public IntPtr wParam;
      public IntPtr lParam;
      public int time;
      public int pt_x;
      public int pt_y;
    }
    public delegate int WNDPROC(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: