Hello. I need to create a C application and i have to use a status bar. I think i can use MFC, but i don't know how to start. Can anyone help me?
Thanks.
Hello. I need to create a C application and i have to use a status bar. I think i can use MFC, but i don't know how to start. Can anyone help me?
Thanks.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
If I were going to do it, I'd first, go d/l the ascii table, if you don't have one. Look in the table, and ID the parts for drawing a rectangle - lines (horizontal and vertical char's), and the 4 corners.
Then practice with that until I could draw a rectangle, just fine. Then use ascii 185, 186 or 187, to mark the progress of your application, with some color - maybe blue, in the center of your rectangle. Ideally, it would have a size suitable for showing the increments you want, in the char's of the progress bar.
You only draw the box and (perhaps),the 186 char's, once, and you don't re-print the earlier progress char's, either. You just add another char in the next place, when it reaches the next 5%, 10%, whatever, and keep that up until it's at 100%.
Give it a try.
Last edited by Adak; 06-08-2008 at 01:05 AM.
I'm assuming that you want to do this in a GUI environment since you're referring to MFC. It's probably a lot easier to do with MFC. But I didn't want to post an MFC example containing a lot of lines of code. So, I'll post a very basic Win32 example containing minimal lines of code .I think i can use MFC, but i don't know how to start.
Code:#pragma comment( lib, "user32.lib" ) #pragma comment( lib, "comctl32.lib" ) #include <windows.h> #include <commctrl.h> const char ClassName[] = "MainWindowClass"; LRESULT CALLBACK MainWndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ) { HWND hStatus = NULL; switch (Msg) { case WM_CREATE: { int iStatBarWidths[] = {200, -1}; hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0, hWnd, (HMENU)NULL, GetModuleHandle(NULL), NULL); if(!hStatus) { MessageBox(NULL, "Failed To Create The Status Bar", "Error", MB_OK | MB_ICONERROR); return 0; } SendMessage(hStatus, SB_SETPARTS, sizeof(iStatBarWidths)/sizeof(int), (LPARAM)iStatBarWidths); SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Fabricio's first status bar"); } break; 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 ) { INITCOMMONCONTROLSEX iccex; iccex.dwSize = sizeof(iccex); iccex.dwICC = ICC_WIN95_CLASSES; WNDCLASSEX wc; HWND hWnd; MSG Msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = MainWndProc; 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 = ClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); InitCommonControlsEx(&iccex); if (!RegisterClassEx(&wc)) { MessageBox(NULL, "Failed To Register The Window Class.", "Error", MB_OK | MB_ICONERROR); return 0; } hWnd = CreateWindowEx( WS_EX_CLIENTEDGE, ClassName, "Fabricio's Status Bar", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 340, 120, NULL, NULL, hInstance, NULL); if (!hWnd) { MessageBox(NULL, "Window Creation Failed.", "Error", MB_OK | MB_ICONERROR); return 0; } ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); while (GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }
I'm referring to the complete working MFC example to display a status bar. Not just the code to display the status bar. I could post just sample MFC code. But that would'nt prove to the OP that it works. I always try to post minimal complete examples to prove that I'm not talking thru one of my orifaces.
Umm, if we exclude the dialog class itself, that would leave a message handler and a member function for handling the status bar which wouldn't be so big in itself.
Of course, it might vary if it's SDI or MDI, but if it's a dialog, it would be easy. At least I think.
The MFC code for status bar is about a 100 lines shorter than the Win32 sample. Create a new dialog app and then add a status bar control. Set its ID and then name it. All that is left is to view the MFC docs about CStatusBar and call the right function to get it to update.
In order to do this from a thread you will have to look at the CThread information. Worker threads should NEVER touch the GUI in MFC.
I'm not so sure about this. The fully functional Win32 example above has a 101 lines of code. Compile this example and you can run it from the command line.The MFC code for status bar is about a 100 lines shorter than the Win32 sample.
On the flip side, Adding a status bar to a MFC dialog Codeproject example has a total of 287 lines for the CPP files alone. That's not counting the header files.
The complete project code can be found in the Release Mode executable link from the above referenced page.
Funny, because I see only 28 lines of code in the article.
A lot of the code is auto-generated and thus doesn't count.
Plus you are unfairly counting the comments.
And it includes more code not necessary to just create a status bar. Sorry, but the comparison is just too unjust.
If we remove the "doesn't count" part from the Win32 example, we are left with:
Code:HWND hStatus=CreateWindowEx(0,STATUSCLASSNAME,NULL,WS_CHILD|WS_VISIBLE,0,0,0,0,hWnd,(HMENU)NULL,GetModuleHandle(NULL),NULL); SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Fabricio's first status bar");
"The Internet treats censorship as damage and routes around it." - John Gilmore