2011-09-29 35 views
7

He intentado esto:Cómo crear un cuadro de texto al estilo de Windows en una aplicación de 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; 
} 

que utilizo:

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

para crear un cuadro de texto, y es sólo un negro-fea cuadro de texto de borde sólido.

Cómo crear un cuadro de texto de estilo de Windows (con un borde azul claro en 3D)?

+1

http://msdn.microsoft.com/en-us/ biblioteca/windows/desktop/bb773175% 28v = vs.85% 29.aspx –

Respuesta

13

En lugar de usar CreateWindow, use CreateWindowEx y especifique WS_EX_CLIENTEDGE como primer parámetro.

Puede comparar los estilos de su control de edición creado con uno de stock (por ejemplo, cuando muestra 'Propiedades' en un archivo en el explorador) con la herramienta Spy ++ que viene con Visual Studio.

0

En Codeblocks poner archivo de manifiesto cerca del proyecto: program_name.exe.manifest

screenshot

Y para escribir este 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> 

Y entonces el programa parezca esto:

Screenshot

1

El PO editado su pregunta, eliminando el código original y por lo tanto invalidar las respuestas, diciendo:

Este es un ejemplo sencillo para crear una ventana con un cuadro de texto.

rodé de nuevo para restaurar el código original, pero me pareció que el ejemplo de trabajo era agradable, así que aquí está:

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