2012-05-20 7 views
0

J'ai une zone de texte sur un formulaire et je veux le régler pour correspondre à la hauteur/largeur de la fenêtre. Y a-t-il un moyen de faire cela en C? Pour être clair, ce n'est pas C-Sharp (C#). J'utilise DevC++Taille de contrôle pour ajuster la taille de la fenêtre

code je dois créer TextBox:

HWND hwndText = CreateWindow(
    TEXT("edit"), 
    TEXT(""), 
    WS_VISIBLE | WS_CHILD | WS_BORDER | WS_HSCROLL | WS_VSCROLL| ES_MULTILINE | ES_AUTOHSCROLL, 
    0,0, 
    630,470, //Height & Width 
    hwnd, 
    (HMENU)ID_MYTEXT, 
    hInstance, 
    NULL 
); 

Source complet:

#include <windows.h> 

const char g_szClassName[] = "myWindowClass"; 

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch(msg) 
    { 
     case WM_CLOSE: 
      DestroyWindow(hwnd); 
     break; 
     case WM_DESTROY: 
      PostQuitMessage(0); 
     break; 
     default: 
      return DefWindowProc(hwnd, msg, wParam, lParam); 
    } 
    return 0; 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow) 
{ 
    WNDCLASSEX wc; 
    HWND hwnd; 
    HMENU ID_MYTEXT; 
    MSG Msg; 

    wc.cbSize  = sizeof(WNDCLASSEX); 
    wc.style   = 0; 
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance  = hInstance; 
    wc.hIcon   = LoadIcon(NULL, IDI_APPLICATION); 
    wc.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = g_szClassName; 
    wc.hIconSm  = LoadIcon(NULL, IDI_APPLICATION); 

    if(!RegisterClassEx(&wc)) 
    { 
     MessageBox(NULL, "Window Registration Failed!", "Error!", 
      MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    } 

    hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE, 
     g_szClassName, 
     "Title", 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 
     NULL, NULL, hInstance, NULL 
    ); 

    HWND hwndText = CreateWindow(
     TEXT("edit"), 
     TEXT(""), 
     WS_VISIBLE | WS_CHILD | WS_BORDER | WS_HSCROLL | WS_VSCROLL| ES_MULTILINE | ES_AUTOHSCROLL, 
     0,0, 
     630,470, 
     hwnd, 
     (HMENU)ID_MYTEXT, 
     hInstance, 
     NULL 
    ); 

    if(hwnd == NULL) 
    { 
     MessageBox(NULL, "Window Creation Failed!", "Error!", 
      MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    } 

    ShowWindow(hwnd, nCmdShow); 
    UpdateWindow(hwnd); 

    SendMessage(hwndText, WM_SETTEXT, 0, (LPARAM)""); 

    while(GetMessage(&Msg, NULL, 0, 0) > 0) 
    { 
     TranslateMessage(&Msg); 
     DispatchMessage(&Msg); 
    } 
    return Msg.wParam; 
} 

Répondre

1

Si cela est tout ce que vous voulez avoir dans votre fenêtre, alors pourquoi ne pas retirer la WS_CHILD style et faites le bouton toute la fenêtre?

L'autre alternative est de gérer WM_SIZE message et de redimensionner la fenêtre de votre bouton enfant.

La version la plus courte de premier exemple ressemble à ceci:

#include "stdafx.h" 

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
    HWND hWnd; 


    hWnd = CreateWindow(_T("BUTTON"), L"testButton", WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 

    if (!hWnd) 
    { 
     return FALSE; 
    } 

    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 

    return TRUE; 
} 

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, 
        _In_opt_ HINSTANCE hPrevInstance, 
        _In_ LPTSTR lpCmdLine, 
        _In_ int  nCmdShow) 
{ 
    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine); 

    // TODO: Place code here. 
    MSG msg; 

    // Perform application initialization: 
    if (!InitInstance (hInstance, nCmdShow)) 
    { 
     return FALSE; 
    } 

    // Main message loop: 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 

    return (int) msg.wParam; 
} 
+0

Comment puis-je faire cela? Je commence juste en C, venant de C#. – sMaciel

+0

Il ne compilera pas comme ça, j'ai mis à jour mon poste avec plus d'informations. – sMaciel

+0

@sMaciel: Il compile, prend un compilateur récent. (Comme Visual C++ 2010 express) - DevC++ est longtemps non maintenu. Au moins, utilisez des blocs de code. – EFraim

Questions connexes