Skocz do zawartości

Przycisk na formie ...


Recommended Posts

Napisano

Cześć,
mam taki kod:

Kod napisał:
#include <windows.h>

static TCHAR lpszAppName[] = TEXT( "API Windows" );

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{
switch (uMsg)
{

case WM_CREATE:
CreateWindow("button", "Przycisk", WS_CHILD | WS_VISIBLE, 10,10,100,40, hWnd, (HMENU)100, hInstance, NULL);
break;

case WM_DESTROY:

PostQuitMessage(0);
break;

case WM_KEYDOWN:

break;

default:

return (DefWindowProc(hWnd, uMsg, wParam, lParam));

}
return(0L);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

MSG msg;
WNDCLASS wndclass;
HWND hWnd;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszAppName;

if(RegisterClass(&wndclass) == 0)

return FALSE;

hWnd = CreateWindow(lpszAppName, lpszAppName,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if(hWnd == NULL)
return FALSE;

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

I czemu wyskakuje error ? czemu nie wyswietla sie forma z przyciskiem :(:(? Robie chyba tak jak na tych lekcjach z WinAPI.org no i blad jest :/.

Napisano

Napisz jeszcze jaki blad wyskakuje to bedzie łatwiej. Te błędy nie wyskakują od tak sobie, ale informuja co jest nie tak. W przyszłości bedziesz sam rozszyfrowywał gdzie jest blad czytajac informacje od kompilatora ;)

Napisano
c:\mojedo~1\programy\md\md.cpp: In function `LRESULT MainWndProc(HWND__ *, unsigned int, unsigned int, long int)':
c:\mojedo~1\programy\md\md.cpp:12: `hInstance' undeclared (first use this function)
c:\mojedo~1\programy\md\md.cpp:12: (Each undeclared identifier is reported only once
c:\mojedo~1\programy\md\md.cpp:12: for each function it appears in.)

Takie błędy... Pinolec, wiem, niektóre błędy juz wyłapuje :) Ale tutaj nie wiem czy nawet w odpowiednim miejscu wrzucilem ten kod zeby wyswietlil sie button :/... JAk mozesz to napisz co jest zle....

Napisano

Chodzi oczywiście o hInstance, które nie jest znane w petli komunikatów.

proponuje:

switch (uMsg) 
{ 
static hInstance;

case WM_CREATE:

hInstance = ((LPCREATESTRUCT) lParam) -> hInstance;

CreateWindow("button", "Przycisk", WS_CHILD | WS_VISIBLE, 10,10,100,40, hWnd, (HMENU)100, hInstance, NULL); 
break;
//..........

Powinno pomoc ;) Ewentualnie ustawiasz hInstance globalnie i w WinMain robisz:

::hInstance = hInstance;

i tez bedzie działać.

powodzenia

Napisano

Hehehe. Gdy sie czlowiek spieszy to sie diabel cieszy. Oczywiscie powinno byc:

static HINSTANCE hInstance;

switch (uMsg) 
{ 
static HINSTANCE hInstance; // popraw tą linijke :D

case WM_CREATE: 

hInstance = ((LPCREATESTRUCT) lParam) -> hInstance; 

CreateWindow("button", "Przycisk", WS_CHILD | WS_VISIBLE, 10,10,100,40, hWnd, (HMENU)100, hInstance, NULL); 
break; 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...