2.4 Windows应用程序举例(3)
【例2.2】在Win32应用程序中使用控件CreateControl(Ex2_02)。
控件是在系统内部定义的用于和用户交互的基本单元,它常用于对话框中。在Win32应用程序中,可在窗口基础上通过CreateWindow函数来创建一个控件窗口。
(1) 新建一个"An empty project"类型的Win32 Application(Win32应用程序)项目,项目的名称为"CreateControl"。
(2) 新建一个C++(www.cppentry.com)源文件(Source File)CreateControl.cpp,并输入如下代码:
- #include
- #include
- #include
-
- 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;
- deltasqrt = sqrt(delta);
- if (a!=0.0) {
- root[0] = (-b + deltasqrt)/(2.0 * a);
- root[1] = (-b - deltasqrt)/(2.0 * a);
- } else
- if (b!=0.0) root[0] = root[1] = -c/b;
- else return 0;
- if (root[0] == root[1]) return 1;
- else return 2;
- }
- char str[80];
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow)
- {
- 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 = "Hello";
- if (!RegisterClass (&wndclass))
- {
- MessageBox (NULL, "窗口注册失败!", "HelloWin", 0);
- return 0;
- }
-
- hwnd = CreateWindow ("Hello",
- "Application窗口",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- NULL,
- NULL,
- hInstance,
- NULL);
- ShowWindow(hwnd, nCmdShow);
- 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 lParam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- static HWND hButton, hwndEdit[3];
- char strEdit[80];
- float a[3];
- double root[2];
- int i;
- switch (message) {
- case WM_CREATE:
- hwndEdit[0] = CreateWindow("edit",
- NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,
- 20, 70, 100, 25, hwnd, NULL, NULL, NULL);
- hwndEdit[1] = CreateWindow("edit", NULL,
- WS_CHILD|WS_VISIBLE|WS_BORDER,
- 130, 70, 100, 25, hwnd, NULL, NULL, NULL);
- hwndEdit[2] = CreateWindow("edit", NULL,
- WS_CHILD|WS_VISIBLE|WS_BORDER,
- 240, 70, 100, 25, hwnd, NULL, NULL, NULL);
- hButton = CreateWindow("button", "Calculate",
- WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
- 350, 70, 80, 25, hwnd, NULL, NULL, NULL);
- return 0 ;
- case WM_COMMAND:
- if (((HWND)lParam == hButton )&&( HIWORD(wParam) == BN_CLICKED))
-
- {
-
- for (i=0; i<3; i++) {
- GetWindowText(hwndEdit[i], strEdit, 80);
- a[i] = (float)atof(strEdit);
- }
- int n = GetRoot(a[0], a[1], a[2], root);
- if (n<1) strcpy(str, "方程无根!");
- else sprintf(str,"方程的解为:%f, %f", root[0], root[1]);
- MessageBox(NULL, str, "方程的根", 0);
- }
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- TextOut(hdc, 10, 10, "请输入一元二次方程的系数:", 25);
- TextOut(hdc, 10, 40, "a", 1);
- TextOut(hdc, 120, 40, "b", 1);
- TextOut(hdc, 230, 40, "c", 1);
- TextOut(hdc, 10, 90, str, strlen(str));
- EndPaint(hwnd, &ps);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
-
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
【责任编辑:
云霞 TEL:(010)68476606】