Case code
#include <Windows.h>LRESULT CALLBACK WndProc (HWND hwnd,uint message,wparam wparam,lparam LPARAM) {HDC hdc; Paintstruct PS; Rect rect; Switch(message) { CaseWm_create:playsound (TEXT ("Hellowin.wav"), null,snd_filename|Snd_async); return 0; CaseWM_PAINT:HDC= BeginPaint (hwnd,&PS); GetClientRect (hwnd,&rect); DrawText (Hdc,text ("hello,windows98!"),-1, &rect,dt_singleline| dt_center|dt_vcenter); EndPaint (hwnd,&PS); return 0; CaseWm_destroy:postquitmessage (0); return 0; } returnDefWindowProc (Hwnd,message,wparam,lparam);}intWINAPI WinMain (hinstance hinstance,hinstance prehinstance,pstr cmdline,inticmdshow) { StaticTCHAR szappname[] = TEXT ("Hellowin"); HWND hwnd;//CanvasMSG msg;//InformationWndclass Wndclass;//form Category
//start initialization for WNDCLASS//Horizontal Redraw//Vertical RedrawWndclass.style = Cs_hredraw |Cs_vredraw; //The window class determines the window procedure for processing a window message, which is actually a pointer to the functionWndclass.lpfnwndproc =WndProc; Wndclass.cbclsextra=0; Wndclass.cbwndextra=0; //Current application instance handleWndclass.hinstance =hinstance; //load icon to get icon handle for program use//for function LoadIcon, if you want to load the icon inside this program, you need to replace the handle of the program instance with null//The second parameter is used to identify the icon//IDI ID for an iconWndclass.hicon =LoadIcon (null,idi_application); //load the mouse cursor to get the mouse handle for use by the programWndclass.hcursor =loadcursor (Null,idc_arrow); //Specify a background color for the client area of the window//handle to a brush//getstockobject,windows API functions. This function retrieves a handle to a predefined alternate pen, brush, font, or palette. Wndclass.hbrbackground =(Hbrush) getstockobject (White_brush); Wndclass.lpszmenuname=NULL; Wndclass.lpszclassname=Szappname; //before you create an application window, you must call the function registerclass to register the window class. if(! RegisterClass (&wndclass))//Registering a window class for the application window{MessageBox (Null,text ("The program requires Windows nt!"), szappname,mb_iconerror); return 0; } //Create a window based on a window class//handle to the windowhwnd = CreateWindow (Szappname,//window class nameTEXT ("The Hello program"),//Window TitleWs_overlappedwindow,//window StyleCw_usedefault,// Initial x coordinateCw_usedefault,//initial y-coordinateCw_usedefault,//initial x-Direction dimensionsCw_usedefault,//initial y-direction dimensionsNull//parent window handleNull//Window menu handleHINSTANCE,//Window Instance handleNULL);//Creating Parameters//after the CreateWindow is complete, only some of the CreateWindow information is saved in memory, if you want to display the window in the screenShowWindow (Hwnd,sw_shownormal); //instructs the window to redraw itselfUpdateWindow (HWND); //retrieve information from a message queue and search for it all the time//the second parameter ~ Fourth parameter is either NULL or 0 indicates that the program wants to get all the window messages created by the program while(GetMessage (&msg,null,0,0)) { //Translate some keyboard messages//return MSG structure to Windows for conversion of certain keyboard messagesTranslateMessage (&msg); //send a message to a window procedureDispatchMessage (&msg); } //return msg.wparam; return 0;}
Tips
(i) If you want to invoke the PlaySound function, you need to add a winmm.lib reference to the environment, otherwise the following error will occur:
unresolved external symbol [email protected], the symbol in function "long __stdcall WndProc (struct hwnd__ *,unsigned int,unsigned int,long)" ([Email Prote Cted]@[email Protected]@[email protected]) is referenced.
The specific configuration steps are as follows:
①: Find Solution Explorer , right-click on your project , select Properties
②: Locate the input under the connector under configuration Properties
③: Small triangle after selecting additional dependencies
④: Add winmm.lib, click OK, apply
⑤: Click to run, you can get the desired effect
(b) I am using Windows XP Classic boot music instead of hellowin.wav, good resources are needed to share the drops, here to share to everyone: Hellowin
2 windows with messages