📄 windem32.c
字号:
/***************************************************************************
FILE : WINDEM32.C TYP ( PROG/UNIT ) : UNIT
AUTOR : rw
DATE : 14.08.2002
Description :
- WIN Demo for CanCam
***************************************************************************/
#include <windows.h>
#include <stdio.h>
#include <commctrl.h> // Headerfile der Common Controls
#include "windem32.h"
#include "Grabber.h"
#include "Dlgs.h"
#define WM_MOUSEWHEEL 0x020A
#include "CanCam.h"
static PARAMS params;
int width,height;
BOOL BMPFlag = FALSE;
BYTE v_size;
char host_adr[20];
WORD port_adr, itime, wx1=0, wx2=1279, wy1=0, wy2=1023;
DWORD exptime;
BYTE cal_param, hardware_version;
BYTE gain0,gain1,gain2,gain3,low_light;
WORD row_delay, frame_delay;
BYTE ss_mode = 1,ss_d_h = 1,ss_d_v = 1,ss_a_h = 1,ss_a_v = 1;
BYTE snapshot_trigger = 1, snapshot_shutter = 1,snapshot_reset = 1;
BYTE sensor_type, trigger_role, wb;
int vPos = 0;
int vPosRange = 0;
int hPos = 0;
int hPosRange = 0;
BOOL vEnable = TRUE,hEnable = TRUE;
BYTE io_port[4];
WORD img_cnt = 1;
DWORD fpga_mem_offset = 0;
BYTE camera = 1;
char szAppName[] = "CANCamDemo" ;
char szClientName[] = "CANCamClient" ;
static HINSTANCE hInst;
static OPENFILENAME ofn ;
static char szFileName[_MAX_PATH];
static HWND hwndStatusBar;
static HWND hwndClient;
int g_nStatusbarOffset;
extern BYTE *Prgb;
LRESULT CALLBACK WndProcClient (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
set_status_bar(int index,char *s_text)
{
SendMessage(hwndStatusBar,SB_SETTEXT, (WPARAM)(index | 0), (LPARAM)s_text);
}
void set_scroll_bar(HWND hwnd ,int lw, int w_img, int lh , int h_img)
{
if(lw > w_img)
{
ShowScrollBar(hwndClient,SB_HORZ,FALSE);
hEnable = FALSE;
}
else
{
hEnable = TRUE;
ShowScrollBar(hwndClient,SB_HORZ,TRUE);
}
if(lh > h_img)
{
ShowScrollBar(hwndClient,SB_VERT,FALSE);
vEnable = FALSE;
}
else
{
vEnable = TRUE;
ShowScrollBar(hwndClient,SB_VERT,TRUE);
}
if(vEnable)
{
vPosRange = abs(h_img - lh);
SetScrollRange (hwnd, SB_VERT, 0, vPosRange, FALSE) ;
SetScrollPos(hwnd, SB_VERT, 0, TRUE) ;
vPos = 0;
}
if(hEnable)
{
hPosRange = abs(w_img - lw);
SetScrollRange (hwnd, SB_HORZ, 0, hPosRange, FALSE) ;
SetScrollPos(hwnd, SB_HORZ, 0, TRUE) ;
hPos = 0;
}
}
void PopFileInitialize (HWND hwnd)
{
static char szFilter[] = "BITMAP Files (*.bmp)\0*.bmp\0\0" ;
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hwnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = szFileName; // Set in Open and Close functions
ofn.nMaxFile = _MAX_PATH ;
ofn.lpstrFileTitle = "Save Image"; // Set in Open and Close functions
ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL ;
ofn.Flags = OFN_OVERWRITEPROMPT; // Set in Open and Close functions
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = "bmp" ;
ofn.lCustData = 0L ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclass ;
HMENU hMenu;
int pa[4];
RECT rc;
//INITCOMMONCONTROLSEX iccx;
hInst = hInstance;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, "ICON_APP") ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (hInstance, "ICON_APP") ;
RegisterClassEx (&wndclass) ;
// Client Class
wndclass.lpfnWndProc = WndProcClient;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszClassName = szClientName;
wndclass.hIconSm = LoadIcon (hInstance, "ICON_APP") ;
RegisterClassEx (&wndclass) ;
// iccx.dwSize = sizeof( iccx );
// iccx.dwICC = ICC_BAR_CLASSES | ICC_COOL_CLASSES;
// InitCommonControlsEx( &iccx );
hwnd = CreateWindowEx (WS_EX_WINDOWEDGE,szAppName, "CanCam Demonstration",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
// Create Status Window (needs comctl32.lib)
hwndStatusBar = CreateStatusWindow (WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | CCS_BOTTOM | SBARS_SIZEGRIP,"",hwnd,2);
pa[0] = 300;
pa[1] = 400;
pa[2] = 470;
// pa[3] = 1020;
SendMessage(hwndStatusBar,SB_SETPARTS, (WPARAM)3, (LPARAM)pa);
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
// Client Window
GetWindowRect(hwndStatusBar, &rc );
g_nStatusbarOffset = rc.bottom - rc.top;
GetClientRect( hwnd, &rc );
rc.bottom -= g_nStatusbarOffset;
hwndClient = CreateWindowEx (WS_EX_CLIENTEDGE,szClientName, "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL,
rc.left, rc.top,
rc.right, rc.bottom,
hwnd, NULL, hInstance, NULL) ;
if(DialogBox (hInstance, "SETTINGSDLG", hwnd, SettingsDlg) == IDOK)
{
if (!initcancam(hwnd,host_adr,port_adr))
return 0;
io_port[0] = 1;
io_port[1] = 1;
io_port[2] = 0;
io_port[3] = 0;
}
else
return 0;
hMenu = GetMenu(hwnd);
CheckMenuItem(hMenu,IDM_SIZE_1280x1024,MF_CHECKED);
set_status_bar(2,"1280 x 1024");
CheckMenuItem(hMenu,IDM_LUT_NORMAL,MF_CHECKED);
if(sensor_type != 1)
EnableMenuItem(GetMenu(hwnd),IDM_WB,MF_GRAYED);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
char s_str[_MAX_PATH];
BYTE error;
DWORD dwId;
static HANDLE hThread1;
static GLOBALHANDLE hBitmapInfo = NULL;
static HDC PaintDC;
static HPALETTE OldPal=NULL,hPal;
static BYTE f_subsample=0;
// DWORD ptradr;
// BYTE *PPixel;
PAINTSTRUCT ps;
WORD x1,x2,y1,y2;
WORD cmd;
int i,iwidth,iheight;
BYTE p_val;
HCURSOR OldCursor;
WORD ihelp;
RECT r;
int ch,cw;
switch (iMsg)
{
case WM_MOUSEWHEEL:
if((int) wParam > 0)
vPos -= 10;
else
vPos += 10;
vPos = max(0,min(vPos,vPosRange));
if(vPos != GetScrollPos(hwndClient,SB_VERT))
{
SetScrollPos(hwndClient, SB_VERT, vPos, TRUE) ;
if(BMPFlag)
InvalidateRect(hwndClient,NULL,FALSE);
}
return 0;
case WM_CREATE :
PopFileInitialize (hwnd);
hBitmapInfo = create_bitmap_header();
ShowWindow(hwnd,SW_SHOWMAXIMIZED);
params.bKill = TRUE;
EnableMenuItem(GetMenu(hwnd),IDM_STOP,MF_GRAYED);
set_status_bar(2,"1280 x 1024");
// BMPFlag = TRUE;
return 0;
case WM_COMMAND :
switch (LOWORD (wParam))
{
case IDM_START :
EnableMenuItem(GetMenu(hwnd),IDM_SAVE,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_START,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_256x256,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_320x240,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_512x512,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_640x480,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_768x576,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_1024x1024,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_1280x1024,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_OPTO,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_CALIBRATE,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_CALIBRATE_PARAM,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_CALIBRATE_PARAM,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_ITIME,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_ITIME,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_EXPTIME,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_EXPTIME,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_GAIN,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_GAIN,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_VIDEO_WINDOW,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_VIDEO_WINDOW,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_ROW_DELAY,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_ROW_DELAY,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_FRAME_DELAY,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_FRAME_DELAY,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_EN_SUBSAMPLING,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_DIS_SUBSAMPLING,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_INIT_SNAPSHOT,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_SNAPSHOT_TRIGGER_AKTIV,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_SNAPSHOT_TRIGGER_INAKTIV,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_DIS_SNAPSHOT,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_LUT_NORMAL,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_LUT_INVERS,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_LUT_LOWLIGHT,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_GRAB_PARAM,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_EXIT,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_FPGA_READ_WRITE,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_STOP,MF_ENABLED);
params.bKill = FALSE;
params.hwnd = hwndClient ;
BMPFlag = FALSE;
InvalidateRect(hwnd, NULL, TRUE);
SendMessage(hwnd, WM_PAINT, 0, 0);
get_video_window(camera,&x1,&y1,&x2,&y2);
width = x2 - x1 +1;
height = y2 - y1+1;
GetClientRect(hwndClient,&r);
cw = r.right - r.left;
ch = r.bottom - r.top;
// set_scroll_bar(hwndClient,cw, width, ch , height);
if(img_cnt > 1)
height = (fpga_mem_offset * (int)(img_cnt)) / width;
set_scroll_bar(hwndClient,cw, width, ch , height);
hThread1=CreateThread(NULL,0,ThreadLive, ¶ms,0,&dwId);
return 0 ;
case IDM_STOP :
EnableMenuItem(GetMenu(hwnd),IDM_STOP,MF_GRAYED);
EnableMenuItem(GetMenu(hwnd),IDM_SAVE,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_START,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_256x256,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_320x240,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_512x512,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_640x480,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_768x576,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_1024x1024,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SIZE_1280x1024,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_OPTO,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_CALIBRATE,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_CALIBRATE_PARAM,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_CALIBRATE_PARAM,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_ITIME,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_ITIME,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_EXPTIME,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_EXPTIME,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_GAIN,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_GAIN,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_VIDEO_WINDOW,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_VIDEO_WINDOW,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_ROW_DELAY,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_ROW_DELAY,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_FRAME_DELAY,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_GET_FRAME_DELAY,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_EN_SUBSAMPLING,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_DIS_SUBSAMPLING,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_INIT_SNAPSHOT,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_SNAPSHOT_TRIGGER_AKTIV,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_SNAPSHOT_TRIGGER_INAKTIV,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_DIS_SNAPSHOT,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_LUT_NORMAL,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_LUT_INVERS,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_LUT_LOWLIGHT,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_SET_GRAB_PARAM,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_EXIT,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),IDM_FPGA_READ_WRITE,MF_ENABLED);
BMPFlag = TRUE;
params.bKill = TRUE;
CloseHandle(hThread1);
return 0 ;
case IDM_SIZE_256x256 :
case IDM_SIZE_320x240 :
case IDM_SIZE_512x512 :
case IDM_SIZE_640x480 :
case IDM_SIZE_768x576 :
case IDM_SIZE_1024x1024 :
case IDM_SIZE_1280x1024 :
cmd = LOWORD (wParam);
for(i = 0; i < 7;i++)
CheckMenuItem(GetMenu(hwnd),IDM_SIZE_256x256+i,MF_UNCHECKED);
CheckMenuItem(GetMenu(hwnd),cmd,MF_CHECKED);
switch(cmd)
{
case IDM_SIZE_256x256 : iwidth = 256; iheight = 256; break;
case IDM_SIZE_320x240 : iwidth = 320; iheight = 240; break;
case IDM_SIZE_512x512 : iwidth = 512; iheight = 512; break;
case IDM_SIZE_640x480 : iwidth = 640; iheight = 480; break;
case IDM_SIZE_768x576 : iwidth = 768; iheight = 576; break;
case IDM_SIZE_1024x1024 : iwidth = 1024; iheight = 1024; break;
case IDM_SIZE_1280x1024 : iwidth = 1280; iheight = 1024; break;
}
x1 = (1288/2) - (iwidth/2);
x2 = (1288/2) + (iwidth/2)-1;
y1 = (1024/2) - (iheight/2);
y2 = (1024/2) + (iheight/2)-1;
sprintf(s_str,"%ld x %ld",iwidth,iheight);
set_status_bar(2,s_str);
GetClientRect(hwndClient,&r);
cw = r.right - r.left;
ch = r.bottom - r.top;
if(img_cnt > 1)
iheight = (fpga_mem_offset * (int)(img_cnt)) / iwidth;
set_scroll_bar(hwndClient,cw, iwidth, ch , iheight);
error = set_video_window(camera,x1,y1,x2,y2);
if(error)
{
sprintf(s_str,"error %d set_video_window",error);
MessageBox(hwnd,s_str,szAppName,MB_OK | MB_ICONINFORMATION);
}
else
{
if (f_subsample)
{
error = enable_subsampling(camera,ss_mode,ss_d_h,ss_d_v,ss_a_h,ss_a_h);
if(error)
{
sprintf(s_str,"Enabling Subsampling Error = %d",error);
MessageBox(hwnd,s_str,szAppName,MB_OK | MB_ICONSTOP);
}
}
}
return 0 ;
case IDM_SAVE :
if(!GetSaveFileName (&ofn))
return 0;
error = SaveBmp(ofn.lpstrFile);
switch(error)
{
case 0: break;
case 1: MessageBox(hwnd,"Create file error", szAppName,MB_OK | MB_ICONSTOP);
break;
case 2: MessageBox(hwnd,"Write error", szAppName,MB_OK | MB_ICONSTOP);
break;
case 3: MessageBox(hwnd,"Live Mode active",szAppName,MB_OK | MB_ICONINFORMATION);
break;
case 5: MessageBox(hwnd,"No Bitmap to save", szAppName,MB_OK | MB_ICONINFORMATION);
break;
}
return 0 ;
case IDM_EXIT :
SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
return 0 ;
case IDM_OPTO:
DialogBox (hInst, "IODLG", hwnd, IODlg) ;
return 0;
case IDM_CALIBRATE:
OldCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
error = calibrate_sensor(camera);
SetCursor(OldCursor);
if(error)
{
sprintf(s_str,"Calibrate Error = %d",error);
MessageBox(hwnd,s_str,szAppName,MB_OK | MB_ICONSTOP);
}
return 0;
case IDM_SET_CALIBRATE_PARAM:
error = get_calibrate_param(camera,&p_val);
cal_param = p_val;
if(DialogBox (hInst, "SET_PARAMDLG", hwnd, SetParamDlg) == IDOK)
{
error = set_calibrate_param(camera,cal_param);
if(error)
{
sprintf(s_str,"Get Calibrate Param Error = %d",error);
MessageBox(hwnd,s_str,szAppName,MB_OK | MB_ICONSTOP);
}
}
return 0;
case IDM_GET_CALIBRATE_PARAM:
error = get_calibrate_param(camera,&p_val);
if(error)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -