Win32 API implements a simple message window sample _c language

Source: Internet
Author: User
Tags win32

Copy Code code as follows:

#include <windows.h>

Lresult CALLBACK WndProc (Hwnd,uint,wparam,lparam);

int WINAPI WinMain (hinstance hinstance,hinstance hprevinstance,pstr szcmdline,int)
{
Static TCHAR Szappname[]=text ("Hellowin");
HWND hwnd;
MSG msg;
Wndclass Wndclass;

Wndclass.style =cs_hredraw|cs_vredraw;
Wndclass.lpfnwndproc =wndproc;
Wndclass.cbclsextra = 0;
Wndclass.cbwndextra = 0;
Wndclass.hinstance =hinstance;
Wndclass.hicon =loadicon (null,idi_application);
Wndclass.hcursor =loadcursor (Null,idc_arrow);
Wndclass.hbrbackground = (hbrush) getstockobject (White_brush);
Wndclass.lpszmenuname =null;
Wndclass.lpszclassname =szappname;

if (! RegisterClass (&wndclass))
{
MessageBox (Null,text ("This program requires Windows nt!"), Szappname,mb_iconerror);
return 0;
}

Hwnd=createwindow (Szappname,text ("The Hello Program"), Ws_overlappedwindow,cw_usedefault,
Cw_usedefault,cw_usedefault,cw_usedefault,null,null,hinstance,null);
ShowWindow (hwnd,icmdshow);
UpdateWindow (HWND);

while (GetMessage (&msg,null,0,0))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
return msg.wparam;
}

Lresult CALLBACK WndProc (HWND hwnd,uint message,wparam Wparam,lparam)
{
HDC HDC;
Paintstruct PS;
RECT RECT;

Switch (message)
{
Case WM_CREATE:
PlaySound (TEXT ("Hellowin.wav"), null,snd_filename| Snd_async);
return 0;

Case WM_PAINT:
Hdc=beginpaint (HWND,&PS);

GetClientRect (Hwnd,&rect);

DrawText (Hdc,text ("HELLO world!"), -1,&rect,
dt_singleline| dt_center| Dt_vcenter);
EndPaint (HWND,&PS);
return 0;
Case Wm_destroy:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (Hwnd,message,wparam,lparam);
}

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.