일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- API
- beans
- meta
- 악성코드
- HTML
- function
- jsp
- CLASS
- JavaScript
- 포인터
- OOP
- request
- Kafka
- WebProgramming
- java
- query
- 투자
- 윈도우즈
- 자료구조
- Call-by-reference
- algorithm
- System
- CSS
- windows
- array
- Sort
- UTF-8
- c++
- C
- 노드
- Today
- Total
hahahia
WinApi를 이용한 Notepad 본문
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include "resource.h"
using namespace std;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="Hahahia`s Notepad";
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0; // 윈도우 생성초기 설정
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=(WNDPROC)WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW | CS_VREDRAW;
WndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1); // Resource 생성 (메뉴)
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_THICKFRAME, // hWnd에 윈도우 생성 CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&Message,0,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
char * drawText;
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static char text[100][1000];
static int count, line;
static SIZE size;
int w_count = 0;
OPENFILENAME OFN, SFN;
char str[100], lpstrFile1[100] = "", lpstrFile2[100] = "";
char filter[] = "Every File(*.*) \0*.*\0Text File\0*.txt;*.doc\0";
int answer; // 질의
switch(iMessage) {
case WM_CREATE:
CreateCaret(hWnd, NULL, 5, 15);
ShowCaret(hWnd);
count = 0;
line = 0;
drawText = new char[1024];
strcpy(drawText, "");
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_FILEOPEN:
memset(&OFN, 0, sizeof(OPENFILENAME));
OFN.lStructSize = sizeof(OPENFILENAME);
OFN.hwndOwner = hWnd;
OFN.lpstrFilter = filter;
OFN.lpstrFile = lpstrFile1;
OFN.nMaxFile = 100;
OFN.lpstrInitialDir = ".";
if(GetOpenFileName(&OFN) != 0){
wsprintf(str, "%s 파일을 열겠습니까?", OFN.lpstrFile);
answer = MessageBox(hWnd, str, "열기선택", MB_YESNO);
if(answer == IDYES){
FILE *fptr = fopen(OFN.lpstrFile, "r+");
line = 0;
while(line < 100 && fgets(text[line], sizeof(text[0]), fptr)) // fgets로 문자열을 읽어옵니다 {
text[line][strlen(text[line])] = '\0'; // 끝을 NULL로 두져
count = strlen(text[line]);
if( text[line][strlen(text[line])-1] == 10 ) // 엔터키
{
line++;
count = 0;
}
}
fclose(fptr);
InvalidateRgn(hWnd, NULL, TRUE);
}
}
break;
case ID_FILESAVE:
memset(&SFN, 0, sizeof(OPENFILENAME));
SFN.lStructSize = sizeof(OPENFILENAME);
SFN.hwndOwner = hWnd;
SFN.lpstrFilter = filter;
SFN.lpstrFile = lpstrFile2;
SFN.nMaxFile = 100;
SFN.lpstrInitialDir = ".";
if(GetSaveFileName(&SFN) != 0)
{
wsprintf(str, "%s 파일로 저장하시겠습니까?", SFN.lpstrFile);
answer = MessageBox(hWnd, str, "저장하기 선택", MB_YESNOCANCEL);
if(answer == IDYES)
{
MessageBox(hWnd, "저장합니당", "저장", MB_OK);
FILE *fptr = fopen(SFN.lpstrFile, "w");
while( w_count <= line && w_count < 100)
{
fputs(text[w_count], fptr);
w_count++;
fputs("\n", fptr);
}
fclose(fptr);
}
else MessageBox(hWnd, "저장 ㄴㄴ", "취소", MB_OK);
}
break;
case ID_FILENEW:
answer = MessageBox(hWnd, "새 파일을 열겠습니까?", "새 파일 선택", MB_OKCANCEL);
if(answer == IDOK){
memset(text, 0, sizeof(text));
line = 0;
count = 0;
InvalidateRgn(hWnd, NULL, true);
}
break;
case IDHELP:
answer = MessageBox(hWnd, "WinAPI NodePad \nMade By Hahahia\n", "프로그램 정보", MB_OK);
break;
case ID_EXIT:
answer = MessageBox(hWnd, "파일을 저장하고 끝내시겠습니까?", "끝내기 선택", MB_YESNOCANCEL);
if(answer == IDYES || answer == IDNO)
PostQuitMessage(0);
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetTextExtentPoint(hdc, text[line], strlen(text[line]), &size);
for(int i=0; i<= line; i++)
{
TextOut(hdc, 0, i*20, text[i], strlen(text[i]));
}
SetCaretPos(size.cx, line*20);
EndPaint(hWnd, &ps);
if(count == 0)
ShowCaret(hWnd);
break;
case WM_CHAR:
if(wParam == VK_BACK) // 줄넘기기
{
if(count == 0 && line == 0) break;
count--;
if(count < 0)
{
line--;
count = strlen(text[line]);
}
}
else if(wParam == VK_RETURN) // enter키
{
count = 0;
line += 1;
}
else text[line][count++] = wParam; // 키보드로 받은값 출력(배열에추가)
text[line][count] = '\0';
strcpy(drawText, "");
for(int i=0;i <= line;i++)
{
strcat(drawText, text[i]);
strcat(drawText, "\n");
}
InvalidateRgn(hWnd, NULL, TRUE); // 동기화
break;
case WM_DESTROY:
HideCaret(hWnd);
DestroyCaret();
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
UI는 정말 별거없구요....대충만든거라..ㅠㅠㅠ
파일 -> 저장 들어가면 저장도되구요 ㅎ
저장하시겠습니까? ㅇㅋ
바탕화면에 있는 test라는 텍스트를 불러옵니다.
잘 나오죠 ㅎㅎ
한가지 흠이라면..... 개행에서 | 글자가 꼭 나와버리네요.... 이건 추후에 수정할게요 ㅠㅠ 은근히 복잡합니당
그리고 리소스 구현 방법도 자세히 포스팅 해보도록 하겠습니다
'Windows Programming > Windows::API' 카테고리의 다른 글
윈도우 속성들의 대한 다양한 변경 (0) | 2012.05.09 |
---|---|
window 빈 창 띄우는 예제(3) WndProc (0) | 2012.05.08 |
window 빈 창 띄우는 예제(2) (0) | 2012.05.08 |
Window 빈 창 띄우는 예제(1) (0) | 2012.05.08 |