C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-14-2008, 07:58 PM   #1
Registered User
 
Join Date: Jul 2008
Posts: 4
Here is all my code that my project contains. I am trying to create a toolbar that lets me use my own images as buttons. I copied this directly off another webiste. I am using Dev-C++ as my compiler and I get no compiler errors when I run this, just the message saying "Your program has encountered an error and needs to close. I am also including the following library in the parameters section of project options in the linker box like this -lcomctl32

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   Reply With Quote
Old 07-14-2008, 11:03 PM   #2
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-15-2008, 01:48 AM   #3
Registered User
 
Join Date: Jul 2008
Posts: 4
I hope thats better, sorry for the irreadability.
taka209 is offline   Reply With Quote
Old 07-15-2008, 03:36 AM   #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   Reply With Quote
Old 07-15-2008, 01:30 PM   #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   Reply With Quote
Old 07-15-2008, 01:54 PM   #6
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-15-2008, 02:42 PM   #7
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
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   Reply With Quote
Old 07-15-2008, 09:03 PM   #8
Super Moderator
 
Bubba's Avatar
 
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   Reply With Quote
Old 07-15-2008, 09:42 PM   #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;
//.....
}
I deleted the stuff with static HINSTANCE ghInstance = NULL and just used the hInstance declared in the WinMain function. When I try to use hInstance in tbAddBitmap.hInst. I says hInstance is undeclared. I'm sorry this is so confusing, thank you for all your suggestions.
Attached Images
 
Attached Files
File Type: cpp main.cpp (3.2 KB, 46 views)
File Type: h resource.h (261 Bytes, 49 views)
taka209 is offline   Reply With Quote
Reply

Tags
bitmap, custom, error, needs to close, toolbar

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 03:01 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22