#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <tchar.h>
HANDLE g_clonse = 0;
LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM); //函数指针
int WINAPI WinMain(
_In_ HINSTANCE hInstance ,
_In_opt_ HINSTANCE hPrevInstance ,
_In_ LPSTR lpCmdLine ,
_In_ int nShowCmd
) {
static TCHAR szappname[] = TEXT("hellowin");
HWND hwnd; //窗口句柄
MSG msg; //消息结构体
WNDCLASS wndclass; //窗口类
AllocConsole();
g_clonse = GetStdHandle(STD_OUTPUT_HANDLE);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc; //窗口消息函数
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance; //包含窗口过程的实例句柄
wndclass.hIcon = LoadIcon(NULL , IDI_SHIELD);
wndclass.hCursor = LoadCursor(NULL , IDC_ARROW); //加载鼠标
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //返回以后强制转换类型为HBRUSH
wndclass.lpszMenuName = NULL; //菜单栏
wndclass.lpszClassName = szappname;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL , TEXT("This program requires windows nt!") , szappname , MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szappname ,
TEXT("mywindow") ,
WS_OVERLAPPEDWINDOW ,
800 ,
400 ,
600 ,
600 ,
NULL ,
NULL ,
hInstance , //应用程序实例句柄
NULL
);
ShowWindow(hwnd , nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg , NULL , 0 , 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void Ondraw(HDC hwnd ,LPARAM lparam , int x , int y ) {
//SetPixel(hwnd , LOWORD(lparam) , HIWORD(lparam) , RGB(0 , 0 , 255));
MoveToEx(hwnd , x , y , NULL);
LineTo(hwnd , LOWORD(lparam) , HIWORD(lparam) );
}
LRESULT CALLBACK WndProc(HWND hwnd , UINT message /* 消息*/ , WPARAM wparam /*64位其他信息*/ , LPARAM lparam /*64位其他消息*/) {
HDC hdcdisplay , hdcchlient;
HPEN hpen;
static int with , higth;
TCHAR szbuffer[28];
//memset(szbuffer , 0 , 28);
RECT rect;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
SetWindowText(hwnd , L"写字板");
break;
case WM_LBUTTONDOWN:
with = LOWORD(lparam);
higth = HIWORD(lparam);
break;
case WM_MOUSEMOVE:
switch (wparam)
{
case MK_LBUTTON:
{
hdcchlient = GetDC(hwnd);
hpen = CreatePen(PS_INSIDEFRAME , 10 , RGB(0 , 0 , 255));
HGDIOBJ hOldPen = SelectObject(hdcchlient , hpen);
Ondraw(hdcchlient , lparam , with, higth);
with = LOWORD(lparam);
higth = HIWORD(lparam);
SelectObject(hdcchlient , hOldPen);
DeleteObject(hpen);
RestoreDC(hdcchlient , NULL);
break;
}
default:
break;
}
break;
case WM_LBUTTONUP:
InvalidateRect(hwnd , NULL , FALSE);
break;
case WM_PAINT:
{
hdcchlient = BeginPaint(hwnd , &ps);
hpen = CreatePen(PS_SOLID , 10 , RGB(0 , 0 , 0));
swprintf_s(szbuffer , L"鼠标像素点 L : %d R: %d" , LOWORD(lparam) , HIWORD(lparam));
//TextOut(hdcchlient , 100 , 100 , szbuffer , lstrlen(szbuffer));
EndPaint(hwnd , &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd , message , wparam , lparam);
}
sdk 写字板
于 2023-08-25 17:33:49 首次发布