如何在C ++ Win32应用程序中创建Windows样式文本框 [英] How to create a Windows style textbox in a C++ Win32 application
问题描述
我试过这个:
#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);
默认值:
return DefWindowProc(hWnd,msg,wParam,lParam);
}
return FALSE;
}
这是一个创建带有文本框的窗口的简单示例。
我使用:
CreateWindow(TEXT(Edit),TEXT test),WS_CHILD | WS_VISIBLE | WS_BORDER,100,20,140,20,hWnd,NULL,NULL,NULL);
创建一个文本框,它只是一个丑陋的黑色实心边框文本框。
如何创建窗口样式文本框(带有浅蓝色边框)?
p>
您可以将创建的编辑控件的样式与库存编辑控件的样式进行比较(例如,您可以使用CreateWindowEx和指定WS_EX_CLIENTEDGE作为第一个参数。当您使用Visual Studio中的Spy ++工具显示资源管理器中的文件上的属性时)。
I tried this:
#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;
}
This is a simple example to create a window with a textbox.
I use:
CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL);
to create a textbox, and it's just an ugly black-solid-border textbox.
How to create a windows style textbox (with a 3D light blue border)?
Instead of using CreateWindow, use CreateWindowEx and specify WS_EX_CLIENTEDGE as the first parameter.
You can compare the styles of your created edit control with a stock one (for example, when you show 'Properties' on a file in the explorer) with the Spy++ tool that comes along Visual Studio.
这篇关于如何在C ++ Win32应用程序中创建Windows样式文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!