User Tools

Site Tools


prog:dev_cpp:250722-01:index

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
prog:dev_cpp:250722-01:index [2025/07/22 17:17]
jethro
prog:dev_cpp:250722-01:index [2025/07/24 09:59] (current)
jethro [Local Backup]
Line 74: Line 74:
  return msg.wParam;  return msg.wParam;
 }</​sxh>​ }</​sxh>​
-  * 在此基础上,​ 随便找些WINAPI函数插进源码框架,​ 正好来练一练:<​sxh c++>#​include ​&lt;cstring&gt; +  * 在此基础上,​ 随便找些WINAPI函数插进源码框架,​ 正好来练一练:<​sxh c++>#​include ​<​windows.h>​ 
-#​include ​&lt;string&gt;+#include <cstring> 
 +#​include ​<string>
 using namespace std; using namespace std;
- +  ​
 LRESULT CALLBACK WndProc(HWND,​ UINT, WPARAM, LPARAM); LRESULT CALLBACK WndProc(HWND,​ UINT, WPARAM, LPARAM);
- +  ​
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,​ LPSTR lpCmdLine, int nCmdShow) { int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,​ LPSTR lpCmdLine, int nCmdShow) {
- WNDCLASSEX wc; +    ​WNDCLASSEX wc; 
- HWND hwnd, hwndDT; +    HWND hwnd, hwndDT; 
- MSG msg; +    MSG msg; 
- RECT rect; +    RECT rect; 
- int dtWidth,​dtHeight;​ +    int dtWidth,​dtHeight;​ 
-  +   
- memset(&​wc,​0,​sizeof(wc));​ +    memset(&​wc,​0,​sizeof(wc));​ 
- wc.cbSize  = sizeof(WNDCLASSEX);​ +    wc.cbSize ​       = sizeof(WNDCLASSEX);​ 
- wc.lpfnWndProc  = WndProc; +    wc.lpfnWndProc ​  ​= WndProc; 
- wc.hInstance  = hInstance;​ +    wc.hInstance ​    ​= hInstance;​ 
- wc.hCursor  = LoadCursor(NULL,​ IDC_ARROW);​ +    wc.hCursor ​      ​= LoadCursor(NULL,​ IDC_ARROW);​ 
-  +     ​ 
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);​ +    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);​ 
- wc.lpszClassName = "​WindowClass";​ +    wc.lpszClassName = "​WindowClass";​ 
- wc.hIcon  = LoadIcon(NULL,​ IDI_APPLICATION);​ +    wc.hIcon ​        ​= LoadIcon(NULL,​ IDI_APPLICATION);​ 
- wc.hIconSm  = LoadIcon(NULL,​ IDI_APPLICATION);​ +    wc.hIconSm ​      ​= LoadIcon(NULL,​ IDI_APPLICATION);​ 
-  +   
- if(!RegisterClassEx(&​wc)) { +    if(!RegisterClassEx(&​wc)) { 
- MessageBox(NULL,​ "​Window Registration Failed!","​Error!",​MB_ICONEXCLAMATION|MB_OK);​ +        MessageBox(NULL,​ "​Window Registration Failed!","​Error!",​MB_ICONEXCLAMATION|MB_OK);​ 
- return 0; +        return 0; 
-+    
-  +   
- hwndDT=GetDesktopWindow();​ //​取桌面句柄  +    hwndDT=GetDesktopWindow();​ //​取桌面句柄  
- GetWindowRect(hwndDT,&​rect);​ //​取桌面范围  +    GetWindowRect(hwndDT,&​rect);​ //​取桌面范围  
- dtWidth=rect.right-rect.left;​ //​桌面宽度  +    dtWidth=rect.right-rect.left;​ //​桌面宽度  
- dtHeight=rect.bottom-rect.top;​ //​桌面高度 ​ +    dtHeight=rect.bottom-rect.top;​ //​桌面高度 ​
-  +
- hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,​wc.lpszClassName,​ +
- "​First Windows Appliction",​WS_VISIBLE|WS_OVERLAPPEDWINDOW,​ +
- (dtWidth-640)/​2, ​  /​*窗体居中*/​  +
- (dtHeight-480)/​2,​ +
- 640, +
- 480, +
- NULL,​NULL,​hInstance,​NULL);​ +
- //​MoveWindow(hwnd,​ (width-640)/​2,​ (height-480)/​2,​ 640, 480, FALSE);+
            
- if(hwnd == NULL) { +    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,​wc.lpszClassName,​ 
- MessageBox(NULL,​ "​Window Creation Failed!","​Error!",​ MB_ICONEXCLAMATION|MB_OK);​ +        "First Windows Appliction",​WS_VISIBLE|WS_OVERLAPPEDWINDOW,​ 
- return 0; +        (dtWidth-640)/​2, ​  /​*窗体居中*/​  
-+        (dtHeight-480)/​2,​ 
-  +        640, 
- while(GetMessage(&​msg,​ NULL, 0, 0) &gt; 0) {  +        480, 
- TranslateMessage(&​msg);​ +        NULL,​NULL,​hInstance,​NULL);​ 
- DispatchMessage(&​msg);​ +    //​MoveWindow(hwnd,​ (width-640)/​2,​ (height-480)/​2,​ 640, 480, FALSE); 
-+       
-  +    ​if(hwnd == NULL) { 
- return msg.wParam;+        MessageBox(NULL,​ "​Window Creation Failed!","​Error!",​ MB_ICONEXCLAMATION|MB_OK);​ 
 +        return 0; 
 +    
 +   
 +    while(GetMessage(&​msg,​ NULL, 0, 0)) {  
 +        TranslateMessage(&​msg);​ 
 +        DispatchMessage(&​msg);​ 
 +    
 +   
 +    return msg.wParam;
 } }
- +  ​
 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 { {
- HDC         hdc; +    ​HDC             ​hdc; 
- PAINTSTRUCT ​ ps; +    PAINTSTRUCT ​    ​ps; 
- RECT rect; +    RECT            rect; 
- POINT mouse; +    POINT           ​mouse; 
- static int   cxChar, cyChar; +    static int      cxChar, cyChar; 
- static int   mX, mY; +    static int      mX, mY; 
- static HWND  hwndButton;​ +    static HWND     ​hwndButton;​ 
- static HWND  hwndEditbox;​ +    static HWND     ​hwndEditbox;​ 
- string strXy; +    string ​         strXy; 
- char x[5], y[5]; +    char            x[5], y[5]; 
- char buff[4096] = {0}; +    char            buff[4096] = {0}; 
- const int IDcmdButton = 1; +    const int       ​IDcmdButton = 1; 
- const int IDeditBox = 2+    const int       ​IDeditBox = 2;
-  +
- cxChar = LOWORD(GetDialogBaseUnits());​ +
- cyChar = HIWORD(GetDialogBaseUnits());+
            
- switch (message) { +    cxChar = LOWORD(GetDialogBaseUnits());​ 
- case WM_CREATE:​ +    cyChar = HIWORD(GetDialogBaseUnits());​ 
- hwndButton = CreateWindow ( TEXT("​button"​),​ TEXT("​Command Button"​),​ +       
-                             WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,​ +    ​switch (message) { 
-                                   cxChar * 6, cyChar * 15, +        case WM_CREATE:​ 
-                                   30 * cxChar, 2.5 * cyChar, +            hwndButton = CreateWindow ( TEXT("​button"​),​ TEXT("​Command Button"​),​ 
-                                   hwnd, (HMENU)IDcmdButton,​ ((LPCREATESTRUCT) lParam)-&gt;hInstance, NULL); +                                        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,​ 
- if (!hwndButton) MessageBox(NULL,"​创建按钮失败","​Message",​MB_OK|MB_ICONERROR);​ +                                        cxChar * 6, cyChar * 15, 
- ShowWindow(hwndButton,​SW_SHOW);​+                                        30 * cxChar, 2.5 * cyChar, 
 +                                        hwnd, (HMENU)IDcmdButton,​ ((LPCREATESTRUCT) lParam)->hInstance, NULL); 
 +            if (!hwndButton) MessageBox(NULL,"​创建按钮失败","​Message",​MB_OK|MB_ICONERROR);​ 
 +            ShowWindow(hwndButton,​SW_SHOW);​
             UpdateWindow(hwndButton);​             UpdateWindow(hwndButton);​
- +                 
             hwndEditbox = CreateWindow( TEXT("​edit"​),​NULL,​             hwndEditbox = CreateWindow( TEXT("​edit"​),​NULL,​
- WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,​ +                                        ​WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,​ 
-                                   cxChar * 31, cyChar * 0.5, +                                        cxChar * 31, cyChar * 0.5, 
-                                   30 * cxChar, 9.5 * cyChar, +                                        30 * cxChar, 9.5 * cyChar, 
- hwnd,​(HMENU)IDeditBox,​NULL,​NULL);​ +                                        hwnd,​(HMENU)IDeditBox,​NULL,​NULL);​ 
-  +                                         ​ 
- if (!hwndEditbox) MessageBox(NULL,"​创建文本框失败","​Message",​MB_OK|MB_ICONERROR);​ +            if (!hwndEditbox) MessageBox(NULL,"​创建文本框失败","​Message",​MB_OK|MB_ICONERROR);​ 
- ShowWindow(hwndEditbox,​SW_SHOW);​+            ShowWindow(hwndEditbox,​SW_SHOW);​
             UpdateWindow(hwndEditbox);​             UpdateWindow(hwndEditbox);​
-        return 0 ; +            ​return 0 ; 
-           ​ +               ​ 
-     ​case WM_PAINT: +        case WM_PAINT: 
- hdc = BeginPaint (hwnd,&​ps);​ +            hdc = BeginPaint (hwnd,&​ps);​ 
-     SetRect(&​rect,​ 10, 10, 300, 200); +            SetRect(&​rect,​ 10, 10, 300, 200); 
-    ​  ​   ​FrameRect(hdc,&​rect,​ (HBRUSH)GetStockObject(BLACK_BRUSH));​ +            FrameRect(hdc,&​rect,​ (HBRUSH)GetStockObject(BLACK_BRUSH));​ 
- GetClientRect (hwnd,&​rect);​ +            GetClientRect (hwnd,&​rect);​ 
- rect.left += 20;+            rect.left += 20;
             rect.top += 2;             rect.top += 2;
             //​SetTextColor(hdc,​ RGB(255,​0,​0));​ //​可以用RGB三原色设置文本颜色             //​SetTextColor(hdc,​ RGB(255,​0,​0));​ //​可以用RGB三原色设置文本颜色
- DrawText(hdc,​ TEXT(" Hello, Dev-C++! "), -1,&​rect,​ DT_SINGLELINE | DT_TOP | DT_LEFT); +            ​DrawText(hdc,​ TEXT(" Hello, Dev-C++! "), -1,&​rect,​ DT_SINGLELINE | DT_TOP | DT_LEFT); 
- EndPaint(hwnd,&​ps);​ +            EndPaint(hwnd,&​ps);​ 
- return 0;+            ​return 0; 
 +   
 +        case WM_SIZE: 
 +            //​GetWindowRect(hwnd,&​rect);​ 
 +            //​MoveWindow(hwndButton,​ (rect.right-rect.left)/​2 - 15*cxChar ,\ 
 +            //​(rect.bottom-rect.top)/​2 - 1.25*cxChar,​ 30*cxChar, 2.5*cyChar, FALSE); 
 +            return 0; 
 +              
 +        case WM_COMMAND:​ 
 +            //​各控件的_click()事件  
 +            switch (LOWORD(wParam)) { 
 +            case 0: 
 +                PostQuitMessage(0);​ 
 +                break; 
 +            case IDcmdButton:​  
 +                GetWindowText(hwndEditbox,​buff,​100);​ 
 +                if (buff[0]) 
 +                    MessageBox(NULL,​ buff, "​ComandButton_Click()",​ MB_OK); 
 +                else 
 +                    MessageBox(NULL,​ "​激发ComandButton_Click()事件",​ "​命令按钮",​ MB_ICONASTERISK);​ 
 +                break; 
 +            case IDeditBox:​ 
 +                GetWindowText(hwndEditbox,​buff,​4096);​ 
 +                break; 
 +            } 
 +            ​return 0; 
    
-     case WM_SIZE: +        ​case WM_LBUTTONDOWN:​ // WM_LBUTTONDOWN是鼠标左键按下的事件 
- //​GetWindowRect(hwnd,&​rect);​ +            GetCursorPos(&​mouse);​ 
- //​MoveWindow(hwndButton,​ (rect.right-rect.left)/​2 - 15*cxChar ,\ +            GetWindowRect(hwnd,&​rect);​ 
- //​(rect.bottom-rect.top)/​2 - 1.25*cxChar,​ 30*cxChar, 2.5*cyChar, FALSE); +            mX=mouse.x-rect.left;​ 
- return 0; +            mY=mouse.y-rect.top;​ 
-  +            itoa(mX,​x,​10);​ 
- case WM_COMMAND:​ +            itoa(mY,​y,​10);​ 
- //​各控件的_click()事件  +            strXy="​鼠标点击的窗体内坐标:​ ("​+string(x)+","​+string(y)+"​)";​ 
- switch (LOWORD(wParam)) { +            SetWindowText(hwndEditbox,​strXy.c_str());​ 
- case 0: +            //​MessageBox(NULL,​ strXy.c_str(),​ "",​ MB_ICONASTERISK);​ 
- PostQuitMessage(0);​ +            return 0; 
- break;​ +  
- case IDcmdButton:​  +        case WM_CLOSE: 
- GetWindowText(hwndEditbox,​buff,​100);​ +            if (IDYES==MessageBox(hwnd,​ "​是否真的要退出&#​xff1f;",​ "​确认",​ MB_ICONQUESTION | MB_YESNO)) 
- if (buff[0]) +                DestroyWindow(hwnd); ​ //​销毁窗口 
- MessageBox(NULL,​ buff, "​ComandButton_Click()",​ MB_OK); +            return 0; 
- else +           ​ 
- MessageBox(NULL,​ "​激发ComandButton_Click()事件",​ "​命令按钮",​ MB_ICONASTERISK);​ +        case WM_DESTROY:​ 
- break;​ +            ShellAbout(hwnd,​ "​第一个窗口程序",​ "​再见,期待您在评论区留言!",​ NULL); 
- case IDeditBox:​ +            PostQuitMessage(0);​ 
- GetWindowText(hwndEditbox,​buff,​4096);​ +            return 0; 
- break;​ +    
-+     ​ 
- return 0;  +    return DefWindowProc(hwnd,​ message, wParam, lParam);
- +
- case WM_LBUTTONDOWN:​ // WM_LBUTTONDOWN是鼠标左键按下的事件 +
- GetCursorPos(&​mouse);​ +
- GetWindowRect(hwnd,&​rect);​ +
- mX=mouse.x-rect.left;​ +
- mY=mouse.y-rect.top;​ +
- itoa(mX,​x,​10);​ +
- itoa(mY,​y,​10);​ +
- strXy="​鼠标点击的窗体内坐标:​ ("​+string(x)+","​+string(y)+"​)";​ +
- SetWindowText(hwndEditbox,​strXy.c_str());​ +
- //​MessageBox(NULL,​ strXy.c_str(),​ "",​ MB_ICONASTERISK);​ +
- return 0; +
- +
- case WM_CLOSE: +
- if (IDYES==MessageBox(hwnd,​ "​是否真的要退出&#​xff1f;",​ "​确认",​ MB_ICONQUESTION | MB_YESNO)) +
- DestroyWindow(hwnd); ​ //​销毁窗口 +
- return 0; +
-   ​ +
- case WM_DESTROY:​ +
- ShellAbout(hwnd,​ "​第一个窗口程序",​ "​再见,期待您在评论区留言!",​ NULL); +
- PostQuitMessage(0);​ +
- return 0; +
-+
-  +
- return DefWindowProc(hwnd,​ message, wParam, lParam);+
 }</​sxh>​ }</​sxh>​
   * 运行效果见文首图片,现在有了BUTTON和EDIT两种控件,要把这个程序改造成一个简单的计算器程序应该很轻松的,你可以自己试试看 (本文完) ​   * 运行效果见文首图片,现在有了BUTTON和EDIT两种控件,要把这个程序改造成一个简单的计算器程序应该很轻松的,你可以自己试试看 (本文完) ​
prog/dev_cpp/250722-01/index.txt · Last modified: 2025/07/24 09:59 by jethro