Phoenix's Blog

博客园 首页 新随笔 联系 订阅 管理
无聊的时候在.Net下用API写了一个Window窗户.
简单比较了一下,纯API的窗体运行启动要快点,起始占用内存也少点,大概6M,但比起用asm创建的一般才2M多点还是看得出.Net确实是吃内存的大户啊.^_^


代码
/*    Snippet Compiler
*    Author : Phoenix Chen    
*    Date   : 2006-02-10
*    Comment:
*/

using System;
using System.Runtime.InteropServices;

namespace CzG
{
    
public class MyClass
    
{
        
static IntPtr hInstance;
        
static IntPtr hWinMain;
        
static string strAppName;
        
static string strClassName;
        
        
public static void Main()
        
{
            WinMain();
        }

        
        
// Initialize Window
        public static void WinMain()
        
{
            strAppName         
= "API Window";
            strClassName     
= "MyClass";
            
            
// Get Application Module Handle
            
// API : GetModuleHandle
            hInstance = Marshal.GetHINSTANCE(typeof(MyClass).Assembly.GetModules()[0]);
            
            WinAPI.WNDCLASSEX wndClass 
= new WinAPI.WNDCLASSEX();
            wndClass.cbSize 
= Marshal.SizeOf(wndClass);
            wndClass.style 
= 3;//CS_HREDRAW | CS_VREDRAW;
            wndClass.lpfnWndProc = new WindowProc(WndProc); // using Marshal.GetFunctionPointerForDelegate to get a delegate's handle in .Net Framework 2.0
            wndClass.hInstance = hInstance;
            wndClass.hIcon 
= WinAPI.LoadIcon(IntPtr.Zero,32512); // IDI_APPLICATION , MAKEINTRESOURCE macro in C++ converts a WORD to DWORD
            wndClass.hCursor = WinAPI.LoadCursor(IntPtr.Zero,32512); // IDC_ARROW
            wndClass.hbrBackground = 6//COLOR_WINDOWFRAME 
            wndClass.lpszClassName = strClassName;
            
            
if(WinAPI.RegisterClassEx(ref wndClass) == 0)
            
{
                
return;
            }

            
            
// Create & Display Window
            
//                                 WS_EX_CLIENTEDGE              WS_DLGFRAME
            hWinMain = WinAPI.CreateWindowEx(0x200,strClassName,strAppName,WS_OVERLAPPEDWINDOW ,100,100,400,300,0,0,hInstance,0);
            
            WinAPI.ShowWindow(hWinMain,
1); //SW_SHOWNORMAL
            WinAPI.UpdateWindow(hWinMain);
            
            
// Message loop
            WinAPI.MSG msg = new WinAPI.MSG();
            
while(true)
            
{
                
if(!WinAPI.GetMessage(ref msg,0,0,0))
                
{
                    
break;
                }

                
                WinAPI.TranslateMessage(
ref msg);
                WinAPI.DispatchMessage(
ref msg);
            }

        }

        
        
// Window Procedure
        public static int WndProc(IntPtr hWnd,uint uMsg,int wParam,int lParam)
        
{
            
switch(uMsg)
            
{
                
case 0x10// WM_CLOSE
                    WinAPI.DestroyWindow(hWinMain);
                    WinAPI.PostQuitMessage(
0);
                    
break;
                
default:
                    
return WinAPI.DefWindowProc(hWnd,uMsg,wParam,lParam);
            }

            
return 0;
        }

    
public const int WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);    
    
public const int WS_MAXIMIZEBOX = 0x10000;
    
public const int WS_MINIMIZEBOX = 0x20000;
    
public const int WS_SYSMENU = 0x80000;
    
public const int WS_OVERLAPPED = 0x0;
    
public const int WS_THICKFRAME = 0x40000;
    
public const int WS_CAPTION = 0xC00000;

    }

    
    
public delegate int WindowProc(IntPtr hWnd,uint uMsg,int wParam,int lParam);

    
public class WinAPI
    
{
        
Structures
        
        
WINAPIs 
    }

}

.Net下的代码
/*    Snippet Compiler
*    Author : Phoenix Chen    
*    Date   :
*    Comment:
*/

using System;
using System.Windows.Forms;

namespace CzG
{
    
public class MyClass : Form
    
{
        
public MyClass()
        
{
            
this.Text = ".Net Window";
        }

        
public static void Main()
        
{
            Application.Run(
new MyClass());
        }

    }

}

posted on 2006-02-10 23:52  Phoenix  阅读(890)  评论(0编辑  收藏  举报