W
Hier mal ein WinAPI-Programm, welches ich mal aus dem WinAPI-Forum gecopypastet habe:
#include <windows.h>
#include <vfw.h>
//#include "resource.h"
#define ID_BUTTON_FOTO 1
#define ID_BUTTON_CLOSE 2
#define ID_VIDEO_WINDOW 3
#define BUTTON_BREITE 60
#define BUTTON_HOHE 20
#define FENSTER_BREITE 350
#define FENSTER_HOHE 320
#define P_FILM 0
#define P_FOTO 1
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL FensterSkalieren (HWND) ;
BOOL ButtonsSkalieren (HWND, int, int) ;
BOOL BildSpeichern (HWND, PTSTR) ;
TCHAR szAppName[] = TEXT ("WebCam") ;
OPENFILENAME ofn ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow){
HWND hwnd ;
MSG msg ;
WNDCLASSEX wndclassex ;
wndclassex.cbSize = sizeof (WNDCLASSEX) ;
wndclassex.style = CS_HREDRAW | CS_VREDRAW ;
wndclassex.lpfnWndProc = WndProc ;
wndclassex.cbClsExtra = 0 ;
wndclassex.cbWndExtra = 0 ;
wndclassex.hInstance = hInstance ;
wndclassex.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (101)) ;
wndclassex.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclassex.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ;
wndclassex.lpszMenuName = szAppName ;
wndclassex.lpszClassName = szAppName ;
wndclassex.hIconSm = NULL ;
RegisterClassEx (&wndclassex) ;
hwnd = CreateWindowEx (NULL ,
szAppName, TEXT ("WebCam"),
WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU | WS_CAPTION,
CW_USEDEFAULT , CW_USEDEFAULT ,
FENSTER_BREITE, FENSTER_HOHE,
NULL, LoadMenu (hInstance, MAKEINTRESOURCE (102)), hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
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)
{
static HINSTANCE hInstance ;
static HWND hwndVideo ;
static int iIndex = 0 ;
char szDeviceName[80],
szDeviceVersion[80],
str[161],
szFileName[MAX_PATH] ;
static int iBreite = 0,
iHoche = 0,
iPruef = P_FOTO ;
switch (message){
case WM_CREATE :
hInstance = ((LPCREATESTRUCT) lParam) -> hInstance ;
CreateWindowEx (NULL, "button",
TEXT ("Foto"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0,
hwnd, (HMENU) ID_BUTTON_FOTO, hInstance, NULL) ;
CreateWindowEx (NULL, "button",
TEXT ("Ende"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0,
hwnd, (HMENU) ID_BUTTON_CLOSE, hInstance, NULL) ;
hwndVideo = capCreateCaptureWindow(
(LPSTR) "Meine WebCam",
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
(HWND) hwnd, (int) ID_VIDEO_WINDOW) ;
if (!capDriverConnect (hwndVideo, iIndex)) {
MessageBox (hwnd, "Treiber für die Webcam kann nicht geladen werden...", "Fehler", MB_ICONERROR) ;
SendMessage (hwnd, WM_DESTROY, 0, 0) ;
return 0;
}
capPreviewRate (hwndVideo, 33.3) ;
capPreview (hwndVideo, TRUE) ;
capGetDriverDescription (iIndex, szDeviceName, sizeof (szDeviceName),
szDeviceVersion, sizeof (szDeviceVersion)) ;
wsprintf (str, "%s, %s", szDeviceName, szDeviceVersion) ;
MessageBox (NULL, str, "Verbunden über...", NULL) ;
FensterSkalieren (hwnd) ;
ButtonsSkalieren (hwnd, iBreite, iHoche) ;
return 0 ;
case WM_SIZE :
iBreite = LOWORD (lParam) ;
iHoche = HIWORD (lParam) ;
return 0 ;
case WM_COMMAND :
switch (LOWORD (wParam)){
case ID_BUTTON_CLOSE :
SendMessage (hwnd, WM_DESTROY, 0, 0) ;
break ;
case ID_BUTTON_FOTO :
if (iPruef == P_FOTO){
SetWindowText (GetDlgItem (hwnd, ID_BUTTON_FOTO), "Film") ;
capPreview (hwndVideo, FALSE) ;
iPruef = P_FILM ;
}
else if (iPruef == P_FILM){
SetWindowText (GetDlgItem (hwnd, ID_BUTTON_FOTO), "Foto") ;
capPreview (hwndVideo, TRUE) ;
iPruef = P_FOTO ;
}
break ;
}
return 0 ;
case WM_DESTROY :
capPreview (hwndVideo, FALSE) ;
capDriverDisconnect (hwndVideo) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
BOOL BildSpeichern (HWND hParent, PTSTR pstrFileName){
ZeroMemory (&ofn, sizeof (OPENFILENAME)) ;
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = TEXT ("Bilddateiten (*.BMP)\0*.bmp\0")
TEXT ("Alle Dateien (*.*)\0*.*\0\0") ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.nMaxFile = MAX_PATH ;
ofn.nMaxFileTitle = MAX_PATH ;
ofn.lpstrInitialDir = NULL ;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = TEXT ("bmp") ;
ofn.lCustData = 0L ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
ofn.hwndOwner = hParent ;
ofn.lpstrFile = pstrFileName ;
ofn.lpstrFileTitle = NULL ;
ofn.Flags = OFN_OVERWRITEPROMPT ;
return GetSaveFileName (&ofn) ;
}
BOOL ButtonsSkalieren (HWND hwndParent, int iBreite, int iHoche){
MoveWindow (GetDlgItem (hwndParent, ID_BUTTON_CLOSE),
iBreite - BUTTON_BREITE, iHoche - BUTTON_HOHE,
BUTTON_BREITE, BUTTON_HOHE, TRUE) ;
MoveWindow (GetDlgItem (hwndParent, ID_BUTTON_FOTO),
iBreite - (2 * BUTTON_BREITE) - 5, iHoche - BUTTON_HOHE,
BUTTON_BREITE, BUTTON_HOHE, TRUE) ;
return TRUE ;
}
BOOL FensterSkalieren (HWND hParent){
CAPSTATUS CS ;
capGetStatus (GetDlgItem (hParent, ID_VIDEO_WINDOW), &CS, sizeof (CAPSTATUS)) ;
SetWindowPos (GetDlgItem (hParent, ID_VIDEO_WINDOW), NULL, 0, 0, CS.uiImageWidth,
CS.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE) ;
SetWindowPos (hParent, NULL, CW_USEDEFAULT, CW_USEDEFAULT, CS.uiImageWidth,
CS.uiImageHeight + 75, SWP_NOZORDER | SWP_NOMOVE) ;
return TRUE ;
}