日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于 Win32 的应用程序

發布時間:2023/12/18 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于 Win32 的应用程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文純屬記錄http://msdn.microsoft.com/zh-cn/library/bb384843.aspx

創建基于 Win32 的項目

  • “文件”菜單上,單擊“新建”,然后單擊“項目”。

  • “新建項目”對話框的左側窗格中,單擊“已安裝的模板”,單擊“Visual C++”,然后選擇“Win32”?在中間窗格中,選擇“Win32 項目”

    “名稱”框中,鍵入項目名稱,例如?win32app。?單擊“確定”。

  • “Win32 應用程序向導”的“歡迎”頁上,單擊“下一步”。

  • 在“應用程序設置”頁上的在“應用程序類型”下,選擇“Windows 應用程序”?“附加選項”下,選擇“空項目”。?單擊“完成”創建項目。

  • “解決方案資源管理器”中,右擊 Win32app 項目,單擊“添加”,然后單擊“新建項”?“添加新項”對話框中選擇“C++ 文件(.cpp)”。?“名稱”框中,鍵入文件名稱,例如GT_HelloWorldWin32.cpp。?單擊“添加”。

  • 啟動基于 Win32 的應用程序

  • 就像每個 C 應用程序和 C++ 應用程序都以?main?函數作為起點那樣,每個基于 Win32 的應用程序同樣必須要有?WinMain?函數。?WinMain?具有以下語法。

    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow);

    有關此函數的參數和返回值的信息,請參見?WinMain 函數。

  • 由于應用程序代碼必須使用現有定義,因此應將 include 語句添加到文件中。

    #include <windows.h> #include <stdlib.h> #include <string.h> #include <tchar.h>
  • 除?WinMain?函數外,每個基于 Win32 的應用程序還必須具有一個窗口過程函數。?此函數通常名為?WndProc?WndProc?具有以下語法。

    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

    此函數處理應用程序從操作系統接收的許多消息。?例如,在具有對話框(該對話框中有一個“確定”按鈕)的應用程序中,如果用戶單擊該按鈕,操作系統就會向該應用程序發送一條消息,告知已單擊該按鈕。?WndProc?負責響應該事件。?在此示例中,相應的響應可能是關閉該對話框。

    有關更多信息,請參見窗口過程。

  • 向 WinMain 函數添加功能

  • 在?WinMain?函數中,創建?WNDCLASSEX?類型的窗口類結構。?此結構包含有關該窗口的信息,例如,應用程序圖標、窗口的背景色、要在標題欄中顯示的名稱、窗口過程函數的名稱等等。?下面的示例演示一個典型?WNDCLASSEX?結構。

    WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = NULL;wcex.lpszClassName = szWindowClass;wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    有關此結構的各字段的信息,請參見?WNDCLASSEX。

  • 現在您已經創建一個窗口類,接下來必須將其注冊。?使用?RegisterClassEx?函數,并將窗口類結構作為參數進行傳遞。

    if (!RegisterClassEx(&wcex)){MessageBox(NULL,_T("Call to RegisterClassEx failed!"),_T("Win32 Guided Tour"),NULL);return 1;}
  • 現在可以創建一個窗口。?使用?CreateWindow?函數。

    static TCHAR szWindowClass[] = _T("win32app"); static TCHAR szTitle[] = _T("Win32 Guided Tour Application");// The parameters to CreateWindow explained: // szWindowClass: the name of the application // szTitle: the text that appears in the title bar // WS_OVERLAPPEDWINDOW: the type of window to create // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y) // 500, 100: initial size (width, length) // NULL: the parent of this window // NULL: this application does not have a menu bar // hInstance: the first parameter from WinMain // NULL: not used in this application HWND hWnd = CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,500, 100,NULL,NULL,hInstance,NULL ); if (!hWnd) {MessageBox(NULL,_T("Call to CreateWindow failed!"),_T("Win32 Guided Tour"),NULL);return 1; }

    此函數返回 HWND,它是某個窗口的句柄。?有關更多信息,請參見?Windows 數據類型。

  • 現在,使用下列代碼來顯示窗口。

    // The parameters to ShowWindow explained: // hWnd: the value returned from CreateWindow // nCmdShow: the fourth parameter from WinMain ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd);

    此時,所顯示的窗口不會有太多內容,因為您尚未實現?WndProc?函數。

  • 現在添加一個消息循環以偵聽操作系統發送的消息。?如果應用程序收到一條消息,則此循環會將該消息調度至?WndProc?函數以接受處理。?消息循環類似于下列代碼。

    MSG msg;while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return (int) msg.wParam;

    有關消息循環中各結構和函數的更多信息,請參見?MSG、GetMessage、TranslateMessage?和?DispatchMessage。

    此時,WinMain?函數應與下列代碼類似。

    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = NULL;wcex.lpszClassName = szWindowClass;wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));if (!RegisterClassEx(&wcex)){MessageBox(NULL,_T("Call to RegisterClassEx failed!"),_T("Win32 Guided Tour"),NULL);return 1;}hInst = hInstance; // Store instance handle in our global variable// The parameters to CreateWindow explained:// szWindowClass: the name of the application// szTitle: the text that appears in the title bar// WS_OVERLAPPEDWINDOW: the type of window to create// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)// 500, 100: initial size (width, length)// NULL: the parent of this window// NULL: this application dows not have a menu bar// hInstance: the first parameter from WinMain// NULL: not used in this applicationHWND hWnd = CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,500, 100,NULL,NULL,hInstance,NULL);if (!hWnd){MessageBox(NULL,_T("Call to CreateWindow failed!"),_T("Win32 Guided Tour"),NULL);return 1;}// The parameters to ShowWindow explained:// hWnd: the value returned from CreateWindow// nCmdShow: the fourth parameter from WinMainShowWindow(hWnd,nCmdShow);UpdateWindow(hWnd);// Main message loop:MSG msg;while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return (int) msg.wParam; }
  • 向 WndProc 函數添加功能

  • 若要啟用?WndProc?函數來處理應用程序所收到的消息,請實現 switch 語句。

    要處理的第一條消息是?WM_PAINT?消息。?如果必須更新所顯示的應用程序窗口的一部分,該應用程序就會收到此消息。?(首次顯示該窗口時,必須將其全部更新。)

    若要處理?WM_PAINT?消息,請首先調用?BeginPaint,然后處理用于布局該窗口中的文本、按鈕和其他控件的所有邏輯,再調用?EndPaint。?對于此應用程序,開始調用和結束調用之間的邏輯會在窗口中顯示字符串“Hello, World!”。?在下列代碼中,請注意?TextOut?函數用于顯示該字符串。

    PAINTSTRUCT ps; HDC hdc; TCHAR greeting[] = _T("Hello, World!");switch (message) { case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// Here your application is laid out.// For this introduction, we just print out "Hello, World!"// in the top left corner.TextOut(hdc,5, 5,greeting, _tcslen(greeting));// End application-specific layout section.EndPaint(hWnd, &ps);break; }
  • 應用程序通常會處理許多其他消息,例如?WM_CREATE?和?WM_DESTROY。?下列代碼展示了一個基本但完整的?WndProc?函數。

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {PAINTSTRUCT ps;HDC hdc;TCHAR greeting[] = _T("Hello, World!");switch (message){case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// Here your application is laid out.// For this introduction, we just print out "Hello, World!"// in the top left corner.TextOut(hdc,5, 5,greeting, _tcslen(greeting));// End application specific layout section.EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);break;}return 0; }
  • 示例

    生成此示例

  • 創建本演練中之前“創建基于 Win32 的項目”中的基于 Win32 的項目。

  • 復制這些步驟之后的代碼,然后將其粘貼到 GT_HelloWorldWin32.cpp 源文件中。

  • “生成”菜單上,單擊“生成解決方案”。

  • 若要運行該應用程序,請按 F5。?在顯示屏的左上角應出現一個窗口,窗口中含有文本“Hello World!”。

  • 代碼

    // GT_HelloWorldWin32.cpp // compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c#include <windows.h> #include <stdlib.h> #include <string.h> #include <tchar.h>// Global variables// The main window class name. static TCHAR szWindowClass[] = _T("win32app");// The string that appears in the application's title bar. static TCHAR szTitle[] = _T("Win32 Guided Tour Application");HINSTANCE hInst;// Forward declarations of functions included in this code module: LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = NULL;wcex.lpszClassName = szWindowClass;wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));if (!RegisterClassEx(&wcex)){MessageBox(NULL,_T("Call to RegisterClassEx failed!"),_T("Win32 Guided Tour"),NULL);return 1;}hInst = hInstance; // Store instance handle in our global variable// The parameters to CreateWindow explained:// szWindowClass: the name of the application// szTitle: the text that appears in the title bar// WS_OVERLAPPEDWINDOW: the type of window to create// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)// 500, 100: initial size (width, length)// NULL: the parent of this window// NULL: this application does not have a menu bar// hInstance: the first parameter from WinMain// NULL: not used in this applicationHWND hWnd = CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,500, 100,NULL,NULL,hInstance,NULL);if (!hWnd){MessageBox(NULL,_T("Call to CreateWindow failed!"),_T("Win32 Guided Tour"),NULL);return 1;}

    總結

    以上是生活随笔為你收集整理的基于 Win32 的应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。