📄 oscillograph.cpp
字号:
// oscillograph.cpp : Defines the entry point for the application.
//
#include <windows.h>
#include <windowsx.h>
#include <aygshell.h>
#include "resource.h"
#include "oscillograph.h"
#include <Winuser.h>
#include <Wingdi.h>
HINSTANCE hInst = NULL;
const TCHAR szAppName[] = TEXT("oscillograph");
RECT rect;
int data[][3]={{65,96,73},{67,104,71},{70,110,79},{75,118,74},{80,120,78},
{88,128,73},{97,130,75},{100,134,73},{105,139,76},{98,130,74},{94,126,72},
{90,120,76},{85,118,73},{80,110,75},{75,107,78}};
int i=0;
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_PAINT, DoPaintMain,
WM_TIMER, DoTimerMain,
WM_COMMAND, DoCommandMain,
WM_DESTROY, DoDestroyMain,
};
// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
IDMOK, DoMainCommandExit,
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
HWND hwndMain;
// Initialize application.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0) return 0x10;
// Application message loop
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage (&msg);
DispatchMessage(&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
HWND hWnd;
WNDCLASS wc;
// Allow only one instance of the application.
hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return 0;
}
// Register application main window class.
wc.style = CS_VREDRAW | CS_HREDRAW; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW); // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 0;
// Save program instance handle in global variable.
hInst = hInstance;
// Create main window.
hWnd = CreateWindow (szAppName, TEXT("oscillograph"), WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!IsWindow (hWnd)) return 0; // Fail if not created.
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
int i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
CreateMenubar(hWnd);
GetClientRect(hWnd,&rect);
SetTimer(hWnd,1,500,NULL);
return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
DrawCoor(hWnd);
return 0;
}
//----------------------------------------------------------------------
// DoTimerMain - Process WM_TIMER message for window.
//
LRESULT DoTimerMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
DrawLine(hWnd);
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
WORD idItem, wNotifyCode;
HWND hwndCtl;
int i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl, wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
SendMessage (hWnd, WM_CLOSE, 0, 0);
return 0;
}
LRESULT CreateMenubar(HWND hWnd){
// create the menu bar
SHMENUBARINFO mbi;
ZeroMemory(&mbi, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_OSCI_MENUBAR;
mbi.hInstRes = hInst;
if(!SHCreateMenuBar(&mbi))
return(-1);
return(0);
}
LRESULT DrawCoor(HWND hWnd){
PAINTSTRUCT ps;
HDC hDc,hMemDc;
HBITMAP hBmp;
HGDIOBJ hOldSel;
hDc = BeginPaint (hWnd, &ps);
hBmp=LoadBitmap(hInst,MAKEINTRESOURCE(1));
hMemDc=CreateCompatibleDC(hDc);
hOldSel=SelectObject(hMemDc,hBmp);
BitBlt(hDc,0,0,rect.right,rect.bottom,hMemDc,0,0,SRCCOPY);
SelectObject(hMemDc,hOldSel);
DeleteObject(hMemDc);
EndPaint (hWnd, &ps);
return 0;
}
LRESULT DrawLine(HWND hWnd){
HDC hDc,hMemDc;
POINT basepoint,pt[2];
HPEN hpRGB[3];
HBITMAP hBmp;
HGDIOBJ hOldSel1,hOldSel2;
int j,m,n,count;
hDc=GetDC(hWnd);
hBmp=LoadBitmap(hInst,MAKEINTRESOURCE(1));
hMemDc=CreateCompatibleDC(hDc);
hOldSel1=SelectObject(hMemDc,hBmp);
BitBlt(hDc,0,0,rect.right,rect.bottom,hMemDc,0,0,SRCCOPY);
SelectObject(hMemDc,hOldSel1);
DeleteObject(hMemDc);
basepoint.x=22;
basepoint.y=rect.bottom-44;
pt[0].x=basepoint.x;
pt[0].y=basepoint.y;
pt[1].x=basepoint.x;
pt[1].y=basepoint.y;
hpRGB[0]=CreatePen(PS_SOLID,1,RGB(255,0,0));
hpRGB[1]=CreatePen(PS_SOLID,1,RGB(0,255,0));
hpRGB[2]=CreatePen(PS_SOLID,1,RGB(0,0,255));
hOldSel2=SelectObject(hDc,hpRGB[0]);
count=(rect.right-43)/4;
for(j=0;j<3;j++){
if(i<=count){
m=i;
n=0;
}
else{
m=count;
n=i-count;
}
SelectObject(hDc,hpRGB[j]);
while((m>=0)&&(i<dim(data))){
pt[0].x=basepoint.x+m*4;
pt[0].y=basepoint.y-(data[n][j]-50)*2;
if(m>0){
pt[1].x=basepoint.x+(m-1)*4;
pt[1].y=basepoint.y-(data[n+1][j]-50)*2;
}
Polyline(hDc,(const POINT*)pt,2);
m--;
n++;
}
}
i++;
SelectObject(hDc,hOldSel2);
for(j=0;j<3;j++)
DeleteObject(hpRGB[j]);
DeleteObject(hBmp);
ReleaseDC(hWnd,hDc);
return 0;
}
// end oscillograph.cpp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -