Code:
// In main.cpp:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
UINT idb;
int iMarker, incdecflag;
switch(msg){
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case ID_CT_LOAD:
cw_LoadCT(hwnd);
if(cw_GlobalVar_ptr_CurrentCT != NULL){
cw_MarkerBoundaries_Initialize(&cw_GlobalVar_MarkerBoundaries, cw_GlobalVar_ptr_CurrentCT, 8);
cw_ThresholdInitialize(cw_GlobalVar_ptr_CurrentCT, &cw_GlobalVar_Threshhold, 8);
}
PostMessage(hwnd, WM_PAINT, 0, 0);
break;
case ID_CT_COMPUTE: // This is the menu function that updates/ modifies my image, and I need to see the update once this is done.
if(cw_GlobalVar_ptr_CurrentCT != NULL){
cw_ComputeCT(hwnd, cw_GlobalVar_ptr_CurrentCT);
}
PostMessage(hwnd, WM_PAINT, 0, 0);
break;
...
...
case WM_PAINT:
{
PAINTSTRUCT ps;
cw_Display();
BeginPaint( hwnd, &ps );
EndPaint( hwnd, &ps );
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int format;
// get the device context (DC)
*hDC = GetDC( hWnd );
// set the pixel format for the DC
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ChoosePixelFormat( *hDC, &pfd );
SetPixelFormat( *hDC, format, &pfd );
// create and enable the render context (RC)
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
glClearColor(0.875,0.875,0.875,1.0);
glClear(GL_COLOR_BUFFER_BIT);
SwapBuffers( hDCMain );
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
int quit = 0;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_OWNDC;
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 = MAKEINTRESOURCE(IDR_MENU1);
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;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
g_szClassName,
"Simulator",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL){
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
//ClientResize(hwnd, 642, 544);
hWndMain = hwnd;
ShowWindow(hwnd, nCmdShow);
//ShowWindow(hwnd, SW_SHOWMAXIMIZED);
UpdateWindow(hwnd);
{
//Create Push Buttons. First, Previous, Next, Last -- set the currect slice index
int iButton;
HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
for(iButton=0; iButton<4; iButton++){
hWndPushButton[iButton] = CreateWindowEx(
0,
//WS_EX_CLIENTEDGE,
"BUTTON",
lpPushButtonName[iButton],
//WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON |BS_NOTIFY ,
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON ,
420 + iButton*80,
4,
70,
24,
hwnd,
(HMENU)idPushButtonID[iButton],
hInstance,
NULL);
}
}
if(SetTimer(hwnd, ID_TIMER, 50, NULL) == 0)
MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
// enable OpenGL for the window
EnableOpenGL( hwnd, &hDCMain, &hRCMain );
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0){
//SetWindowText(hWndStaticText, "ABCD");
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
DisableOpenGL( hwnd, hDCMain, hRCMain );
#ifndef NDEBUG
FreeConsole();
#endif
return Msg.wParam;
}
void ClientResize(HWND hWnd, int nWidth, int nHeight){
RECT rcClient, rcWindow;
POINT ptDiff;
GetClientRect(hWnd, &rcClient);
GetWindowRect(hWnd, &rcWindow);
ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
MoveWindow(hWnd,rcWindow.left, rcWindow.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);
}
...
...
// In my drawing-related cpp file:
void cw_drawCT(void)
// Draw CT images in the viewport
{
if(cw_GlobalVar_ptr_CurrentCT != NULL){
cw_CT_SLICE *ptr_slice;
int irow, icol;
GLubyte *SliceImage;
ptr_slice = cw_GlobalVar_ptr_CurrentCT->ptr_Arr1D_ptr_CTSlice[cw_GlobalVar_CurrentSliceIndex];
SliceImage = (GLubyte *)malloc(sizeof(GLubyte)*ptr_slice->nCols*ptr_slice->nRows*3);
if(SliceImage==NULL){
MessageBox(hWndMain, "Fail to allocate memory", "Error", MB_OK | MB_ICONEXCLAMATION);
PostMessage(hWndMain, WM_CLOSE, 0, 0);
return;
}
for(irow=0;irow<ptr_slice->nRows; irow++){
for(icol=0; icol<ptr_slice->nCols; icol++){
int grayscale = (ptr_slice->data[irow*ptr_slice->nCols+icol]*256)/ptr_slice->MaxGrayScale;
SliceImage[(irow*ptr_slice->nCols+icol)*3] = (GLubyte) grayscale;
SliceImage[(irow*ptr_slice->nCols+icol)*3+1] = (GLubyte) grayscale;
SliceImage[(irow*ptr_slice->nCols+icol)*3+2] = (GLubyte) grayscale;
}
}
{ //1 : 1 only
glRasterPos2i(0, cw_GL_ViewPortHeight);
glPixelZoom(1.0, -1.0); //OPENGL line draw/scan from bottom to up.
glDrawPixels(ptr_slice->nCols, ptr_slice->nRows, GL_RGB, GL_UNSIGNED_BYTE, SliceImage);
glPixelZoom(1.0, 1.0);
}
free(SliceImage);
}
}
void cw_Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//Setup GL viewport
if(cw_GlobalVar_ptr_CurrentCT != NULL){
cw_CT_SLICE *pslice;
RECT rcClient;
pslice = cw_GlobalVar_ptr_CurrentCT->ptr_Arr1D_ptr_CTSlice[cw_GlobalVar_CurrentSliceIndex];
cw_GL_ViewPortOrigin_X = 280;
cw_GL_ViewPortOrigin_Y = 30 + pslice->nRows; //in client area coordinates
cw_GL_ViewPortWidth = pslice->nCols;
cw_GL_ViewPortHeight = pslice->nRows;
GetClientRect(hWndMain, &rcClient);
glViewport(cw_GL_ViewPortOrigin_X, rcClient.bottom - cw_GL_ViewPortOrigin_Y, cw_GL_ViewPortWidth, cw_GL_ViewPortHeight);
//GL viewport has a coordinate system different client area coordinate
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (double)cw_GL_ViewPortWidth, 0.0, (double)cw_GL_ViewPortHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
}
//draw CT image
cw_drawCT();
SwapBuffers( hDCMain );
return;
}