win32控制台中调用win32api和控件资源

来源:互联网 发布:福利彩票不能网络购买 编辑:程序博客网 时间:2024/05/18 23:15

1. 调用对话框资源(使用对话框回调函数)

ex.宽字符串逆序输出

// 1. 添加对话框资源// 2. 添加IDC_EDIT1控件// 3. 添加以下源文件代码#include <iostream>#include <Windows.h>#include "resource.h"#include <algorithm>#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") INT_PTR CALLBACK Dialog1Proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);int main(){std::cout << "程序开始" << std::endl;system("pause");HWND hWnd = CreateDialog(nullptr, MAKEINTRESOURCE(IDD_DIALOG1), nullptr, Dialog1Proc);ShowWindow(hWnd, SW_SHOW);MSG msg = {};while (GetMessage(&msg, nullptr, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}std::cout << "结束" << std::endl;system("pause");return 0;}INT_PTR CALLBACK Dialog1Proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){// 在回调函数中仍可以对控制台输出static TCHAR buffer[256] = {};switch (uMsg){case WM_INITDIALOG:{SetWindowText(hDlg, L"String Reversion");SetDlgItemText(hDlg, IDOK, L"Reverse");SetDlgItemText(hDlg, IDCANCEL, L"Close");SetFocus(GetDlgItem(hDlg, IDC_EDIT1));SetWindowPos(hDlg, nullptr, 450, 250, 0, 0, SWP_NOSIZE);}break;case WM_COMMAND:{switch (LOWORD(wParam)){case IDOK:{GetDlgItemText(hDlg, IDC_EDIT1, buffer, 256);std::reverse(buffer, buffer + wcslen(buffer));SetDlgItemText(hDlg, IDC_EDIT1, buffer);}break;case IDCANCEL:{GetDlgItemText(hDlg, IDC_EDIT1, buffer, 256);SendMessage(hDlg, WM_SYSCOMMAND, SC_CLOSE, 0);return 1;}break;default:break;}}break;case WM_SYSCOMMAND:{if (wParam == SC_CLOSE){DestroyWindow(hDlg);PostQuitMessage(0);// 跳出消息循环return 1;}}break;default:break;}return 0;}

2. 同理,可以建立窗口循环,只要把hInstance的地方都换成nullptr即可
ex.Hello World程序
#include <Windows.h>#include <cmath>LRESULT CALLBACK WindowProc(   HWND hwnd,   UINT uMsg,   WPARAM wParam,   LPARAM lParam );//程序入口点int main(){//1. 设计窗口类。注意"You must fill the structure with the appropriate class attributes before passing it to the function."也就是说结构体的每一个成员不管是否用到,都要赋值。WNDCLASS wc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hCursor = LoadCursor(nullptr, IDC_ARROW);;  wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);;  wc.lpszMenuName = NULL;//NULL则没有菜单wc.style = CS_HREDRAW | CS_VREDRAW;//窗口的类样式,不是窗口样式WS_XXX,这里是水平重绘和垂直重绘wc.hbrBackground = (HBRUSH)COLOR_WINDOW;wc.lpfnWndProc = WindowProc;//设置用哪个函数处理消息  wc.lpszClassName = L"MyClass";  wc.hInstance = nullptr;//2. 注册窗口类。这样系统才能知道这个窗口类的存在。RegisterClass(&wc);//3. 创建窗口HWND hwnd = CreateWindow(L"MyClass",//必须和注册的类名一致L"WinForm",WS_OVERLAPPEDWINDOW,50,//窗口相对父级的x坐标50,//窗口相对父级的y坐标1000,//窗口宽度800,//窗口高度NULL,//没有父窗口NULL,//没有菜单nullptr,//当前应用程序的实例句柄NULL//没有附加数据);//4. 显示窗口ShowWindow(hwnd, SW_SHOW);//*5. 更新窗口UpdateWindow(hwnd);//6. 消息循环MSG msg;while(GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return 0;}LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){switch(uMsg){case WM_DESTROY:{PostQuitMessage(0);return 0;}case WM_PAINT:{PAINTSTRUCT ps;HDC hdc = BeginPaint(hwnd, &ps);SelectObject(ps.hdc, (HPEN)GetStockObject(NULL_PEN));HBRUSH hbrush = CreateSolidBrush(RGB(255, 125, 12));SelectObject(hdc, hbrush);POINT pts[3] = {{0, 0}, {500, 0}, {250, 250 * sqrt(3.0)}};Polygon(hdc, pts, 3);SetBkMode(hdc, TRANSPARENT);TextOut(hdc, 220, 125 * sqrt(3) - 40, L"Hello World",wcslen(L"Hello World"));EndPaint(hwnd, &ps);break;}default:break;}return DefWindowProc(hwnd, uMsg, wParam, lParam);}


0 0
原创粉丝点击