2011-09-29 206 views
7

我想这创建一个Windows风格的文本框:如何在C++ Win32应用程序

#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; 
} 

我用:

CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL); 

创建一个文本框,它只是一个丑陋的黑色 - 固体边框文本框。

如何创建一个Windows风格的文本框(与3D浅蓝色边框)?

+1

http://msdn.microsoft.com/en-us/库/窗/台式机/ bb773175%28V = vs.85%29.aspx –

回答

13

而不是使用CreateWindow,请使用CreateWindowEx并指定WS_EX_CLIENTEDGE作为第一个参数。

使用Visual Studio附带的Spy ++工具,您可以将创建的编辑控件的样式与库存样式进行比较(例如,当您在资源管理器中的文件中显示“属性”时)。

0

在代码块放在靠近项目清单文件: program_name.exe.manifest

screenshot

而要program_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> 

然后你的程序看起来像这个:

Screenshot

1

的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; 
} 
相关问题