Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

underline_bruce

Win32 API

Recommended Posts

Aew galera...

 

Tava dando uma lida no PDF "Programming Windows" de Charles Petzold, e tive uma duvida. Num certo algoritmo ele faz uma tela com um "Push Button" e varios outros componentes (edit's, check box's, radio button's e etc...), só que eu não entendi onde cada componete grafico é declarado e configurado.

 

Por exemplo, como faço para inserir um button ("push button") na janela? E como faço para alterar o "left", "top", "height" e "width" (coordenadas x, y, comprimento e largura)?

 

O algoritmo é esse ai, compilei no MS - Visual C++ e correu tudo bem!

 

#include <windows.h>

 

struct

{

int iStyle;

TCHAR * szText;

}

button[] =

{

BS_PUSHBUTTON, TEXT ("AperteButton"),

BS_DEFPUSHBUTTON, TEXT ("DefPushButton"),

BS_CHECKBOX, TEXT ("CheckBox"),

BS_AUTOCHECKBOX, TEXT ("AutoCheckBox"),

BS_RADIOBUTTON, TEXT ("RadioButton"),

BS_3STATE, TEXT ("3State"),

BS_AUTO3STATE, TEXT ("Auto3State"),

BS_GROUPBOX, TEXT ("GroupBox"),

BS_AUTORADIOBUTTON, TEXT ("AutoRadio"),

BS_OWNERDRAW, TEXT ("OwnErDraw")

};

 

#define NUM (sizeof button / sizeof button[0])

 

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

 

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)

{

static TCHAR szAppName[] = TEXT ("buttons");

HWND hwnd;

MSG msg;

WNDCLASS wndclass;

 

wndclass.style = CS_HREDRAW | CS_VREDRAW;

wndclass.lpfnWndProc = WndProc;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hInstance = hInstance;

wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);

wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = szAppName;

 

if (!RegisterClass (&wndclass))

{

MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR);

return 0;

}

 

hwnd = CreateWindow (szAppName, TEXT ("buttons"),

WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,

CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

 

ShowWindow (hwnd, iCmdShow);

UpdateWindow (hwnd);

while (GetMessage (&msg, NULL, 0, 0))

{

TranslateMessage (&msg);

DispatchMessage (&msg);

}

return msg.wParam;

}

 

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

static HWND hwndButton[NUM];

static RECT rect;

static TCHAR szTop[] = TEXT ("message wParam lParam"),

szUnd[] = TEXT ("_______ ______ ______"),

szFormat[] = TEXT ("%-16s%04X-%04X %04X-%04X"),

szBuffer[50];

static int cxChar, cyChar;

HDC hdc;

PAINTSTRUCT ps;

int i;

 

switch (message)

{

case WM_CREATE:

cxChar = LOWORD (GetDialogBaseUnits ());

cyChar = HIWORD (GetDialogBaseUnits ());

 

for (i = 0; i < NUM; i++)

hwndButton = CreateWindow (TEXT("button"),

button.szText, WS_CHILD | WS_VISIBLE | button.iStyle,

cxChar, cyChar * (1 + 2 * i), 20 * cxChar, 7 * cyChar / 4,

hwnd, (HMENU) i,

((LPCREATESTRUCT) lParam)->hInstance, NULL);

return 0;

 

case WM_SIZE:

rect.left = 24 * cxChar;

rect.top = 2 * cyChar;

rect.right = LOWORD (lParam);

rect.bottom = HIWORD (lParam);

return 0;

 

case WM_PAINT:

InvalidateRect (hwnd, &rect, TRUE);

 

hdc = BeginPaint (hwnd, &ps);

SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));

SetBkMode (hdc, TRANSPARENT);

 

TextOut (hdc, 24 * cxChar, cyChar, szTop, lstrlen (szTop));

TextOut (hdc, 24 * cxChar, cyChar, szUnd, lstrlen (szUnd));

 

EndPaint (hwnd, &ps);

return 0;

case WM_DRAWITEM:

case WM_COMMAND:

ScrollWindow (hwnd, 0, -cyChar, &rect, &rect);

 

hdc = GetDC (hwnd);

SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));

 

TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar -1),

szBuffer,

wsprintf (szBuffer, szFormat,

message == WM_DRAWITEM ? TEXT ("WM_DRAWITEM"):

TEXT ("WM_COMMAND"),

HIWORD (wParam), LOWORD (wParam),

HIWORD (lParam), LOWORD (lParam)));

 

ReleaseDC (hwnd, hdc);

ValidateRect (hwnd, &rect);

break;

 

case WM_DESTROY:

PostQuitMessage (0);

return 0;

}

return DefWindowProc (hwnd, message, wParam, lParam);

}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.