![]() |
| | #1 |
| Registered User Join Date: Jul 2008
Posts: 4
| My project contains 3 files. 1 C++ source file, 1 Header file, 1 Resource File __________________________________________________ ______________ header file, resource.h: Code: #define IDC_TOOLBAR 1000
#define IDB_TOOLBAR 1001
#define IDM_BUTTON0 1002
#define IDM_BUTTON1 1003
#define IDM_BUTTON2 1004
#define IDM_BUTTON3 1005
#define IDM_BUTTON4 1006
#define IDM_BUTTON5 1007
#define IDM_BUTTON6 1008
#define IDM_BUTTON7 1009
resource file, ToolBarCustom.rc: Code: #include "resource.h"
IDB_TOOLBAR BITMAP "standard.bmp"
//I have a bitmap file in the same directory that is 96x16 and contains 6 images
c++ source file, main.cpp: Code: #include <windows.h>
#include <commctrl.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static char gszClassName[] = "MyWindowClass";
static HINSTANCE ghInstance = NULL;
HWND ghToolBar;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
ghInstance = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = ghInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = gszClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass)) {
MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_STATICEDGE,
gszClassName,
"Windows Title",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
320, 240,
NULL, NULL,
ghInstance,
NULL);
if(hwnd == NULL) {
MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
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) {
switch(Message) {
case WM_CREATE:
TBADDBITMAP tbAddBitmap;
TBBUTTON tbButton[8];
InitCommonControls();
ghToolBar = CreateWindowEx(
NULL,
TOOLBARCLASSNAME,
NULL,
WS_CHILD | WS_VISIBLE,
0, 0,
0, 0,
hwnd, (HMENU)IDC_TOOLBAR,
ghInstance,
NULL);
SendMessage(ghToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), NULL);
tbAddBitmap.hInst = ghInstance;
tbAddBitmap.nID = IDB_TOOLBAR;
SendMessage(ghToolBar, TB_ADDBITMAP, 0, (LPARAM) &tbAddBitmap);
ZeroMemory(tbButton, sizeof(tbButton));
tbButton[0].iBitmap = 0;
tbButton[0].fsState = TBSTATE_ENABLED;
tbButton[0].fsStyle = TBSTYLE_BUTTON;
tbButton[0].idCommand = IDM_BUTTON0;
tbButton[1].iBitmap = 1;
tbButton[1].fsState = TBSTATE_ENABLED;
tbButton[1].fsStyle = TBSTYLE_BUTTON;
tbButton[1].idCommand = IDM_BUTTON1;
tbButton[2].fsStyle = TBSTYLE_SEP;
tbButton[3].iBitmap = 2;
tbButton[3].fsState = TBSTATE_ENABLED;
tbButton[3].fsStyle = TBSTYLE_BUTTON;
tbButton[3].idCommand = IDM_BUTTON2;
tbButton[4].iBitmap = 3;
tbButton[4].fsState = TBSTATE_ENABLED;
tbButton[4].fsStyle = TBSTYLE_BUTTON;
tbButton[4].idCommand = IDM_BUTTON3;
tbButton[5].fsStyle = TBSTYLE_SEP;
tbButton[6].iBitmap = 4;
tbButton[6].fsState = TBSTATE_ENABLED;
tbButton[6].fsStyle = TBSTYLE_BUTTON;
tbButton[6].idCommand = IDM_BUTTON4;
tbButton[7].iBitmap = 5;
tbButton[7].fsState = TBSTATE_ENABLED;
tbButton[7].fsStyle = TBSTYLE_BUTTON;
tbButton[7].idCommand = IDM_BUTTON5;
SendMessage(ghToolBar, TB_ADDBUTTONS, 8, (LPARAM) &tbButton);
break;//WM_CREATE BREAK
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDM_BUTTON0:
case IDM_BUTTON1:
case IDM_BUTTON2:
case IDM_BUTTON3:
case IDM_BUTTON4:
case IDM_BUTTON5:
MessageBox(hwnd, "A Button was clicked.", "Toolbar Message", MB_ICONINFORMATION | MB_OK);
break;
}
break;//WM_COMMAND BREAK
case WM_SIZE:
SendMessage(ghToolBar, TB_AUTOSIZE, 0, 0);
break;//WM_SIZE BREAK
case WM_CLOSE:
DestroyWindow(hwnd);
break;//WM_CLOSE BREAK
case WM_DESTROY:
PostQuitMessage(0);
break;//WM_DESTROY BREAK
default://default for Message Switch statement
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
If any one can give me any suggestions what so ever that would be very helpful. Thank you. If anyone would like my files attached please let me know. Last edited by taka209; 07-15-2008 at 01:47 AM. |
| taka209 is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Yeah, first thing is indent your code so it's readable.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jul 2008
Posts: 4
| I hope thats better, sorry for the irreadability. |
| taka209 is offline | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| I'm pretty sure that if you run this with a debugger, you should be able to see the error on the call-stack. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #5 |
| Registered User Join Date: Jul 2008
Posts: 4
| Okay, I ran the debugger and I get a "Warning" message that pops up saying "An Access Violation (Segmentation Fault) raised in your program." |
| taka209 is offline | |
| | #6 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| So now you view the call stack window (it's on one of the menus), then you click on one of the functions (written by you) which is in the stack trace. Then you start looking at variables to try to determine the cause of the problem.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #7 |
| Rampaging 35 Stone Welsh Join Date: Apr 2007
Posts: 2,926
| MS debugger shoud take you right to the line where the error occured.
__________________ He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet |
| abachler is offline | |
| | #8 |
| Super Moderator Join Date: Aug 2001
Posts: 7,470
| Sounds to me like it didn't create your window or your window handle is invalid. I could be wrong but something doesn't look right about extended style static edge with base style overlapped window. If you attempt to show a window and pass in an invalid handle you very well could get this type of error or the show function may just completely ignore your request to show the window.
__________________ If you aim at everything you will hit something but you won't know what it is. |
| Bubba is offline | |
| | #9 |
| Registered User Join Date: Jul 2008
Posts: 4
| I think you may be right, im just not sure how to make it work correctly I have made the following changes. Code: LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static char gszClassName[] = "MyWindowClass";
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = 0;
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)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = gszClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//.........
hwnd = CreateWindowEx(
WS_EX_STATICEDGE,
gszClassName,
"Windows Title",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
320, 240,
NULL, NULL,
hInstance,
NULL);
//......
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
//......
TBADDBITMAP tbAddBitmap;
TBBUTTON tbButton[8];
InitCommonControls();
HWND ghToolBar;
ghToolBar = CreateWindowEx(0,TOOLBARCLASSNAME,NULL,WS_CHILD | WS_VISIBLE,0,0,0,0,hwnd,(HMENU)IDC_TOOLBAR,GetModuleHandle(NULL),NULL);
SendMessage(ghToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
tbAddBitmap.hInst = hInstance;//replaced it with hInstance
tbAddBitmap.nID = IDB_TOOLBAR;
//.....
}
|
| taka209 is offline | |
![]() |
| Tags |
| bitmap, custom, error, needs to close, toolbar |
| Thread Tools | |
| Display Modes | |
|