How to make a text box in C++?
How to make a text box in C++?
Those examples didn't work because I don't even have things like IDC_LIST or IDC_NUMBER etc.
You did define them in a header file and they are part of a resource script right?
operating systems: mac os 10.6, debian 5.0, windows 7
editor: back to emacs because it's more awesomer!!
version control: git
website: http://0xff.ath.cx/~as/
Where can I get the resource script?
I don't have these things defined in winuser.h
Well then I downloaded the source files of some examples. Heres one:
Compile log:Code:#include <windows.h> #include "resource.h" const char g_szClassName[] = "myWindowClass"; const int ID_TIMER = 1; const int BALL_MOVE_DELTA = 2; typedef struct _BALLINFO { int width; int height; int x; int y; int dx; int dy; }BALLINFO; BALLINFO g_ballInfo; HBITMAP g_hbmBall = NULL; HBITMAP g_hbmMask = NULL; HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent) { HDC hdcMem, hdcMem2; HBITMAP hbmMask; BITMAP bm; GetObject(hbmColour, sizeof(BITMAP), &bm); hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL); hdcMem = CreateCompatibleDC(0); hdcMem2 = CreateCompatibleDC(0); SelectObject(hdcMem, hbmColour); SelectObject(hdcMem2, hbmMask); SetBkColor(hdcMem, crTransparent); BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT); DeleteDC(hdcMem); DeleteDC(hdcMem2); return hbmMask; } void DrawBall(HDC hdc, RECT* prc) { HDC hdcBuffer = CreateCompatibleDC(hdc); HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom); HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer); HDC hdcMem = CreateCompatibleDC(hdc); HBITMAP hbmOld = SelectObject(hdcMem, g_hbmMask); FillRect(hdcBuffer, prc, GetStockObject(WHITE_BRUSH)); BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCAND); SelectObject(hdcMem, g_hbmBall); BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT); BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY); SelectObject(hdcMem, hbmOld); DeleteDC(hdcMem); SelectObject(hdcBuffer, hbmOldBuffer); DeleteDC(hdcBuffer); DeleteObject(hbmBuffer); } void UpdateBall(RECT* prc) { g_ballInfo.x += g_ballInfo.dx; g_ballInfo.y += g_ballInfo.dy; if(g_ballInfo.x < 0) { g_ballInfo.x = 0; g_ballInfo.dx = BALL_MOVE_DELTA; } else if(g_ballInfo.x + g_ballInfo.width > prc->right) { g_ballInfo.x = prc->right - g_ballInfo.width; g_ballInfo.dx = -BALL_MOVE_DELTA; } if(g_ballInfo.y < 0) { g_ballInfo.y = 0; g_ballInfo.dy = BALL_MOVE_DELTA; } else if(g_ballInfo.y + g_ballInfo.height > prc->bottom) { g_ballInfo.y = prc->bottom - g_ballInfo.height; g_ballInfo.dy = -BALL_MOVE_DELTA; } } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { UINT ret; BITMAP bm; g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL)); if(g_hbmBall == NULL) MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION); g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(0, 0, 0)); if(g_hbmMask == NULL) MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION); GetObject(g_hbmBall, sizeof(bm), &bm); ZeroMemory(&g_ballInfo, sizeof(g_ballInfo)); g_ballInfo.width = bm.bmWidth; g_ballInfo.height = bm.bmHeight; g_ballInfo.dx = BALL_MOVE_DELTA; g_ballInfo.dy = BALL_MOVE_DELTA; ret = SetTimer(hwnd, ID_TIMER, 50, NULL); if(ret == 0) MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_PAINT: { RECT rcClient; PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rcClient); DrawBall(hdc, &rcClient); EndPaint(hwnd, &ps); } break; case WM_TIMER: { RECT rcClient; HDC hdc = GetDC(hwnd); GetClientRect(hwnd, &rcClient); UpdateBall(&rcClient); DrawBall(hdc, &rcClient); ReleaseDC(hwnd, hdc); } break; case WM_DESTROY: KillTimer(hwnd, ID_TIMER); DeleteObject(g_hbmBall); DeleteObject(g_hbmMask); 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; 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, "An Animation Program", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }
And I get these errors with every example.Code:Compiler: Default compiler gcc.exe "C:\Programs\Dev-Cpp\source\anim_one\anim_one.c" -o "C:\Programs\Dev-Cpp\source\anim_one\anim_one.exe" -fexpensive-optimizations -O3 -g3 -I"C:\PROGRAMS\DEV-CPP\include" -L"C:\PROGRAMS\DEV-CPP\lib" -g3 C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x20): In function `CreateBitmapMask': C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:30: undefined reference to `GetObjectA@12' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x4d):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:31: undefined reference to `CreateBitmap@20' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x5f):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:33: undefined reference to `CreateCompatibleDC@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x70):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:34: undefined reference to `CreateCompatibleDC@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x81):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:36: undefined reference to `SelectObject@8' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x95):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:37: undefined reference to `SelectObject@8' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0xa7):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:39: undefined reference to `SetBkColor@8' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0xe3):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:41: undefined reference to `BitBlt@36' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x121):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:43: undefined reference to `BitBlt@36' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x12c):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:45: undefined reference to `DeleteDC@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x137):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:46: undefined reference to `DeleteDC@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x163): In function `DrawBall': C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:53: undefined reference to `CreateCompatibleDC@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x181):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:54: undefined reference to `CreateCompatibleBitmap@12' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x193):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:55: undefined reference to `SelectObject@8' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x1a4):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:57: undefined reference to `CreateCompatibleDC@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x1bb):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:58: undefined reference to `SelectObject@8' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x1cd):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:60: undefined reference to `GetStockObject@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x22a):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:62: undefined reference to `BitBlt@36' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x23f):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:64: undefined reference to `SelectObject@8' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x28a):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:65: undefined reference to `BitBlt@36' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x2cb):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:67: undefined reference to `BitBlt@36' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x2dd):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:69: undefined reference to `SelectObject@8' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x2e8):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:70: undefined reference to `DeleteDC@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x2fa):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:72: undefined reference to `SelectObject@8' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x305):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:73: undefined reference to `DeleteDC@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x313):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:74: undefined reference to `DeleteObject@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x483): In function `WndProc': C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:167: undefined reference to `DeleteObject@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x494):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:168: undefined reference to `DeleteObject@4' C:\WINDOWS\TEMP/ccoASjgb.o(.text+0x590):C:/Programs/Dev-Cpp/source/anim_one/anim_one.c:122: undefined reference to `GetObjectA@12' collect2: ld returned 1 exit status
Last edited by maxorator; 09-22-2005 at 08:20 AM.
the resource scripts can be used to include images to the exe (as showed in that sample; also you can use them to carry a menu, pointers to icon images, etc that not appear in that sample), so IDBALL will be a BITMAP that points to an existent file named as you want and formatted as *.bmp; also you should define the IDBALL as an integer number (look at the MAKEINTRESOURCE macro definition, that must use a DWORD integer, you use that macro in the LoadBitmap function, where you use the an HINSTANCE as first parameter, and a pointer to the bmp you want to load). That integer works as an index to the 'object' that contains the bitmap loaded into the exe while the compilation (that's not the exact description). Also you have to compile it in win32 gui mode to avoid those 'function@number' errors.
Niara
Ok, after adding that win32 gui thing to the linker options (-lgdi32) I get less errors.
Compiler log:
Code:Compiler: Default compiler Building Makefile: "C:\Programs\Dev-Cpp\source\font_one\Makefile.win" make.exe -f "C:\Programs\Dev-Cpp\source\font_one\Makefile.win" all g++.exe -c ../anim_one/anim_one.c -o ../anim_one/anim_one.o -I"C:/PROGRAMS/DEV-CPP/lib/gcc/mingw32/3.4.2/include" -I"C:/PROGRAMS/DEV-CPP/include/c++/3.4.2/backward" -I"C:/PROGRAMS/DEV-CPP/include/c++/3.4.2/mingw32" -I"C:/PROGRAMS/DEV-CPP/include/c++/3.4.2" -I"C:/PROGRAMS/DEV-CPP/include" ../anim_one/anim_one.c: In function `void DrawBall(HDC__*, RECT*)': ../anim_one/anim_one.c:55: error: invalid conversion from `void*' to `HBITMAP__*' ../anim_one/anim_one.c:58: error: invalid conversion from `void*' to `HBITMAP__*' ../anim_one/anim_one.c:60: error: invalid conversion from `void*' to `HBRUSH__*' ../anim_one/anim_one.c:60: error: initializing argument 3 of `int FillRect(HDC__*, const RECT*, HBRUSH__*)' g++.exe ../anim_one/anim_one.o Projekt1_private.res -o "Projekt1.exe" -L"C:/PROGRAMS/DEV-CPP/lib" -mwindows -lgdi32 G__~1.EXE: ../anim_one/anim_one.o: No such file or directory
Dev-C++ likes to compile .c files as C++. You need to go into project options and untick the item about compiling with C++.
I still have a little problem with it:
I always get those "could not load idball" and "could not create mask" messageboxes so this is still useless example...Code:case WM_CREATE: { UINT ret; BITMAP bm; g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL)); if(g_hbmBall == NULL) MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION); g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(0, 0, 0)); if(g_hbmMask == NULL) MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION); GetObject(g_hbmBall, sizeof(bm), &bm); ZeroMemory(&g_ballInfo, sizeof(g_ballInfo)); g_ballInfo.width = bm.bmWidth; g_ballInfo.height = bm.bmHeight; g_ballInfo.dx = BALL_MOVE_DELTA; g_ballInfo.dy = BALL_MOVE_DELTA; ret = SetTimer(hwnd, ID_TIMER, 50, NULL); if(ret == 0) MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION); } break;
The examples include the resource files. Add it to the project.
>> so this is still useless example...
That is a retarded attitude. Read the content of the tutorials.
For the first error the IDB_BALL defined on the resource should point to a valid *.bmp file, and you should have defined too the IDB_BALL as an integer. The second error will be solved when the function loads a valid bmp into your HBITMAP.
Niara
You don't need to worry about resource files for something as simple as a textbox. Here is a simple program that you can play around with. It just creates a textbox.
Code:#include <windows.h> /* Link with user32.lib or equivalent */ LRESULT CALLBACK MainMessageHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CREATE: { CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT("Write here!"), WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU) 0xED, GetModuleHandle(NULL), NULL); /* Fall through */ } case WM_SIZE: { RECT rc; GetClientRect(hwnd, &rc); MoveWindow(GetDlgItem(hwnd, 0xED), rc.left, rc.top, rc.right, rc.bottom, TRUE); return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hwnd, uMsg, wParam, lParam); } HWND CreateMainWindow(INT nShow) { WNDCLASSEX wc = { 0 }; HWND hwnd = NULL; wc.cbSize = sizeof(wc); wc.style = 0; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = GetModuleHandle(NULL); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIconSm = NULL; wc.lpfnWndProc = MainMessageHandler; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("SimpleEditContainer"); if(RegisterClassEx(&wc)) { hwnd = CreateWindowEx(0, TEXT("SimpleEditContainer"), TEXT("Main Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), NULL); ShowWindow(hwnd, nShow); } return hwnd; } INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE reserved, LPSTR szCommand, INT nShow) { MSG msg; CreateMainWindow(nShow); while ( GetMessage(&msg, NULL, 0, 0) ) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; }