C++ 如何在C++;Win32应用程序
我试过这个:C++ 如何在C++;Win32应用程序,c++,winapi,C++,Winapi,我试过这个: #include <windows.h> #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") LRESULT CALLBACK
#include <windows.h>
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow)
{
LPTSTR windowClass = TEXT("WinApp");
LPTSTR windowTitle = TEXT("Windows Application");
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = windowClass;
wcex.lpszMenuName = NULL;
wcex.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWnd;
if (!(hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL)))
{
MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR);
return EXIT_FAILURE;
}
HWND hWndEdit = CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return EXIT_SUCCESS;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(EXIT_SUCCESS);
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return FALSE;
}
创建一个文本框,它只是一个丑陋的黑色实心边框文本框
如何创建windows样式的文本框(带有3D浅蓝色边框)?使用
CreateWindow
而不是使用CreateWindowEx
并指定WS\u EX\u CLIENTEDGE
作为第一个参数
您可以使用Visual Studio附带的Spy++工具将创建的编辑控件的样式与常用控件的样式进行比较(例如,当您在资源管理器中的文件上显示“属性”时)。在代码块中,将清单文件放在项目附近: 程序_name.exe.manifest 要编程_name.exe.manifest,请编写以下命令:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="CompanyName.ProductName.YourApplication"
type="win32"
/>
<description>Program name</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
程序名
然后你的程序看起来像这样:
OP编辑了他的问题,删除了原始代码,从而使答案无效,他说: 这是一个使用文本框创建窗口的简单示例 我将其回滚以恢复原始代码,但我认为该工作示例很好,因此如下所示: #include <windows.h> #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow) { LPTSTR windowClass = TEXT("WinApp"); LPTSTR windowTitle = TEXT("Windows Application"); WNDCLASSEX wcex; wcex.cbClsExtra = 0; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbWndExtra = 0; wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wcex.hInstance = hInstance; wcex.lpfnWndProc = WndProc; wcex.lpszClassName = windowClass; wcex.lpszMenuName = NULL; wcex.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClassEx(&wcex)) { MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR); return EXIT_FAILURE; } HWND hWnd; if (!(hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL))) { MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR); return EXIT_FAILURE; } HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE, 100, 20, 140, 20, hWnd, NULL, NULL, NULL); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return EXIT_SUCCESS; } LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(EXIT_SUCCESS); default: return DefWindowProc(hWnd, msg, wParam, lParam); } return FALSE; } #包括 #pragma注释(链接器,“/manifestdependency:\”type='win32'name='Microsoft.Windows.Common Controls'version='6.0.0.0'processorArchitecture='x86'publicKeyToken='6595b64144ccf1df'language='*'\“”) LRESULT回调WndProc(HWND HWND、UINT msg、WPARAM WPARAM、LPARAM LPARAM); int APIENTRY WinMain(HINSTANCE HINSTANCE、HINSTANCE hPrevInstance、, LPSTR nCmdLine,int nCmdShow) { LPTSTR windowClass=文本(“WinApp”); LPTSTR windowTitle=文本(“Windows应用程序”); WNDCLASSEX wcex; wcex.cbClsExtra=0; wcex.cbSize=sizeof(WNDCLASSEX); wcex.cbWndExtra=0; wcex.hbrBackground=(HBRUSH)(彩色窗口+1); wcex.hCursor=LoadCursor(空,IDC_箭头); wcex.hIcon=LoadIcon(空,IDI_应用程序); wcex.hIconSm=LoadIcon(空,IDI_应用程序); wcex.hInstance=hInstance; wcex.lpfnWndProc=WndProc; wcex.lpszClassName=windowClass; wcex.lpszMenuName=NULL; wcex.style=CS_HREDRAW | CS_VREDRAW; 如果(!RegisterClassEx(&wcex)) { MessageBox(NULL,文本(“RegisterClassEx失败!”),文本(“错误”), MB_(错误); 返回退出失败; } HWND-HWND; 如果(!(hWnd=CreateWindow(windowClass、windowTitle、WS_OVERLAPPEDWINDOW、, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, CW_usefault,NULL,NULL,hInstance,NULL))) { MessageBox(NULL,文本(“CreateWindow失败!”),文本(“错误”),MB_ICONERROR); 返回退出失败; } HWND hWndEdit=CreateWindowEx(WS_EX_CLIENTEDGE,文本(“编辑”),文本(“测试”), WS|u CHILD | WS|u VISIBLE,100,20,140, 20,hWnd,空,空,空); 显示窗口(hWnd、nCmdShow); 更新窗口(hWnd); 味精; while(GetMessage(&msg,NULL,0,0)) { 翻译信息(&msg); 发送消息(&msg); } 返回退出成功; } LRESULT回调WndProc(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM) { 开关(msg) { 案例WM_销毁: 退出消息(退出成功); 违约: 返回DefWindowProc(hWnd、msg、wParam、lParam); } 返回FALSE; } #include <windows.h> #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow) { LPTSTR windowClass = TEXT("WinApp"); LPTSTR windowTitle = TEXT("Windows Application"); WNDCLASSEX wcex; wcex.cbClsExtra = 0; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbWndExtra = 0; wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wcex.hInstance = hInstance; wcex.lpfnWndProc = WndProc; wcex.lpszClassName = windowClass; wcex.lpszMenuName = NULL; wcex.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClassEx(&wcex)) { MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR); return EXIT_FAILURE; } HWND hWnd; if (!(hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL))) { MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR); return EXIT_FAILURE; } HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE, 100, 20, 140, 20, hWnd, NULL, NULL, NULL); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return EXIT_SUCCESS; } LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(EXIT_SUCCESS); default: return DefWindowProc(hWnd, msg, wParam, lParam); } return FALSE; }