A simple Win32 program written in C # Language

Source: Internet
Author: User

The classic demo program that I first came into contact with when I was learning C/C ++/Windows programming. Today I used C # to play with it.

In fact, using C # To compile a small Wrapper class library for Win32 windows and controls is also indispensable.

The VCL of Delphi can be used as a reference.

Code memo:

Using system; using system. runtime. interopservices;/*** zyg-Dobzhansky 2009-8-19 20:58 */namespace demo {class program {static void main () {// initialize the window class structure 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; // register the window class registerclass (WC); // create and display the window intptr hwnd; hwnd = createjavaswex (0, "csharpwindow", "Welcome ", (INT) ws_style.ws_overlappedwindow,-1,-1,400,300, intptr. zero, intptr. zero, getmodulehandle (null), null); showwindow (hwnd, 1); updatewindow (hwnd); // enter the message loop 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 {partition = 0x00000000l, ws_caption = partition,/* ws_border | ws_dlgframe */partition = 0x00080000l, ws_thickframe = 0x00040000l, ws_minimizebox = 0x00020000l, partition = 0x0000000l, partition = (partition | ws_caption | partition | ws_thickframe | ws_minimizebox | partition),} 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 createcontactwex (INT dwexstyle, string lpszclassname, string lpszwindowname, int style, int X, int y, int width, int height, intptr hwndparent, intptr hmenu, intptr hinst, [financialas (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, intptr hwnd, int minfilter, int maxfilter); [dllimport ("user32.dll", charset = charset. auto)] public static extern bool translatemessage (ref MSG); [dllimport ("user32.dll", charset = charset. auto)] public static extern int dispatchmessage (ref 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 );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.