Fundamentals of Windows Programming

Source: Internet
Author: User
Tags textout

Fundamentals of Windows Programming

Create a Win32 Application EX_SDK , constructs an edit box control and a button in the program. The edit box is used to enter the coefficients of the unary two-second equation, separated by commas, when the calculate button is clicked, the equation coefficients are obtained, and the obtained roots are passed TextOut displayed in the Window client area.

Preparation and description of the experiment

( 1 ) with knowledge: simple SDK Programming Basics.

( 2 ) Prepare the required procedures for this machine.

( 3 ) Create this lab work folder "... \ Network Programming Experiment \ Experiment 1 "

Experimental content and procedures

1 . start Visual C + + 6.0

turn on the computer, start Visual C + + 6.0 System.

2 . Create a project and add code

①Select"file"→"New"menu commands, open the Application wizard, show"New"dialog box. Click"Engineering"tab, select from the list boxWIN32 Application(Win32application) Item. In the Project Name box, enterWin32Application project NameEX_SDK. Click the browse button...positioning the project to a folder"... \Network Programming Experiment\Experiment1 ".

②Click"Determine"button to continue. In the first step of the wizard dialog box, selectAn empty project(an empty project) item. Click"Complete"button, the system will displayAppWizard(Application Wizard) to create the information, click"Determine"button, the system will automatically create this application.

③Select again"file"→"New"menu commands that show the"New"dialog box. Click"file"tab, in the list box on the left, selectC + + Source Fileitem, on the right side of the"file"under the edit box, typeEx_sdk.cpp, click"Determine"button.

④ Enter the following code in the Open document window:

#include <windows.h> #include <math.h> #include <stdio.h>//to find the root of a two-second equation, the function returns the number of roots int getroot (float A, Float B, float C, double *root) {double Delta, Deltasqrt;delta = b*b-4.0 * A * C;IF (delta<0.0) return 0;//no root deltasqr t = sqrt (delta), if (a!=0.0) {root[0] = (-B + deltasqrt)/(2.0 * a); root[1] = (-B-DELTASQRT)/(2.0 * a);} elseif (b!=0.0) r Oot[0] = root[1] =-c/b;elsereturn 0;if (root[0] = = root[1]) return 1;else return 2; Char str[80]; LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);//window procedure int WINAPI WinMain (hinstance hinstance, hinstance Hprevinsta NCE, LPSTR lpcmdline, int ncmdshow) {HWND hwnd;//Window handle msg msg;//Message Wndclas Swndclass;//window class 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 = "Sdkwin";//Window class name if (! RegisterClass (&wndclass)//Register window {MessageBox (NULL, "window registration failed!)     "," Hellowin ", 0);     return 0; }//Create Window hwnd = CreateWindow ("Sdkwin",//Window class name "Experiment 1--windows Programming Basics",//Window Title Ws_overlappedwindow,//Window style cw_usedefault,//Window initial X Position cw_usedefault,//Window initial y position cw_usedefault,/                       /window initial x size Cw_usedefault,//Window initial y size NULL,                  Parent window Handle NULL,//Window menu handle HINSTANCE,                  Application instance handle NULL);   Create window Parameters ShowWindow (hwnd, nCmdShow),//Display window UpdateWindow (HWND),//Update window, including window's client area while (GetMessage (&A Mp;msg, NULL, 0, 0)) {translatemessage (&msg);//Convert some keyboard messages DispatchMessage (&msg);//Send messages to the window process, here is the WN Dproc} return Msg.wparam;} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {hdchdc; Paintstructps;statichwndhwndbutton, hwndedit;charstredit[80], stra[3][80], strhint[80];floata[3];d oubleroot[2]; Inti, J, K, M;switch (message) {Case WM_CREATE://window creates the resulting message Hwndedit = CreateWindow ("edit", NULL, ws_child| ws_visible| Ws_ border,?10, $, N, a, hwnd, NULL, NULL, NULL), Hwndbutton = CreateWindow ("button", "Calculation", ws_child| ws_visible|bs_pushbutton,240, 0, (+), hwnd, NULL, NULL, NULL); Case wm_command://Command message, the control produces a notification code in the high word of WParam if ((HWND) LParam = = Hwndbutton) && (HiWord (wParam) = = bn_clicked)) {//Gets the contents of the edit box control and converts it to a float value GetWindowText (Hwndedit, Stredit, 80);//Get edit box Contents//delimited String k = 0;m = 0;for(j=0; j<80; j + +) {if (stredit[j] = = ', ') {k++; m = 0;} else {stra[k][m] = stredit[j];m++;}} for (i=0; i<3; i++) a[i] = (float) atof (stra[i]);//convert string to float value int n = getroot (a[0], a[1], a[2], root); if (n<1) strc PY (str, "Equation without root!"); else sprintf (str, "solution of equation:%f,%f", root[0], root[1]); InvalidateRect (hwnd, NULL, TRUE);} Case WM_PAINT:HDC = BeginPaint (hwnd, &AMP;PS); strcpy (Strhint, "Please enter 3 coefficients for the two-second equation, separated by commas"); TextOut (HDC, ten, Strhint, strlen (Strhint)); TextOut (hdc, ten, N, str, strlen (str));          EndPaint (hwnd, &AMP;PS); return 0; Case wm_destroy://The message generated when the window closes postquitmessage (0); return 0;} Return DefWindowProc (HWND, message, WParam, lParam);//execute default message handling}

3 . Run and test

< Span style= "font-family:times; letter-spacing:0px "> Compile and run the program and fill in the edit box with a factor of two times the equation 6 - ' calculation " button, the result of the solution will be displayed ;



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.